Git Comands You Should Know

Git is a distributed version management system that is free and open source. The most significant and often used Git commands are included on this cheat sheet for quick access.

Not being able to recall git commands? A cheat sheet with more than 40 instructions is provided here to make your life easier.

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

2.Initialize a local repository

git init [directory]

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

3.Clone a remote repository

git clone [url]

retrieve an entire repository from a hosted location via URL.

STAGE & SNAPSHOT

4.Show modified files in working directory

git status

5. Add a file to the staging area

git add [file]

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

git add .

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*

7.Remove a file from the staging area

git reset [file]

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

9.Display the changes between two commits

git diff [commit id 01> <commit id 02]

10.Commit your staged content

git commit -m “[descriptive message]”

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

git commit -a -m "<message>"

# OR

git commit -am "<message>"

BRANCH & MERGE

11.Display list of your branch

git branch

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]

13.Switch to a branch

git checkout [branch]

14.Delete a branch

git branch -d [branch]

15.Merge a branch

git merge [branch-name]

16.Abort conflicting merge

git merge --abort

INSPECT & COMPARE

Examining logs, diffs and object information

17.Display the commit history

git log

show the commits on branchA that are not on branchB

git log branchB..branchA

show the commits that changed file, even across renames

git log --follow [file]

18. Object in Git in human-readable forma

git show [SHA]

SHARE & UPDATE

Retrieving updates from another repository and updating local repos.

19.Add a remote repository

git remote add [remote name] [url]

20.Display remote repositories

git remote

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

git remote -v

21.Remove a remote repository

git remote remove [remote name]

22.Rename a remote repository

git remote rename [old name] [new name]

23.Fetch changes from a remote repository

git fetch [remote name]

24.Fetch changes from a particular branch

git fetch [remote name] [branch-name]

25.Pull changes from a remote repository

git pull [remote name] [branch-name]

26.Push changes to a remote repository

git push [remote name]

27.Push changes to a particular branch

git push [remote name] [branch]

TEMPORARY COMMITS

28.Stash changes

git stash

You can also add a message to the stash.

git stash save "<message>"

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

git stash list

30.Apply a stash
Applying the stash will NOT remove it from the stash list.

git stash apply <stash id>

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>

Discard the changes from top of stash stack

git stash drop

32.Remove all stashes

git stash clear

33.Apply and remove a stash

git stash pop <stash id>

34.Display the changes in a stash

git stash show <stash id>

REWRITE HISTORY

Rewriting branches, updating commits and clearing history

35.Revert a commit

git revert <commit id>

36.Reset a commit

git reset <commit id>

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

git reset --hard <commit id>

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

git rebase [branch]

TRACKING PATH CHANGES

Versioning file removes and path changes

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

git rm [file]

39.Change an existing file path and stage the move

git mv [existing-path] [new-path]

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

git log --stat -M

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*/

42.system wide ignore patern for all local repositories

git config --global core.excludesfile [file]

43.Cache your login credentials in Git

git config --global credential.helper cache

Conclusion

Using these commands might greatly improve your Git productivity. You don’t need to memorise them all since I’ve made this cheat sheet. If you’d like, you can print this page or bookmark it for later use.

Get 3 course worth $129 for FREE

RECENT COURSE

COURSERA COURSE