DEV Community

Cover image for Git Cheat Sheet – Git Commands You Should Know
Abdullah
Abdullah

Posted on

Git Cheat Sheet – Git Commands You Should Know

  1. What is a Distributed Version Control System?
  2. Git Commands You Should Know
    1. How to check your Git configuration
    2. How to setup your Git username
    3. How to setup your Git user email
    4. How to cache your login credentials in Git
    5. How to initialize a Git repo
    6. How to add a file to the staging area in Git
    7. How to add all files in the staging area in Git
    8. How to add only certain files to the staging area in Git
    9. How to check a repository’s status in Git
    10. How to commit changes in the editor in Git
    11. How to commit changes with a message in Git
    12. How to commit changes (and skip the staging area) in Git
    13. How to see your commit history in Git
    14. How to see your commit history including changes in Git
    15. How to see a specific commit in Git
    16. How to see log stats in Git
    17. How to see changes made before committing them using “diff” in Git
    18. How to see changes using “git add -p”
    19. How to remove tracked files from the current working tree in Git
    20. How to rename files in Git
    21. How to ignore files in Git
    22. How to revert unstaged changes in Git
    23. How to revert staged changes in Git
    24. How to amend the most recent commit in Git
    25. How to rollback the last commit in Git
    26. How to rollback an old commit in Git
    27. How to create a new branch in Git
    28. How to switch to a newly created branch in Git
    29. How to list branches in Git
    30. How to create a branch in Git and switch to it immediately
    31. How to delete a branch in Git
    32. How to merge two branches in Git
    33. How to show the commit log as a graph in Git
    34. How to show the commit log as a graph of all branches in Git
    35. How to abort a conflicting merge in Git
    36. How to add a remote repository in Git
    37. How to see remote URLs in Git
    38. How to get more info about a remote repo in Git
    39. How to push changes to a remote repo in Git
    40. How to pull changes from a remote repo in Git
    41. How to check remote branches that Git is tracking
    42. How to fetch remote repo changes in Git
    43. How to check the current commits log of a remote repo in Git
    44. How to merge a remote repo with your local repo in Git
    45. How to get the contents of remote branches in Git without automatically merging
    46. How to push a new branch to a remote repo in Git
    47. How to remove a remote branch in Git
    48. How to use Git rebase
    49. How to run rebase interactively in Git
    50. How to force a push request in Git
  3. Conclusion

What is a Distributed Version Control System?

A Distributed Version Control System (DVCS) is a type of version control where the complete codebase—including its full history—is mirrored on every developer's computer. This setup allows for more flexible workflows, including offline work, easier branching, and faster operations compared to centralized version control systems like SVN.

Git Commands You Should Know

Understanding and mastering Git commands is essential for effective version control. Here's a comprehensive guide to help you get started and become proficient with Git.

How to check your Git configuration

Before diving into Git commands, it's crucial to check your Git configuration to ensure everything is set up correctly.

git config --list
Enter fullscreen mode Exit fullscreen mode

How to setup your Git username

Your Git username is used to label your commits with your identity. Set it up using:

git config --global user.name "Your Name"
Enter fullscreen mode Exit fullscreen mode

How to setup your Git user email

Similar to the username, your email address is attached to your commits.

git config --global user.email "you@example.com"
Enter fullscreen mode Exit fullscreen mode

How to cache your login credentials in Git

To avoid entering your credentials repeatedly, you can cache them.

git config --global credential.helper cache
Enter fullscreen mode Exit fullscreen mode

How to initialize a Git repo

Start tracking your project with Git by initializing a repository.

git init
Enter fullscreen mode Exit fullscreen mode

How to add a file to the staging area in Git

Prepare your changes for a commit by adding them to the staging area.

git add <file>
Enter fullscreen mode Exit fullscreen mode

How to add all files in the staging area in Git

Add all your changes to the staging area at once.

git add .
Enter fullscreen mode Exit fullscreen mode

How to add only certain files to the staging area in Git

Sometimes, you only want to stage specific files.

git add <file1> <file2>
Enter fullscreen mode Exit fullscreen mode

How to check a repository’s status in Git

Check the status of your working directory and staging area.

git status
Enter fullscreen mode Exit fullscreen mode

How to commit changes in the editor in Git

Commit your staged changes with an editor for the commit message.

git commit
Enter fullscreen mode Exit fullscreen mode

How to commit changes with a message in Git

Commit your changes directly from the command line with a message.

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

How to commit changes (and skip the staging area) in Git

You can commit changes directly without staging them first.

git commit -a -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

How to see your commit history in Git

Review the history of your commits.

git log
Enter fullscreen mode Exit fullscreen mode

How to see your commit history including changes in Git

View commit history along with the changes introduced.

git log -p
Enter fullscreen mode Exit fullscreen mode

How to see a specific commit in Git

Inspect a particular commit in detail.

git show <commit-hash>
Enter fullscreen mode Exit fullscreen mode

How to see log stats in Git

Get a concise summary of your commit history.

git log --stat
Enter fullscreen mode Exit fullscreen mode

How to see changes made before committing them using “diff” in Git

Compare changes in your working directory.

git diff
Enter fullscreen mode Exit fullscreen mode

How to see changes using “git add -p”

Interactively select changes to stage.

git add -p
Enter fullscreen mode Exit fullscreen mode

How to remove tracked files from the current working tree in Git

Remove files from your working directory and staging area.

git rm <file>
Enter fullscreen mode Exit fullscreen mode

How to rename files in Git

Rename and move files within your repository.

git mv <oldfile> <newfile>
Enter fullscreen mode Exit fullscreen mode

How to ignore files in Git

Use a .gitignore file to exclude files from tracking.

# .gitignore
node_modules/
*.log
Enter fullscreen mode Exit fullscreen mode

How to revert unstaged changes in Git

Discard changes in your working directory.

git checkout -- <file>
Enter fullscreen mode Exit fullscreen mode

How to revert staged changes in Git

Unstage changes while retaining them in your working directory.

git reset <file>
Enter fullscreen mode Exit fullscreen mode

How to amend the most recent commit in Git

Modify the last commit with new changes.

git commit --amend
Enter fullscreen mode Exit fullscreen mode

How to rollback the last commit in Git

Undo the last commit while keeping changes in the working directory.

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

How to rollback an old commit in Git

Revert to a specific commit.

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

How to create a new branch in Git

Branching allows you to work on different features or fixes.

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

How to switch to a newly created branch in Git

Move to your new branch.

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

How to list branches in Git

See all branches in your repository.

git branch
Enter fullscreen mode Exit fullscreen mode

How to create a branch in Git and switch to it immediately

Create and switch to a new branch in one command.

git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode

How to delete a branch in Git

Remove branches that you no longer need.

git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

How to merge two branches in Git

Combine changes from different branches.

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

How to show the commit log as a graph in Git

Visualize your commit history as a graph.

git log --graph
Enter fullscreen mode Exit fullscreen mode

How to show the commit log as a graph of all branches in Git

See the commit history graph for all branches.

git log --graph --all
Enter fullscreen mode Exit fullscreen mode

How to abort a conflicting merge in Git

If a merge goes wrong, you can abort it.

git merge --abort
Enter fullscreen mode Exit fullscreen mode

How to add a remote repository in Git

Link your local repository to a remote one.

git remote add origin <url>
Enter fullscreen mode Exit fullscreen mode

How to see remote URLs in Git

List the URLs of your remote repositories.

git remote -v
Enter fullscreen mode Exit fullscreen mode

How to get more info about a remote repo in Git

Detailed information about your remotes.

git remote show origin
Enter fullscreen mode Exit fullscreen mode

How to push changes to a remote repo in Git

Upload your local changes to a remote repository.

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

How to pull changes from a remote repo in Git

Fetch and merge changes from a remote repository.

git pull
Enter fullscreen mode Exit fullscreen mode

How to check remote branches that Git is tracking

List the remote branches your local repository is tracking.

git branch -r
Enter fullscreen mode Exit fullscreen mode

How to fetch remote repo changes in Git

Download changes from a remote repository without merging.

git fetch
Enter fullscreen mode Exit fullscreen mode

How to check the current commits log of a remote repo in Git

View the commit log for a remote repository.

git log origin/<branch-name>
Enter fullscreen mode Exit fullscreen mode

How to merge a remote repo with your local repo in Git

Combine changes from a remote repository into your local one.

git merge origin/<branch-name>
Enter fullscreen mode Exit fullscreen mode

How to get the contents of remote branches in Git without automatically merging

Fetch remote branches without merging them.

git fetch origin
Enter fullscreen mode Exit fullscreen mode

How to push a new branch to a remote repo in Git

Share your new branch with the remote repository.

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

How to remove a remote branch in Git

Delete a branch from the remote repository.

git push origin --delete <branch-name>
Enter fullscreen mode Exit fullscreen mode

How to use Git rebase

Rebase your current branch onto another branch.

git rebase <branch-name>
Enter fullscreen mode Exit fullscreen mode

How to run rebase interactively in Git

Use interactive rebase for a more controlled process.

git rebase -i <commit-hash>
Enter fullscreen mode Exit fullscreen mode

How to force a push request in Git

Forcefully push changes to a remote repository.

git push --force
Enter fullscreen mode Exit fullscreen mode

Conclusion

Mastering Git commands empowers you to manage your codebase efficiently, collaborate with others, and maintain a robust history of your project's development. Practice these commands and explore more advanced features as you grow more comfortable with Git.

Top comments (0)