Rashad Mirza logo
Git Cheat Sheet

Git Cheat Sheet

Rashad Mirza Nov 3, 2022
I found this Git Cheat Sheet on my google docs.

I'll share it with you without modifying anything.

Note: I wrote it approximately 4-5 years ago for myself and it's not a perfect cheat sheet.

Terminal
Download http://iterm2.com
$ brew install zsh zsh-completions
$ chsh -s /bin/zsh
$ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Config
$ nano $HOME/.gitconfig (git global config file)
$ cat .git/config (local git file see user name, email etc)
$ git config --global user.name “Rashad M.”
$ git config --global user.email “rashad@rashadmirza.com”
$ git commit --amend --author=”Rashad ” (Change author on last commit)

$ git config --list
View the commit info (.git/object/folder_name/1st 2 letter)
$ git cat-file -p f5bd

Change remote URL
$ git remote set-url origin https://github.com/rashad404/example.git

Push an existing repository from the command line
$ git remote add origin https://github.com/rashad404/example.git
$ git branch -M main
$ git push -u origin main

Logs
$ git log

View staging area
$ git ls-files -s

View changed files in last commit
$ git log --name-status HEAD^..HEAD

List commit changed files
$ git diff-tree --no-commit-id --name-only -r 46ad46897371742f2b5c6336f61e9a012d4b6146

List last commit changed files
$ git diff-tree --no-commit-id --name-only -r 891fa1ecadba6c2450e012f6645437c559557546

Unstage file
$ git rm --cached file2.txt

Download Github desktop

VIM Editor:
Press “i” for insert mode and type
Esc to exit from Insert mode.
:wq to save changes and exit

See where HEAD points
$ cat .git/HEAD

See where master branch points
$ refs/heads/master

See parent
$ git cat-file -p 78e23ccd

See tree
$ git cat-file -t 78e23ccd

Deteached HEAD -> When HEAD points directly to the commit

Branch
$ git branch (List)
$ git branch (Create new branch)
$ git checkout (Checkout specific branch)
$ git checkout -b (create and enter)
$ git branch -d (Delete)
$ git branch -D (Force delete)
$ git push origin : (Delete remote branch)
$ git branch -m (Rename)

Commit
$ git commit -m 'message'

See changed files
$ git log --name-status HEAD^..HEAD

See changes for the exact file
$ git diff HEAD app/test.php


Merge
$ git merge (You have to be on the receiving branch, then merge the new branch.)
$ git add (After editing the file and solving conflict add the file to the stage)
$ git commit

Remote repositories
$ git remote (list remote repositories)
$ git remote -v (list with URL)
$ git remote show origin (show additional info)
$ git branch -vv (list with corresponding branches)
$ git branch -a (list all branches local and remote)
$ git branch -r (list only remote branches)
$ git checkout (if there is a remote branch, it will create the same branch locally and track it)

$ git fetch (will fetch new branches, all objects from remote to local, will not update working dir)
$ git remote prune origin (if you delete a branch on GitHub, this command will remove it from the remote branch list)

$ git pull (Same as $ git fetch + $ git merge FETCH_HEAD )
$ git pull origin dev
$ git merge --abort (Undo merge)
$ git pull -v

$ git push --set-upstream origin feature-2 (if local branch exits, but remote not exists, then run this command)
$ git push -u -v origin feature-2 (same as above)

If new-branch created on GitHub, you need to:
$ git fetch new-branch
$ git checkout new-branch

$ git remote update origin --prune (if github branch deleted, run this for remove tracking)
$ git push origin -d temp (delete github branch)

$ git show-ref (show all references)
$ ls .git/refs/heads (list local references)
$ ls .git/refs/remotes/origin (list remote references)
$ git show-ref (compare local and remote references)

If there is files on local and want to push to remote:
$ git remote add origin
$ git push -u origin master

$ git log --name-status HEAD^..HEAD (see last commit changes)

REBASING
Future branch on top of the master/release
Merging future into master/release branch
On the branch
$ git rebase master
$ git add .
$ COMPARE FILES
$ git add .
$ git commit -m ‘merging’
$ git rebase --continue
$ if you want abort: git rebase --abort
$ git commit -m 'Merge branch issue_215'

$ git checkout master
$ git pull
$ git checkout mybranch
$ git rebase master

// fatal: Not possible to fast-forward, aborting.
$ git pull --rebase

RESTORE FILE
$ git checkout HEAD index.html (restore the last committed version of the file)
$ git checkout 8a7b201 index.html(restore an old revision of a file)

DELETE Commit, Revert commit, Undo Commit
$ git reset --soft HEAD~1 (will keep changes)
$ git reset --hard HEAD~1 (will not keep changes)

Unstage file
$ git restore --staged FILENAME

STASH
$ git stash
$ git stash push -m "my_stash"
$ git stash list
$ git stash apply (will stash recent one)
$ git stash apply stash@{8} (will stash by number)
$ git stash drop stash@{1} (remove from stash)

Existing files, new repository
$ // delete .git directory
$ git init
$ git remote add origin https://github.com/rashad404/example.git
$ git add .
$ git commit -m ‘initial commit’
$ git push -u origin main