Git Cheat Sheet - 40+ Git Comands You Should Know
Tags: git, github, programming, command
Published: 2022-12-04 14:19:44

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
```
**2.Initialize a local repository**
```
git init [directory]
```
The <directory> 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
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!!