Common Git Commands
Here are some common Git commands used in daily version control tasks:
1. Basic Git Commands
-
git init: Initializes a new Git repository in the current directory.
git init
-
git clone: Clones an existing repository from a remote server.
git clone https://github.com/user/repo.git
-
git status Shows the current status of your working directory and staging area.
git status
-
git add: Stages changes (files or directories) for the next commit.
git add file.txt git add . # Stage all changes
-
git commit: Records the staged changes in the repository with a message.
git commit -m "Commit message"
-
git push: Pushes the local commits to the remote repository.
git push origin branch-name
-
git pull: Fetches and integrates changes from the remote repository to the local branch.
git pull origin branch-name
2. Branching and Merging
-
git branch: Lists, creates, or deletes branches.
git branch # List all branches git branch new-branch # Create a new branch git branch -d old-branch # Delete a branch
-
git checkout: Switches to a different branch or commit.
git checkout branch-name git checkout -b new-branch # Create and switch to a new branch
-
git merge: Merges a branch into the current branch.
git checkout main git merge feature-branch
3. Remote Repositories
-
git remote: Manages the remote repository connections.
git remote -v # List remote connections git remote add origin https://github.com/user/repo.git # Add a new remote
-
git fetch: Downloads changes from the remote repository but does not integrate them.
git fetch origin
4. Viewing History
-
git log: Displays the commit history.
git log # Show commit history git log --oneline # Condensed history view git log --graph --oneline --decorate # Graphical commit history
-
git diff: Shows changes between commits, branches, or the working directory.
git diff # Changes not staged git diff --staged # Changes staged for commit git diff branch1 branch2 # Compare two branches
5. Undoing Changes
-
git reset: Resets the staging area or moves the branch pointer.
git reset file.txt # Unstage a file git reset --hard HEAD^ # Revert to the previous commit
-
git revert: Reverts a specific commit by creating a new commit that undoes it.
git revert commit-sha
6. Stashing Changes
-
git stash: Temporarily saves changes that are not ready to be committed.
git stash # Stash current changes git stash pop # Apply the stashed changes and remove them from the stash git stash list # List all stashed changes
7. Tagging
-
git tag: Creates tags to mark specific commits, typically used for releases.
git tag v1.0 # Create a lightweight tag git tag -a v1.0 -m "Version 1.0" # Annotated tag git push origin v1.0 # Push tag to remote
8. Collaboration
-
git rebase: Re-applies commits on top of another base tip, useful for linearizing commit history.
git checkout feature-branch git rebase main
-
git cherry-pick: Applies a specific commit from one branch to another.
git cherry-pick commit-sha
These commands are foundational for working with Git in both solo and collaborative projects.
Git Flow Overview
Git Flow is a branching strategy for managing feature development, releases, and hotfixes in a structured and scalable way. It helps teams manage parallel development, collaborate efficiently, and handle releases and bug fixes smoothly. Here's a brief overview of the common Git Flow:
1. Main Branches
- main (or master): The production-ready branch that contains the stable, deployed code. All releases are tagged here.
- develop: The main development branch where the latest code for the next release is integrated. Features and fixes are merged into
develop
.
2. Supporting Branches
Git Flow uses additional branches for feature development, bug fixes, and releases. Each branch has a specific role:
-
Feature Branches (
feature/
):- Used to develop new features or changes.
- Created from
develop
and merged back intodevelop
once complete. - Example:
feature/new-login
-
Release Branches (
release/
):- Used to prepare a new release.
- Created from
develop
when it’s feature-complete, allowing for final testing, bug fixes, and versioning before merging intomain
. - Example:
release/v1.0
-
Hotfix Branches (
hotfix/
):- Used to fix critical bugs in production.
- Created from
main
and merged back into bothmain
anddevelop
to ensure the fix is applied to both branches. - Example:
hotfix/fix-payment-bug
3. Git Flow Workflow
- Feature Development: Developers create a feature branch (
feature/
) fromdevelop
to work on new features. After completion, the feature is merged back intodevelop
. - Releasing: Once
develop
is stable and ready for release, arelease/
branch is created for final testing and adjustments. Once ready, it's merged intomain
(for production) anddevelop
(to keep it updated). - Hotfixes: If a critical bug is found in production, a
hotfix/
branch is created frommain
, fixed, and then merged into bothmain
anddevelop
to keep both updated.
4. Version Tags
After merging a release/
or hotfix/
into main
, you tag the commit with a version number (e.g., v1.0
) to mark the release.
Summary of Key Git Flow Commands
- Start a feature:
git checkout -b feature/xyz develop
-
Finish a feature:
git checkout develop && git merge feature/xyz
-
Start a release:
git checkout -b release/v1.0 develop
-
Finish a release:
git checkout main && git merge release/v1.0
-
Start a hotfix:
git checkout -b hotfix/fix-bug main
-
Finish a hotfix:
git checkout main && git merge hotfix/fix-bug
Git Flow is ideal for teams that require a structured process for managing releases, features, and hotfixes, particularly in projects with regular production releases.