Git Cheat Sheet with 40+ commands & concepts

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 

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

2. Clone a remote repository

git clone 

3. Add a file to the staging area

git add 

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

git add .

4. Commit changes

git commit -m ""

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

git commit -a -m ""

# or

git commit -am ""

5. Remove a file from the staging area

git reset 

6. Move or rename a file

git mv  

7. Remove a file from the repository

git rm 

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

git rm --cached 

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

Useful flags:

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

14. Create a branch

git branch 

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

git checkout -b 

15. Switch to a branch

git checkout 

16. Delete a branch

git branch -d 

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

git branch -D 

17. Merge a branch

git merge 

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 

19. Checkout a previous commit

git checkout id>

20. Revert a commit

git revert id>

21. Reset a commit

git reset id>

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

git reset --hard id>

22. Check out the status of the repository

git status

23. Display the commit history

git log

24. Display the changes to unstaged files

git diff

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

git diff --staged

25. Display the changes between two commits

git diff id 01> id 02>

26. Stash changes

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

git stash

You can also add a message to the stash.

git stash save ""

27. List stashes

git stash list

28. Apply a stash

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

git stash apply id>

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

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

git stash apply stash@{0}

29. Remove a stash

git stash drop id>

30. Remove all stashes

git stash clear

31. Apply and remove a stash

git stash pop id>

32. Display the changes in a stash

git stash show id>

33. Add a remote repository

git remote add  

34. Display remote repositories

git remote

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

git remote -v

35. Remove a remote repository

git remote remove 

36 Rename a remote repository

git remote rename  

37. Fetch changes from a remote repository

git fetch 

38. Fetch changes from a particular branch

git fetch  

39. Pull changes from a remote repository

git pull  

40. Push changes to a remote repository

git push 

41. Push changes to a particular branch

git push  

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.

Total
1
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
how-to-make-stopwatch-in-react

How to Make Stopwatch in React

Next Post
hello-world-with-react

Hello World with React

Related Posts