Git Cheatsheet

Table of contents

Shorten git ssh url

$ git config --global url."ssh://[email protected]:8910/path/to/your/git/".insteadOf yourserver://

Git push all (push to mutiple repos)

Create an all remote with several repo URLs to its name:

$ git remote add all origin-host:path/proj.git  
$ git remote set-url --add all nodester-host:path/proj.git  
$ git remote set-url --add all duostack-host:path/proj.git  

Basic

# initiates git in the current directory  
$ git init            

# add remote reposiory  
$ git remote add origin https://github.com/repo_name.git 

# creates a git repo from given address (get the address from your git-server)  
$ git clone <address> 

# clones a git repo from the address into the given directory and checkout's the given branch  
$ git clone <address> -b <branch_name> <path/to/directory> 

# Clones a single branch
$ git clone <address> -b <branch_name> --single-branch

# adds(stages) file.txt to the git  
$ git add <file_name>

# adds(stages) all new modifications, deletions, creations to the git  
$ git add *

# create new branch and switch to it
$ git switch -c NEWBRANCH

# shows the modifications and stuff that are not staged yet
$ git status

Git reset

# Removes file.txt from the stage  
$ git reset file.txt 

# Throws away all your uncommitted changes, hard reset files to HEAD
$ git reset --hard

# moves the head pointer
$ git reset --soft <commit_id>

# moves the head pointer and then copies the files from the commit it is now pointing to the staging area
$ git reset --mixed <commit_id> 

# moves the head pointer and then copies the files from the commit it is now pointing to the staging area
$ git reset -hard <commit_id>

git reset  
1. Move HEAD and current branch  
2. Reset the staging area  
3. Reset the working area  
  
--soft = (1)  
--mixed = (1) & (2) (default)  
--hard = (1) & (2) & (3)  

Git rm

# removes file.txt both from git and file system  
$ git rm file.txt

# only removes file.txt both from git index
$ git rm --cached file.txt

Git branch

# shows all the branches (current branch is shown with a star)
$ git branch

# shows all the branches local and remote
$ git branch -a

# creates my-branch
$ git branch my-branch

# deletes my-branch
$ git branch -d my-branch

# switches to my-branch
$ git checkout my-branch

# merges my-branch to current branch
$ git merge my-branch

# delete remote branch
$ git push origin --delete my-branch

# rename the branch
$ git branch -m <new-branch-name>

# checkout a branch with no commit history
$ git checkout --orphan <branch_name>

# list all branches and their upstreams, as well as last commit on branch
$ git branch -vv

# List all local and remote branches
$ git branch -a
   
# merge the specified commit
$ git cherry-pick <commit_id>

# pick the entire range of commits where A is older than B ( the ^ is for including A as well )
$ git cherry-pick <commit_id_A>^..<commit_id_B>

Git Remote

# shows the remotes
$ git remote

# shows the remote for pull and push
$ git remote -v

# creates a remote (get the address from your git-server)
$ git remote add my-remote <address>

# Remove a remote
$ git remote rm my-remote

# Change remote url
$ git remote set-url <remote-name> <remote-url>

Git log

# shows the log of commits
$ git log

#git log by default uses less command so you can use these: f=next page, b=prev page, search=/<query>, n=next match, p=prev match, q=quit

# shows the log of commits without less command
$ git log --no-pager

# Show what files were changed, only name
$ git log --name-only

# Show what files were changed, and number of changes
$ git log --stat

# Show name and how it was changed (A = added, M = modified, D = deleted)
$ git log --name-status

# shows the log of commits, each commit in a single line
$ git log --oneline

# shows the log of commits, each commit in a single line with graph
$ git log --oneline --graph --decorate

# shows the log of commits since given time
$ git log --since=<time>

# change over time for a specific file
$ git log -- <file_name>

$ git log -p <file_name>

# lists commit(s) in branch1 that are not in branch2
$ git log <Branch1> ^<Branch2>

# lists the last x commits
$ git log -n <x>

# lists the last x commits, each commit in single line
$ git log -n <x> --oneline

# Find lines matching the pattern in tracked files
$ git grep --heading --line-number '<string/regex>'

# Search Commit log
$ git log --grep='<string/regex>'

# record when the tips of branches and other references were updated in the local repository.
$ git reflog



# show information about files in the index and the working tree
$ git ls-files

Git commit

# commit changes with a msg
$ git commit -m "msg"

# commit changes with a title and description
$ git commit -m "title" -m "description"

# combine staged changes with the previous commit, or edit the previous commit message without changing its snapshot
$ git commit --amend

# amends a commit without changing its commit message
$ git commit --amend --no-edit

# Amend the author of a commit
$ git commit --amend --author='Author Name <[email protected]>'

# pushes the commits to the my-remote in my-branch (does not push the tags)
$ git push my-remote my-branch

# Undo a commit by creating a new commit
$ git revert <commit-id>

Git diff

# shows one or more objects (blobs, trees, tags and commits).
$ git show

# show changes between commits, commit and working tree
$ git diff

#show changes between working directory vs last commit
$ git diff HEAD

#show changes between stage area vs last commit
$ git diff --staged HEAD

# show colored diff
$ git diff --color

# Shows changes staged for commit
$ git diff --staged

# Shows diff between branches
$ git diff main..second-branch

Git tag

# shows all the tags
$ git tag

# creates an annotated tag
$ git tag -a v1.0 -m "msg"

# shows the description of version-1.0 tag
$ git show v1.0

# deletes the tag in local directory
$ git tag --delete v1.0

# deletes the tag in my-remote (be carefore to not delete a branch)
$ git push --delete my-remote v1.0

# push v1.0 tag to my-remote in my-branch
$ git push my-remote my-branch v1.0

# pulls the tags from remote
$ git fetch --tags

# pulls and tries to merge my-branch from my-remote to the current branch git pull = git fetch && get merge
$ git pull my-remote my-branch

Git stash

# stashes the staged and unstaged changes (git status will be clean after it)
$ git stash

# stash everything including new untracked files (but not .gitignore)
$ git stash -u

# stash with a msg
$ git stash save "msg"

# list all stashes
$ git stash list

# delete the recent stash and applies it
$ git stash pop

# delete the {2} stash and applies it
$ git stash pop stash@{2}

# shows the description of stash
$ git stash show

# keep the stash and applies it to the git
$ git stash apply

# creates a branch from your stash
$ git stash branch my-branch stash@{1}

# deletes the {1} stash
$ git stash drop stash@{1}

# clears all the stash
$ git stash clear

Git rebase

# Rebase commits from a commit ID
$ git rebase -i <commit_id>

# Abort a running rebase
$ git rebase --abort

# Continue rebasing after fixing all conflicts
$ git rebase --continue

Git clean

# clean untracked files permanently
$ git clean -f

# To remove directories permanently
$ git clean -f -d/git clean -fd

# To remove ignored files permanently
$ git clean -f -X/git clean -fX

# To remove ignored and non-ignored files permanently
$ git clean -f -x/git clean -fx

# shows what would be deleted
$ git clean -d --dry-run

Git config

# lists the git configuration for all repos
$ git config --global --list

# opens an editor to edit the git config file
$ git config --global --edit

# add git aliases to speed up workflow , eg.
$ git config --global alias.<handle> <command>

# config default editor
$ git config --global core.editor <editor_name>

# create an archive of files from a named tree
$ git archive <branch_name> --format=zip --outpute=./<archive_name>.zip

Troubleshooting

If you get warning: remote HEAD refers to nonexistent ref, unable to checkout, then do $ git branch -a and then checkout the branch you want $ git checkout branch.