DEV Community

Cover image for Git Cheat Sheet - 40+ Git Comands You Should Know
Shivam Singh
Shivam Singh

Posted on

Git Cheat Sheet - 40+ Git Comands You Should Know

Git is the free and open source distributed version control system.This cheat sheet features the most important and commonly used Git commands for easy reference.

Having trouble remembering git commands? Here's a cheat sheet with more than 40+ commands to make your life simpler.

GIT SETUP & INIT

1.Configuring user information

git config --global user.name "[firstname lastname]"
git config --global user.email "[valid-email]"
git config --global color.ui auto
Enter fullscreen mode Exit fullscreen mode

2.Initialize a local repository

git init [directory]
Enter fullscreen mode Exit fullscreen mode

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

3.Clone a remote repository

git clone [url]
Enter fullscreen mode Exit fullscreen mode

retrieve an entire repository from a hosted location via URL.

STAGE & SNAPSHOT

4.Show modified files in working directory

git status
Enter fullscreen mode Exit fullscreen mode

5. 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

6.Add only certain file to the staging area
With the asterisk in the command below, you can add all files starting with 'fil' in the staging area.

git add fil*
Enter fullscreen mode Exit fullscreen mode

7.Remove a file from the staging area

git reset [file]
Enter fullscreen mode Exit fullscreen mode

8.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

9.Display the changes between two commits

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

10.Commit your staged content

git commit -m “[descriptive 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

BRANCH & MERGE

11.Display list of your branch

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 12.Create a new branch create a new branch at the current commit
git branch [branch-name]
Enter fullscreen mode Exit fullscreen mode

13.Switch to a branch

git checkout [branch]
Enter fullscreen mode Exit fullscreen mode

14.Delete a branch

git branch -d [branch]
Enter fullscreen mode Exit fullscreen mode

15.Merge a branch

git merge [branch-name]
Enter fullscreen mode Exit fullscreen mode

16.Abort conflicting merge

git merge --abort
Enter fullscreen mode Exit fullscreen mode

INSPECT & COMPARE

Examining logs, diffs and object information

17.Display the commit history

git log
Enter fullscreen mode Exit fullscreen mode

show the commits on branchA that are not on branchB

git log branchB..branchA
Enter fullscreen mode Exit fullscreen mode

show the commits that changed file, even across renames

git log --follow [file]
Enter fullscreen mode Exit fullscreen mode

18. Object in Git in human-readable forma

git show [SHA]
Enter fullscreen mode Exit fullscreen mode

SHARE & UPDATE

Retrieving updates from another repository and updating local repos.

19.Add a remote repository

git remote add [remote name] [url]
Enter fullscreen mode Exit fullscreen mode

20.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

21.Remove a remote repository

git remote remove [remote name]
Enter fullscreen mode Exit fullscreen mode

22.Rename a remote repository

git remote rename [old name] [new name]
Enter fullscreen mode Exit fullscreen mode

23.Fetch changes from a remote repository

git fetch [remote name]
Enter fullscreen mode Exit fullscreen mode

24.Fetch changes from a particular branch

git fetch [remote name] [branch-name]
Enter fullscreen mode Exit fullscreen mode

25.Pull changes from a remote repository

git pull [remote name] [branch-name]
Enter fullscreen mode Exit fullscreen mode

26.Push changes to a remote repository

git push [remote name]
Enter fullscreen mode Exit fullscreen mode

27.Push changes to a particular branch

git push [remote name] [branch]
Enter fullscreen mode Exit fullscreen mode

TEMPORARY COMMITS

28.Stash changes

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

29.List stashes
list stack-order of stashed file changes

git stash list
Enter fullscreen mode Exit fullscreen mode

30.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)
31.Remove a stash

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

Discard the changes from top of stash stack

git stash drop
Enter fullscreen mode Exit fullscreen mode

32.Remove all stashes

git stash clear
Enter fullscreen mode Exit fullscreen mode

33.Apply and remove a stash

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

34.Display the changes in a stash

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

REWRITE HISTORY

Rewriting branches, updating commits and clearing history

35.Revert a commit

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

36.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

37.Apply any commits of current branch ahead of specified one

git rebase [branch]
Enter fullscreen mode Exit fullscreen mode

TRACKING PATH CHANGES

Versioning file removes and path changes

38.Delete the file from project and stage the removal for commit

git rm [file]
Enter fullscreen mode Exit fullscreen mode

39.Change an existing file path and stage the move

git mv [existing-path] [new-path]
Enter fullscreen mode Exit fullscreen mode

40.Show all commit logs with indication of any paths that moved

git log --stat -M
Enter fullscreen mode Exit fullscreen mode

IGNORING PATTERNS

Preventing unintentional staging or commiting of files.

41.Save a file with desired paterns as .gitignore with either direct string matches or wildcard globs.

logs/
*.notes
pattern*/
Enter fullscreen mode Exit fullscreen mode

42.system wide ignore patern for all local repositories

git config --global core.excludesfile [file]
Enter fullscreen mode Exit fullscreen mode

43.Cache your login credentials in Git

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

Conclusion

You may significantly increase your Git productivity by using these commands. I've created this cheat sheet so you don't have to memorise them all. You can print this page if you'd like or save it as a bookmark for later use.

Thanks for reading!!

Top comments (0)