DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Mastering Git: A Comprehensive List of All Git Commands for Developers

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

  1. git init Initialize a new Git repository in your project directory.
   git init
Enter fullscreen mode Exit fullscreen mode
  1. git clone <repository-url> Clone a remote repository to your local machine.
   git clone https://github.com/user/repository.git
Enter fullscreen mode Exit fullscreen mode
  1. git status Show the current state of the working directory and staging area (i.e., which files are staged, unstaged, or untracked).
   git status
Enter fullscreen mode Exit fullscreen mode
  1. git add <file> Add changes in a specific file to the staging area.
   git add index.html
Enter fullscreen mode Exit fullscreen mode
  1. git commit -m "message" Commit staged changes to the repository with a descriptive message.
   git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode
  1. git log View the commit history of the repository.
   git log
Enter fullscreen mode Exit fullscreen mode
  1. git diff Show the differences between the working directory and the index (staging area).
   git diff
Enter fullscreen mode Exit fullscreen mode

Branching and Merging Commands

  1. git branch List all branches in your repository.
   git branch
Enter fullscreen mode Exit fullscreen mode
  1. git branch <branch-name> Create a new branch.
   git branch feature-xyz
Enter fullscreen mode Exit fullscreen mode
  1. git checkout <branch-name>

    Switch to the specified branch.

    git checkout feature-xyz
    
  2. git merge <branch-name>

    Merge the specified branch into the current branch.

    git merge feature-xyz
    
  3. git branch -d <branch-name>

    Delete a local branch after it has been merged.

    git branch -d feature-xyz
    
  4. git merge --abort

    Abort the merge process if conflicts arise and revert to the pre-merge state.

    git merge --abort
    

Remote Repositories and Collaboration

  1. git remote add <name> <url>

    Add a new remote repository URL.

    git remote add origin https://github.com/user/repository.git
    
  2. git fetch

    Fetch all branches from the remote repository, without merging them into your local branches.

    git fetch origin
    
  3. git pull

    Fetch and merge changes from the remote repository into the current branch.

    git pull origin main
    
  4. git push

    Push your local commits to a remote repository.

    git push origin main
    
  5. 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
    
  6. git remote -v

    List the remote repositories associated with the current repository.

    git remote -v
    

Undoing Changes

  1. git reset <file>

    Unstage a file, keeping the changes in the working directory.

    git reset index.html
    
  2. git reset --hard

    Reset the repository to a specific commit and discard changes in the working directory.

    git reset --hard HEAD
    
  3. git revert <commit-hash>

    Create a new commit that undoes the changes made by a specific commit.

    git revert abc123
    
  4. git checkout -- <file>

    Discard changes in the working directory for a specific file, reverting it to the last committed state.

    git checkout -- index.html
    
  5. git stash

    Temporarily store changes that are not yet ready to be committed.

    git stash
    
  6. git stash pop

    Retrieve and apply the most recent stash.

    git stash pop
    

Advanced Git Commands

  1. git rebase <branch>

    Rebase your current branch onto the given branch, integrating changes from the upstream branch.

    git rebase main
    
  2. git rebase --interactive

    Launch an interactive rebase to edit, reorder, or squash commits.

    git rebase -i HEAD~5
    
  3. git cherry-pick <commit-hash>

    Apply the changes introduced by an existing commit to the current branch.

    git cherry-pick abc123
    
  4. git bisect

    Use binary search to find the commit that introduced a bug by checking out various commits between two points.

    git bisect start
    
  5. git reflog

    Show the history of changes to the HEAD reference, which helps recover lost commits.

    git reflog
    
  6. 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

  1. git config --global user.name "Your Name"

    Set your username globally for Git commits.

    git config --global user.name "Your Name"
    
  2. git config --global user.email "youremail@example.com"

    Set your email globally for Git commits.

    git config --global user.email "youremail@example.com"
    
  3. git config --list

    List all the configuration settings for Git.

    git config --list
    
  4. 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

  1. git log

    View the commit history with a detailed log of each commit.

    git log
    
  2. git log --oneline

    View the commit history in a simplified format with one line per commit.

    git log --oneline
    
  3. git log --graph

    View the commit history in a graphical representation.

    git log --graph
    
  4. git show <commit-hash>

    Display detailed information about a specific commit.

    git show abc123
    

Tagging in Git

  1. git tag <tag-name>

    Create a new tag at the current commit.

    git tag v1.0
    
  2. git tag -a <tag-name> -m "message"

    Create an annotated tag with a message.

    git tag -a v1.0 -m "Version 1.0 release"
    
  3. git push origin <tag-name>

    Push a specific tag to a remote repository.

    git push origin v1.0
    
  4. git push --tags

    Push all tags to a remote repository.

    git push --tags
    

Git Workflow and Management

  1. git rm <file>

    Remove a file from both the working directory and the staging area.

    git rm index.html
    
  2. git mv <old-filename> <new-filename>

    Rename or move a file in your Git repository.

    git mv oldfile.txt newfile.txt
    
    

Configuration and Setup

  1. git config --global core.autocrlf true|false Set Git's behavior with line endings. When set to true, it automatically converts line endings to the appropriate format for the operating system.
   git config --global core.autocrlf true
Enter fullscreen mode Exit fullscreen mode
  1. git config --global core.fileMode false Disable file permission changes from being tracked by Git.
   git config --global core.fileMode false
Enter fullscreen mode Exit fullscreen mode
  1. git config --global color.ui auto Enable colorized output in Git commands for better readability.
   git config --global color.ui auto
Enter fullscreen mode Exit fullscreen mode
  1. git config --global merge.tool <tool> Set the default merge tool for resolving conflicts.
   git config --global merge.tool vimdiff
Enter fullscreen mode Exit fullscreen mode

Stashing and Cleaning

  1. git stash list List all stashes stored in your repository.
   git stash list
Enter fullscreen mode Exit fullscreen mode
  1. git stash show Show changes saved in the latest stash.
   git stash show
Enter fullscreen mode Exit fullscreen mode
  1. git stash apply Apply a stash without removing it from the stash list.
   git stash apply
Enter fullscreen mode Exit fullscreen mode
  1. git clean -n Preview which files would be removed by git clean.
   git clean -n
Enter fullscreen mode Exit fullscreen mode
  1. git clean -f Clean untracked files from your working directory.
   git clean -f
Enter fullscreen mode Exit fullscreen mode
  1. git clean -fd

    Remove both untracked files and directories.

    git clean -fd
    

Git Log and History

  1. git log --oneline --graph --decorate

    View a simplified commit history with a graphical representation and decorations (branch names, tags).

    git log --oneline --graph --decorate
    
  2. git log --since="2 weeks ago"

    Show commits made in the last two weeks.

    git log --since="2 weeks ago"
    
  3. git log --author="Author Name"

    View commits made by a specific author.

    git log --author="John Doe"
    
  4. git log --grep="fix bug"

    Search commit messages for a specific pattern.

    git log --grep="fix bug"
    
  5. git log --stat

    Show statistics for each commit (added/deleted lines).

    git log --stat
    
  6. git blame <file>

    View line-by-line commit history for a file, showing which commit last modified each line.

    git blame index.html
    
  7. git show-branch

    Display the commit history for all branches.

    git show-branch
    

Rebasing and Interactive Rebasing

  1. git rebase --continue

    After resolving a conflict during a rebase, continue the process.

    git rebase --continue
    
  2. git rebase --skip

    Skip the current patch in the rebase process if it causes conflicts.

    git rebase --skip
    
  3. git rebase --abort

    Abort the rebase process and restore the repository to its original state.

    git rebase --abort
    
  4. git rebase -i HEAD~3

    Start an interactive rebase for the last three commits.

    git rebase -i HEAD~3
    

Cherry-pick and Squashing

  1. 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
    
  2. 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
    
  3. git commit --amend

    Modify the most recent commit, including changing the commit message or adding new changes.

    git commit --amend
    

Advanced Tagging

  1. git tag -d <tag-name>

    Delete a tag from the local repository.

    git tag -d v1.0
    
  2. git push origin --delete <tag-name>

    Delete a tag from the remote repository.

    git push origin --delete v1.0
    

Managing Remotes

  1. git remote remove <remote-name>

    Remove a remote repository from your configuration.

    git remote remove origin
    
  2. git remote rename <old-name> <new-name>

    Rename a remote repository.

    git remote rename origin upstream
    
  3. git remote show <remote-name>

    Show detailed information about a specific remote repository.

    git remote show origin
    

Repository Maintenance

  1. git gc

    Run the garbage collection process to optimize the repository by cleaning up unnecessary files and optimizing the local database.

    git gc
    
  2. git fsck

    Check the integrity of the Git repository and verify the connectivity of all objects.

    git fsck
    
  3. git reflog expire --expire=now --all

    Remove all expired reflog entries from the repository.

    git reflog expire --expire=now --all
    

Remote Repositories and Forking

  1. 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
    
  2. 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
    
  3. git remote prune <remote-name>

    Remove references to remote branches that no longer exist.

    git remote prune origin
    

Other Git Commands

  1. 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
    
  2. git submodule update --init

    Initialize, fetch, and checkout the submodule for a repository.

    git submodule update --init
    
  3. git submodule update --remote

    Update all submodules to their latest commit.

    git submodule update --remote
    
  4. git bisect start

    Start the bisect process to help locate the commit that introduced a bug.

    git bisect start
    
  5. git bisect bad

    Mark the current commit as bad when using git bisect.

    git bisect bad
    
  6. 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
Enter fullscreen mode Exit fullscreen mode
  • git commit --no-edit Commit changes without modifying the commit message (useful after a merge).
  git commit --no-edit
Enter fullscreen mode Exit fullscreen mode

Branching and Merging

  • git branch -m <old-branch-name> <new-branch-name> Rename a branch.
  git branch -m old-branch new-branch
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • git merge --abort Abort a merge if conflicts arise or if you want to cancel the operation.
  git merge --abort
Enter fullscreen mode Exit fullscreen mode
  • git rebase -i Start an interactive rebase, allowing you to edit commit messages, squash commits, or reorder them.
  git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode
  • git rebase --onto <new-base> <upstream> <branch> Rebase a branch onto a different base.
  git rebase --onto new-base upstream branch
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • git pull --rebase Fetch the latest changes from the remote branch and apply your local commits on top.
  git pull --rebase
Enter fullscreen mode Exit fullscreen mode
  • git fetch --all Fetch updates from all remote repositories.
  git fetch --all
Enter fullscreen mode Exit fullscreen mode

Undoing Changes

  • git reset --soft <commit> Move HEAD to the specified commit and keep changes in the staging area.
  git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • git reset --mixed <commit> Move HEAD to the specified commit, keeping changes in the working directory but unstaged.
  git reset --mixed HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • git reset --hard <commit> Reset to the specified commit, discarding all changes.
  git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • git revert <commit> Create a new commit that undoes the changes introduced by the specified commit.
  git revert abc123
Enter fullscreen mode Exit fullscreen mode
  • git checkout -- <file> Discard changes in a file, reverting it to the last committed version.
  git checkout -- myfile.txt
Enter fullscreen mode Exit fullscreen mode
  • git stash pop Apply the most recent stash and remove it from the stash list.
  git stash pop
Enter fullscreen mode Exit fullscreen mode

History and Logs

  • git log --pretty=oneline Show commit history in a compact, one-line format.
  git log --pretty=oneline
Enter fullscreen mode Exit fullscreen mode
  • git log --since="2024-01-01" Show commits from a specific date.
  git log --since="2024-01-01"
Enter fullscreen mode Exit fullscreen mode
  • git show <commit> Show detailed information about a specific commit.
  git show abc123
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode
  • git tag -d <tag-name> Delete a tag locally.
  git tag -d v1.0
Enter fullscreen mode Exit fullscreen mode
  • git tag <tag-name> <commit> Create a lightweight tag at a specific commit.
  git tag v1.1 abc123
Enter fullscreen mode Exit fullscreen mode
  • git submodule update --remote Update the submodule to the latest commit in the upstream repository.
  git submodule update --remote
Enter fullscreen mode Exit fullscreen mode

Git Aliases

  • git config --global alias.co checkout Create an alias for the checkout command, making it shorter (git co).
  git config --global alias.co checkout
Enter fullscreen mode Exit fullscreen mode
  • git config --global alias.st status Create an alias for the status command (git st).
  git config --global alias.st status
Enter fullscreen mode Exit fullscreen mode
  • git config --global alias.br branch Create an alias for the branch command (git br).
  git config --global alias.br branch
Enter fullscreen mode Exit fullscreen mode

Security and Encryption

  • git config --global user.signingkey <key-id> Set the GPG key to sign your commits.
  git config --global user.signingkey 1234567890
Enter fullscreen mode Exit fullscreen mode
  • git commit --gpg-sign Sign a commit with GPG.
  git commit --gpg-sign
Enter fullscreen mode Exit fullscreen mode
  • 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"
Enter fullscreen mode Exit fullscreen mode

Initialization and Setup

  1. git init <directory> Initialize a new Git repository in the specified directory.
   git init my-project
Enter fullscreen mode Exit fullscreen mode
  1. git clone <repository-url> Clone an existing remote repository to your local machine.
   git clone https://github.com/user/repository.git
Enter fullscreen mode Exit fullscreen mode

Committing Changes

  1. git add --all Add all changes (including file deletions) in the working directory to the staging area.
   git add --all
Enter fullscreen mode Exit fullscreen mode
  1. git commit -m "message" Commit changes with a commit message.
   git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode
  1. git commit --amend --no-edit Amend the last commit without changing the commit message.
   git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode
  1. git commit --amend -m "new message" Amend the last commit and modify the commit message.
   git commit --amend -m "Updated commit message"
Enter fullscreen mode Exit fullscreen mode

Branching and Merging

  1. git branch -a List all branches, both local and remote.
   git branch -a
Enter fullscreen mode Exit fullscreen mode
  1. git branch -r List only remote branches.
   git branch -r
Enter fullscreen mode Exit fullscreen mode
  1. git merge --no-ff <branch-name> Merge the specified branch into the current branch without fast-forwarding.
   git merge --no-ff feature-branch
Enter fullscreen mode Exit fullscreen mode
  1. 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
    
  2. 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

  1. git push <remote> <branch-name>

    Push a local branch to a remote repository.

    git push origin feature-branch
    
  2. git push --tags

    Push all tags to the remote repository.

    git push --tags
    
  3. 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
    
  4. git fetch origin <branch-name>

    Fetch changes from a specific remote branch.

    git fetch origin feature-branch
    
  5. 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
    
  6. 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

  1. git rebase --interactive HEAD~n

    Rebase the last n commits interactively.

    git rebase --interactive HEAD~3
    
  2. git rebase --abort

    Abort a rebase operation and restore the repository to its previous state.

    git rebase --abort
    
  3. git rebase --continue

    Continue the rebase process after resolving any conflicts.

    git rebase --continue
    
  4. git reflog

    Show the history of the HEAD reference, useful for recovering lost commits.

    git reflog
    
  5. git show <commit-hash>

    Show the changes made in a specific commit.

    git show abc123
    
  6. git log --oneline

    View the commit history in a simple, one-line format.

    git log --oneline
    

Undoing Changes and Cleaning Up

  1. git reset --hard <commit>

    Reset the repository to a specific commit and discard all local changes.

    git reset --hard abc123
    
  2. git reset <file>

    Unstage a file from the staging area.

    git reset myfile.txt
    
  3. git clean -n -d

    Preview which untracked files and directories would be removed.

    git clean -n -d
    
  4. git stash clear

    Remove all stashed changes.

    git stash clear
    
  5. 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

  1. git submodule add <repository-url> <path>

    Add a submodule to your project.

    git submodule add https://github.com/submodule/repository.git submodule-directory
    
  2. git submodule update --init --recursive

    Initialize and update submodules recursively.

    git submodule update --init --recursive
    
  3. git submodule foreach <command>

    Run a Git command in each submodule.

    git submodule foreach git pull origin main
    

Working with Remotes and Aliases

  1. git remote rename <old-name> <new-name>

    Rename a remote repository.

    git remote rename origin upstream
    
  2. git remote remove <remote-name>

    Remove a remote repository.

    git remote remove upstream
    
  3. git config --global alias.cm commit

    Create an alias for the commit command (e.g., use git cm instead of git commit).

    git config --global alias.cm commit
    

Security and Encryption

  1. git config --global user.signingkey <key-id>

    Configure a GPG key for commit signing.

    git config --global user.signingkey ABC12345
    
  2. git commit --gpg-sign

    Sign a commit with your GPG key.

    git commit --gpg-sign -m "Signed commit"
    
  3. 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)