Git
Branch
git checkout -b <branch-name> #Create your branch locally, new branch followed by checkout:
git push -u origin <branch-name> #Push to remote
git branch -d <branch-name> #Delete a branch on your local filesystem
git push origin :<branch-name> #Delete the branch on remote
git branch --merged #Show which branches contain all commits from current branch
git branch -m old new #Rename branch
Config
git config user.name "User Name" #Set username
git config user.email [email protected] #Set email
git config --list #To check current values:
Diff
git diff index.html #To diff your changes
git diff --staged index.html #To diff your changes on stage:
git diff --color-words index.html #To show changes on same line with different colors:
git diff --color-words test..test2 #To show changes between branches on same line with different colors:
git diff abc123 index.html #To show changes between commit abc123 and now on file specified:
For word wrap on less in diff use minus sign (-) + shift + s + enter
Clean
git init #Initialize repo
git remote add origin https://[email protected]/x/x.git #Add remote
git add . #Add files
git commit -m "Initial commit" #Commit changes
git push --force -u origin master #Push code, force as it won't be in sync
git push origin :oldBranch #Remove leftover branches
.gitignore
git rm --cached index.html #Stop tracking a file if gitignore was added after the file was already tracked:
Line Ending
git config --global core.autocrlf true #Default setting for Windows OS is globally configured like so:
git config --global core.autocrlf input #And configured like the following on Linux and Mac OS:
Log
Show git commit log with one line per commit:
git log --oneline
Show git commit log with one line per commit, only commits on the range specified:
git log abc123..abc1234 --oneline
Show commits with changes on index.html since commit abc123:
git log abc123.. Index.html
Show lines changes on index.html since commit abc123:
git log -p abc123.. Index.html
Show statistics or number of changes per commit and summary:
git log --stat --summary
Search for term temp on commit comments:
git log --grep="temp"
History of a file with diffs including renames:
git log --follow -p -- <file>
title: Git - Ls-tree
category: VCS
To list files structure of a commit at HEAD:
git ls-tree HEAD
title: Git - Remote
category: VCS
Add first time if doesn't exist:
git remote add origin https://[email protected]/x/x.git
Change URL:
git remote set-url origin https://[email protected]/x/x.git
title: Git - Reset
category: VCS
git fetch --all && git reset --hard origin/master && git pull
title: Git - Simulate Clean
category: VCS
Simulate a cleanup operation of untracked files. Won't remove anything:
git clean -n
Will remove any untracked files:
git clean -f
title: Git - SVN2GIT
category: VCS
Steps to convert SVN repository to GIT using svn2git:
svn co --username x http://x.x.x.x/x svn log --quiet | grep -E "r[0-9]+ | .+ |" | cut -d'|' -f2 | sed 's/ //g' | sort | uniq echo "\ x x x [email protected] x x x [email protected] x x x [email protected] ... x x x [email protected]" > authors.txt svn2git http://x.x.x.x/x --authors ./authors.txt --verbose git remote add origin https://[email protected]/x/x.git git pull origin master git config --global user.name "User Name" git config --global user.email [email protected] git add file.txt git commit -m "SVN to Git Migration" git push -u origin master
title: Git - Undo
category: VCS
Checkout a file, discard any uncommitted change and stay on the same branch:
git checkout -- index.html
Checkout a file from an older commit:
git checkout abcdef123456 -- index.html
Unstage a file:
git reset HEAD index.html
Amend last commit, reusing it without creating a new commit:
git commit --amend -m "test"
Reset HEAD to specified commit:
git reset --hard abcdef123456
Reset HEAD to specified commit, the changes are on staging area and need to be committed:
git reset --mixed abcdef123456
Reset HEAD to specified commit, the changes are on the FS to be staged and committed:
git reset --soft abcdef123456