Comprehensive Guide to Git Commands
Git is an essential tool for version control in software development. It helps developers track changes in their code, collaborate efficiently, and manage multiple versions of a project. To maximize your Git workflow, it's crucial to familiarize yourself with its core commands. This article provides a comprehensive list of Git commands categorized for easier reference, from basic operations to advanced techniques.
Basic Git Commands
-
git init
Initialize a new Git repository in your project directory.
git init
-
git clone <repository-url>
Clone a remote repository to your local machine.
git clone https://github.com/user/repository.git
-
git status
Show the current state of the working directory and staging area (i.e., which files are staged, unstaged, or untracked).
git status
-
git add <file>
Add changes in a specific file to the staging area.
git add index.html
-
git commit -m "message"
Commit staged changes to the repository with a descriptive message.
git commit -m "Initial commit"
-
git log
View the commit history of the repository.
git log
-
git diff
Show the differences between the working directory and the index (staging area).
git diff
Branching and Merging Commands
-
git branch
List all branches in your repository.
git branch
-
git branch <branch-name>
Create a new branch.
git branch feature-xyz
-
git checkout <branch-name>
Switch to the specified branch.
git checkout feature-xyz
-
git merge <branch-name>
Merge the specified branch into the current branch.
git merge feature-xyz
-
git branch -d <branch-name>
Delete a local branch after it has been merged.
git branch -d feature-xyz
-
git merge --abort
Abort the merge process if conflicts arise and revert to the pre-merge state.
git merge --abort
Remote Repositories and Collaboration
-
git remote add <name> <url>
Add a new remote repository URL.
git remote add origin https://github.com/user/repository.git
-
git fetch
Fetch all branches from the remote repository, without merging them into your local branches.
git fetch origin
-
git pull
Fetch and merge changes from the remote repository into the current branch.
git pull origin main
-
git push
Push your local commits to a remote repository.
git push origin main
-
git push -u origin <branch-name>
Push a branch to the remote repository and set the upstream tracking branch.
git push -u origin feature-xyz
-
git remote -v
List the remote repositories associated with the current repository.
git remote -v
Undoing Changes
-
git reset <file>
Unstage a file, keeping the changes in the working directory.
git reset index.html
-
git reset --hard
Reset the repository to a specific commit and discard changes in the working directory.
git reset --hard HEAD
-
git revert <commit-hash>
Create a new commit that undoes the changes made by a specific commit.
git revert abc123
-
git checkout -- <file>
Discard changes in the working directory for a specific file, reverting it to the last committed state.
git checkout -- index.html
-
git stash
Temporarily store changes that are not yet ready to be committed.
git stash
-
git stash pop
Retrieve and apply the most recent stash.
git stash pop
Advanced Git Commands
-
git rebase <branch>
Rebase your current branch onto the given branch, integrating changes from the upstream branch.
git rebase main
-
git rebase --interactive
Launch an interactive rebase to edit, reorder, or squash commits.
git rebase -i HEAD~5
-
git cherry-pick <commit-hash>
Apply the changes introduced by an existing commit to the current branch.
git cherry-pick abc123
-
git bisect
Use binary search to find the commit that introduced a bug by checking out various commits between two points.
git bisect start
-
git reflog
Show the history of changes to theHEAD
reference, which helps recover lost commits.
git reflog
-
git filter-branch
Rewrite the commit history of a repository, useful for removing files or changing commit metadata.
git filter-branch --tree-filter 'rm -rf file' HEAD
Git Configuration and Setup
-
git config --global user.name "Your Name"
Set your username globally for Git commits.
git config --global user.name "Your Name"
-
git config --global user.email "youremail@example.com"
Set your email globally for Git commits.
git config --global user.email "youremail@example.com"
-
git config --list
List all the configuration settings for Git.
git config --list
-
git config --global core.editor <editor>
Set the default editor for commit messages and Git operations.
git config --global core.editor "vim"
Git Logs and History
-
git log
View the commit history with a detailed log of each commit.
git log
-
git log --oneline
View the commit history in a simplified format with one line per commit.
git log --oneline
-
git log --graph
View the commit history in a graphical representation.
git log --graph
-
git show <commit-hash>
Display detailed information about a specific commit.
git show abc123
Tagging in Git
-
git tag <tag-name>
Create a new tag at the current commit.
git tag v1.0
-
git tag -a <tag-name> -m "message"
Create an annotated tag with a message.
git tag -a v1.0 -m "Version 1.0 release"
-
git push origin <tag-name>
Push a specific tag to a remote repository.
git push origin v1.0
-
git push --tags
Push all tags to a remote repository.
git push --tags
Git Workflow and Management
-
git rm <file>
Remove a file from both the working directory and the staging area.
git rm index.html
-
git mv <old-filename> <new-filename>
Rename or move a file in your Git repository.
git mv oldfile.txt newfile.txt
Configuration and Setup
-
git config --global core.autocrlf true|false
Set Git's behavior with line endings. When set totrue
, it automatically converts line endings to the appropriate format for the operating system.
git config --global core.autocrlf true
-
git config --global core.fileMode false
Disable file permission changes from being tracked by Git.
git config --global core.fileMode false
-
git config --global color.ui auto
Enable colorized output in Git commands for better readability.
git config --global color.ui auto
-
git config --global merge.tool <tool>
Set the default merge tool for resolving conflicts.
git config --global merge.tool vimdiff
Stashing and Cleaning
-
git stash list
List all stashes stored in your repository.
git stash list
-
git stash show
Show changes saved in the latest stash.
git stash show
-
git stash apply
Apply a stash without removing it from the stash list.
git stash apply
-
git clean -n
Preview which files would be removed bygit clean
.
git clean -n
-
git clean -f
Clean untracked files from your working directory.
git clean -f
-
git clean -fd
Remove both untracked files and directories.
git clean -fd
Git Log and History
-
git log --oneline --graph --decorate
View a simplified commit history with a graphical representation and decorations (branch names, tags).
git log --oneline --graph --decorate
-
git log --since="2 weeks ago"
Show commits made in the last two weeks.
git log --since="2 weeks ago"
-
git log --author="Author Name"
View commits made by a specific author.
git log --author="John Doe"
-
git log --grep="fix bug"
Search commit messages for a specific pattern.
git log --grep="fix bug"
-
git log --stat
Show statistics for each commit (added/deleted lines).
git log --stat
-
git blame <file>
View line-by-line commit history for a file, showing which commit last modified each line.
git blame index.html
-
git show-branch
Display the commit history for all branches.
git show-branch
Rebasing and Interactive Rebasing
-
git rebase --continue
After resolving a conflict during a rebase, continue the process.
git rebase --continue
-
git rebase --skip
Skip the current patch in the rebase process if it causes conflicts.
git rebase --skip
-
git rebase --abort
Abort the rebase process and restore the repository to its original state.
git rebase --abort
-
git rebase -i HEAD~3
Start an interactive rebase for the last three commits.
git rebase -i HEAD~3
Cherry-pick and Squashing
-
git cherry-pick -x <commit-hash>
Apply a commit from another branch, and include a reference to the original commit in the commit message.
git cherry-pick -x abc123
-
git merge --squash <branch-name>
Squash all the changes from the specified branch into a single commit without committing immediately.
git merge --squash feature-xyz
-
git commit --amend
Modify the most recent commit, including changing the commit message or adding new changes.
git commit --amend
Advanced Tagging
-
git tag -d <tag-name>
Delete a tag from the local repository.
git tag -d v1.0
-
git push origin --delete <tag-name>
Delete a tag from the remote repository.
git push origin --delete v1.0
Managing Remotes
-
git remote remove <remote-name>
Remove a remote repository from your configuration.
git remote remove origin
-
git remote rename <old-name> <new-name>
Rename a remote repository.
git remote rename origin upstream
-
git remote show <remote-name>
Show detailed information about a specific remote repository.
git remote show origin
Repository Maintenance
-
git gc
Run the garbage collection process to optimize the repository by cleaning up unnecessary files and optimizing the local database.
git gc
-
git fsck
Check the integrity of the Git repository and verify the connectivity of all objects.
git fsck
-
git reflog expire --expire=now --all
Remove all expired reflog entries from the repository.
git reflog expire --expire=now --all
Remote Repositories and Forking
-
git fork
Fork a repository on GitHub to create your own copy. This command is not natively available in Git but is facilitated by GitHub's interface.
git fork
-
git pull --rebase
Fetch the latest changes from the remote branch and rebase your local commits on top of them. This prevents unnecessary merge commits.
git pull --rebase
-
git remote prune <remote-name>
Remove references to remote branches that no longer exist.
git remote prune origin
Other Git Commands
-
git archive --format=zip --output=archive.zip <branch-name>
Create an archive of a specific branch or commit.
git archive --format=zip --output=archive.zip main
-
git submodule update --init
Initialize, fetch, and checkout the submodule for a repository.
git submodule update --init
-
git submodule update --remote
Update all submodules to their latest commit.
git submodule update --remote
-
git bisect start
Start the bisect process to help locate the commit that introduced a bug.
git bisect start
-
git bisect bad
Mark the current commit as bad when usinggit bisect
.
git bisect bad
-
git bisect good <commit-hash>
Mark the commit as good in the bisect process to help narrow down the problematic commit.
git bisect good abc123
Commands Related to:
Committing Changes
-
git add -p
Add changes interactively, allowing you to choose specific changes or chunks of changes to commit.
git add -p
-
git commit --no-edit
Commit changes without modifying the commit message (useful after a merge).
git commit --no-edit
Branching and Merging
-
git branch -m <old-branch-name> <new-branch-name>
Rename a branch.
git branch -m old-branch new-branch
-
git merge --no-ff <branch-name>
Perform a merge with a merge commit, even if the merge could be fast-forwarded.
git merge --no-ff feature-branch
-
git merge --squash <branch-name>
Squash changes from another branch and stage them for a commit without creating a merge commit.
git merge --squash feature-branch
-
git merge --abort
Abort a merge if conflicts arise or if you want to cancel the operation.
git merge --abort
-
git rebase -i
Start an interactive rebase, allowing you to edit commit messages, squash commits, or reorder them.
git rebase -i HEAD~3
-
git rebase --onto <new-base> <upstream> <branch>
Rebase a branch onto a different base.
git rebase --onto new-base upstream branch
Collaboration
-
git push --force-with-lease
Force push while ensuring that you do not overwrite changes made by others in the branch.
git push --force-with-lease
-
git pull --rebase
Fetch the latest changes from the remote branch and apply your local commits on top.
git pull --rebase
-
git fetch --all
Fetch updates from all remote repositories.
git fetch --all
Undoing Changes
-
git reset --soft <commit>
Move HEAD to the specified commit and keep changes in the staging area.
git reset --soft HEAD~1
-
git reset --mixed <commit>
Move HEAD to the specified commit, keeping changes in the working directory but unstaged.
git reset --mixed HEAD~1
-
git reset --hard <commit>
Reset to the specified commit, discarding all changes.
git reset --hard HEAD~1
-
git revert <commit>
Create a new commit that undoes the changes introduced by the specified commit.
git revert abc123
-
git checkout -- <file>
Discard changes in a file, reverting it to the last committed version.
git checkout -- myfile.txt
-
git stash pop
Apply the most recent stash and remove it from the stash list.
git stash pop
History and Logs
-
git log --pretty=oneline
Show commit history in a compact, one-line format.
git log --pretty=oneline
-
git log --since="2024-01-01"
Show commits from a specific date.
git log --since="2024-01-01"
-
git show <commit>
Show detailed information about a specific commit.
git show abc123
Advanced Topics
-
git tag -a <tag-name> -m <message>
Create an annotated tag with a message.
git tag -a v1.0 -m "First stable release"
-
git tag -d <tag-name>
Delete a tag locally.
git tag -d v1.0
-
git tag <tag-name> <commit>
Create a lightweight tag at a specific commit.
git tag v1.1 abc123
-
git submodule update --remote
Update the submodule to the latest commit in the upstream repository.
git submodule update --remote
Git Aliases
-
git config --global alias.co checkout
Create an alias for thecheckout
command, making it shorter (git co
).
git config --global alias.co checkout
-
git config --global alias.st status
Create an alias for thestatus
command (git st
).
git config --global alias.st status
-
git config --global alias.br branch
Create an alias for thebranch
command (git br
).
git config --global alias.br branch
Security and Encryption
-
git config --global user.signingkey <key-id>
Set the GPG key to sign your commits.
git config --global user.signingkey 1234567890
-
git commit --gpg-sign
Sign a commit with GPG.
git commit --gpg-sign
-
git tag -s <tag-name> -m <message>
Create a signed tag using GPG.
git tag -s v1.0 -m "Signed tag for release v1.0"
Initialization and Setup
-
git init <directory>
Initialize a new Git repository in the specified directory.
git init my-project
-
git clone <repository-url>
Clone an existing remote repository to your local machine.
git clone https://github.com/user/repository.git
Committing Changes
-
git add --all
Add all changes (including file deletions) in the working directory to the staging area.
git add --all
-
git commit -m "message"
Commit changes with a commit message.
git commit -m "Initial commit"
-
git commit --amend --no-edit
Amend the last commit without changing the commit message.
git commit --amend --no-edit
-
git commit --amend -m "new message"
Amend the last commit and modify the commit message.
git commit --amend -m "Updated commit message"
Branching and Merging
-
git branch -a
List all branches, both local and remote.
git branch -a
-
git branch -r
List only remote branches.
git branch -r
-
git merge --no-ff <branch-name>
Merge the specified branch into the current branch without fast-forwarding.
git merge --no-ff feature-branch
-
git merge --ff-only <branch-name>
Ensure that the merge will only happen if a fast-forward is possible.
git merge --ff-only feature-branch
-
git merge <branch-name> --strategy=recursive -X theirs
Resolve merge conflicts by automatically choosing changes from the specified branch.
git merge feature-branch --strategy=recursive -X theirs
Collaboration and Remote Repositories
-
git push <remote> <branch-name>
Push a local branch to a remote repository.
git push origin feature-branch
-
git push --tags
Push all tags to the remote repository.
git push --tags
-
git pull --rebase <remote> <branch>
Fetch the latest changes from a remote branch and rebase your local changes on top of them.
git pull --rebase origin main
-
git fetch origin <branch-name>
Fetch changes from a specific remote branch.
git fetch origin feature-branch
-
git remote set-url origin <new-url>
Change the URL of a remote repository.
git remote set-url origin https://github.com/user/new-repository.git
-
git remote add <remote-name> <remote-url>
Add a new remote repository.
git remote add upstream https://github.com/another-user/repository.git
Rebasing and History
-
git rebase --interactive HEAD~n
Rebase the lastn
commits interactively.
git rebase --interactive HEAD~3
-
git rebase --abort
Abort a rebase operation and restore the repository to its previous state.
git rebase --abort
-
git rebase --continue
Continue the rebase process after resolving any conflicts.
git rebase --continue
-
git reflog
Show the history of theHEAD
reference, useful for recovering lost commits.
git reflog
-
git show <commit-hash>
Show the changes made in a specific commit.
git show abc123
-
git log --oneline
View the commit history in a simple, one-line format.
git log --oneline
Undoing Changes and Cleaning Up
-
git reset --hard <commit>
Reset the repository to a specific commit and discard all local changes.
git reset --hard abc123
-
git reset <file>
Unstage a file from the staging area.
git reset myfile.txt
-
git clean -n -d
Preview which untracked files and directories would be removed.
git clean -n -d
-
git stash clear
Remove all stashed changes.
git stash clear
-
git checkout <commit-hash>
Check out a specific commit to explore the project at that point in time.
git checkout abc123
Advanced Tagging and Submodules
-
git submodule add <repository-url> <path>
Add a submodule to your project.
git submodule add https://github.com/submodule/repository.git submodule-directory
-
git submodule update --init --recursive
Initialize and update submodules recursively.
git submodule update --init --recursive
-
git submodule foreach <command>
Run a Git command in each submodule.
git submodule foreach git pull origin main
Working with Remotes and Aliases
-
git remote rename <old-name> <new-name>
Rename a remote repository.
git remote rename origin upstream
-
git remote remove <remote-name>
Remove a remote repository.
git remote remove upstream
-
git config --global alias.cm commit
Create an alias for thecommit
command (e.g., usegit cm
instead ofgit commit
).
git config --global alias.cm commit
Security and Encryption
-
git config --global user.signingkey <key-id>
Configure a GPG key for commit signing.
git config --global user.signingkey ABC12345
-
git commit --gpg-sign
Sign a commit with your GPG key.
git commit --gpg-sign -m "Signed commit"
-
git tag -s <tag-name> -m <message>
Create a signed tag for your release.
git tag -s v1.0 -m "Signed v1.0 tag"
Conclusion
This guide covers the most commonly used Git commands to help you work effectively with Git. From setting up your repository to handling advanced workflows, these commands provide the tools you need to manage your codebase, collaborate with others, and ensure smooth development processes.
By mastering these Git commands, you will be able to work efficiently on individual projects and in collaborative team environments, ensuring that version control is never a bottleneck in your workflow.
Top comments (0)