DEV Community

Cover image for Git Cheat Sheet with 40+ commands & concepts
Tapajyoti Bose
Tapajyoti Bose

Posted on

Git Cheat Sheet with 40+ commands & concepts

Tired of memorizing git commands? Here is a cheat sheet with 40+ commands to simplify your life.

1. Initialize a local repository

git init <directory>
Enter fullscreen mode Exit fullscreen mode

The <directory> is optional. If you don't specify it, the current directory will be used.

2. Clone a remote repository

git clone <url>
Enter fullscreen mode Exit fullscreen mode

3. Add a file to the staging area

git add <file>
Enter fullscreen mode Exit fullscreen mode

To add all files in the current directory, use . in place of <file>.

git add .
Enter fullscreen mode Exit fullscreen mode

4. Commit changes

git commit -m "<message>"
Enter fullscreen mode Exit fullscreen mode

If you want to add all changes made to tracked files & commit

git commit -a -m "<message>"

# or

git commit -am "<message>"
Enter fullscreen mode Exit fullscreen mode

5. Remove a file from the staging area

git reset <file>
Enter fullscreen mode Exit fullscreen mode

6. Move or rename a file

git mv <current path> <new path>
Enter fullscreen mode Exit fullscreen mode

7. Remove a file from the repository

git rm <file>
Enter fullscreen mode Exit fullscreen mode

You can also remove it from staging area only using --cached flag

git rm --cached <file>
Enter fullscreen mode Exit fullscreen mode

Basic Git Concepts

  1. Default branch name: main
  2. Default remote name: origin
  3. Current branch reference: HEAD
  4. Parent of HEAD: HEAD^ or HEAD~1
  5. Grandparent of HEAD: HEAD^^ or HEAD~2

13. Display branches

git branch
Enter fullscreen mode Exit fullscreen mode

Useful flags:

  • -a: Display all branches (local & remote)
  • -r: Display remote branches
  • -v: Display branches with last commit

14. Create a branch

git branch <branch>
Enter fullscreen mode Exit fullscreen mode

You can create a branch and switch to it using the checkout command.

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

15. Switch to a branch

git checkout <branch>
Enter fullscreen mode Exit fullscreen mode

16. Delete a branch

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

You can also force delete a branch using the -D flag.

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

17. Merge a branch

git merge <branch to merge into HEAD>
Enter fullscreen mode Exit fullscreen mode

Useful flags:

  • --no-ff: Create a merge commit even if the merge resolves as a fast-forward
  • --squash: Squash all commits from the specified branch into a single commit

Fast forward Merge

Fast-forward-merge

Non-Fast forward Merge

No-fast-forward-merge

It is suggested to not use the --squash flag as it will squash all commits into a single commit, leading to a messy commit history.

18. Rebase a branch

Rebasing is the process of moving or combining a sequence of commits to a new base commit

Rebase

git rebase <branch to rebase from>
Enter fullscreen mode Exit fullscreen mode

19. Checkout a previous commit

git checkout <commit id>
Enter fullscreen mode Exit fullscreen mode

20. Revert a commit

git revert <commit id>
Enter fullscreen mode Exit fullscreen mode

21. Reset a commit

git reset <commit id>
Enter fullscreen mode Exit fullscreen mode

You can also add the --hard flag to delete all changes, but use it with caution.

git reset --hard <commit id>
Enter fullscreen mode Exit fullscreen mode

22. Check out the status of the repository

git status
Enter fullscreen mode Exit fullscreen mode

23. Display the commit history

git log
Enter fullscreen mode Exit fullscreen mode

24. Display the changes to unstaged files

git diff
Enter fullscreen mode Exit fullscreen mode

You can also use the --staged flag to display the changes to staged files.

git diff --staged
Enter fullscreen mode Exit fullscreen mode

25. Display the changes between two commits

git diff <commit id 01> <commit id 02>
Enter fullscreen mode Exit fullscreen mode

26. Stash changes

The stash allows you to temporarily store changes without committing them.

git stash
Enter fullscreen mode Exit fullscreen mode

You can also add a message to the stash.

git stash save "<message>"
Enter fullscreen mode Exit fullscreen mode

27. List stashes

git stash list
Enter fullscreen mode Exit fullscreen mode

28. Apply a stash

Applying the stash will NOT remove it from the stash list.

git stash apply <stash id>
Enter fullscreen mode Exit fullscreen mode

If you do not specify the <stash id>, the latest stash will be applied (Valid for all similar stash commands)

You can also use the format stash@{<index>} to apply a stash (Valid for all similar stash commands)

git stash apply stash@{0}
Enter fullscreen mode Exit fullscreen mode

29. Remove a stash

git stash drop <stash id>
Enter fullscreen mode Exit fullscreen mode

30. Remove all stashes

git stash clear
Enter fullscreen mode Exit fullscreen mode

31. Apply and remove a stash

git stash pop <stash id>
Enter fullscreen mode Exit fullscreen mode

32. Display the changes in a stash

git stash show <stash id>
Enter fullscreen mode Exit fullscreen mode

33. Add a remote repository

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

34. Display remote repositories

git remote
Enter fullscreen mode Exit fullscreen mode

Add a -v flag to display the URLs of the remote repositories.

git remote -v
Enter fullscreen mode Exit fullscreen mode

35. Remove a remote repository

git remote remove <remote name>
Enter fullscreen mode Exit fullscreen mode

36 Rename a remote repository

git remote rename <old name> <new name>
Enter fullscreen mode Exit fullscreen mode

37. Fetch changes from a remote repository

git fetch <remote name>
Enter fullscreen mode Exit fullscreen mode

38. Fetch changes from a particular branch

git fetch <remote name> <branch>
Enter fullscreen mode Exit fullscreen mode

39. Pull changes from a remote repository

git pull <remote name> <branch>
Enter fullscreen mode Exit fullscreen mode

40. Push changes to a remote repository

git push <remote name>
Enter fullscreen mode Exit fullscreen mode

41. Push changes to a particular branch

git push <remote name> <branch>
Enter fullscreen mode Exit fullscreen mode

That's all folks! 🎉

Thanks for reading

Need a Top Rated Front-End Development Freelancer to chop away your development woes? Contact me on Upwork

Want to see what I am working on? Check out my Personal Website and GitHub

Want to connect? Reach out to me on LinkedIn

I am a Digital Nomad and occasionally travel. Follow me on Instagram to check out what I am up to.

Follow my blogs for bi-weekly new tidbits on Dev

FAQ

These are a few commonly asked questions I get. So, I hope this FAQ section solves your issues.

  1. I am a beginner, how should I learn Front-End Web Dev?
    Look into the following articles:

    1. Front End Development Roadmap
    2. Front End Project Ideas
  2. Would you mentor me?

    Sorry, I am already under a lot of workload and would not have the time to mentor anyone.

Top comments (21)

Collapse
 
jgurtz profile image
Jason Gurtz

Couple cool things missed:

git branch -vv               # additionally shows upstream,
                             # tracked branch details

git diff branch1..branch2    # diff between two branches
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kingme profile image
KingMe 🤴🏾

Git diff, thanks 😉

Collapse
 
channaveer profile image
Channaveer Hakari • Edited

If you could add tagging & cherrypick then it would be great addon to the list

Collapse
 
ruppysuppy profile image
Tapajyoti Bose

Yeah, I missed those 😅

Collapse
 
chiragagg5k profile image
Chirag Aggarwal

Definitely a good list, but I would suggest everyone to make their own versions. Trust me it’s lot better feeling to read the documentation you are completely aware of in future

Collapse
 
madza profile image
Madza

Valuable list 👍💯✨

Collapse
 
ruppysuppy profile image
Tapajyoti Bose

Thanks! Means a lot coming from you!

Collapse
 
javlonfattoev profile image
Javlon Fattoev

cool

Collapse
 
idktech profile image
Ivaylo

Great article! Huge help.

Collapse
 
gemasaputera profile image
Gema Saputera

nice post

Collapse
 
koleajeolayinka profile image
KOLEAJEOLAYINKA

The information you shared is very helpful

Collapse
 
kalifasenou profile image
Kalifa SENOU

Thanks you, we need it !

Collapse
 
kawsar19 profile image
kawsar ahmed

cool list

Collapse
 
jamessimel profile image
Simel James

Woooah💯🎉💕 I whole course on git
Thanks @ruppysuppy

Collapse
 
laurianoelmiroduarte profile image
Lauriano Elmiro Duarte

very good

Collapse
 
wisdomudo profile image
WISDOMUDO

Cool list

Collapse
 
anubhavitis profile image
Anubhav Singhal

Great list buddy. Saving it for future reference.

Collapse
 
drsturla profile image
sturla ardani

Thanks, great post!

Collapse
 
rutamhere profile image
Rutam Prita Mishra

This was really insightful. Kudos!