DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

A Comprehensive Guide to All Git Commands for Every Developer

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
    

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)