Git command cheatsheet
We use some kind of VCS(version control system) tool to manage our source code. Git is one such tool which is widely used by developer across the world. Git has a powerful set of command and it is mostly used as a command line tool. So this post is all about cheat sheet of common Git command for quick reference.
Config
Config command is used to 'add new' or 'modify existing' Git setting.
Description | Command |
---|---|
list all the current git setting | git config --list |
set git username for commit | git config --global user.name "vikash kumar" |
set git email for commit | git config --global user.email "vikash@sysleaf.com" |
set editor for git message, vim is default | git config --global core.editor emac |
tell git to remember your credential | git config credential.helper store |
Repository: init & clone
A project is basically a folder containing files and/or subfolder. To manage the project, Git uses a data structure called ‘repository’. To create repository, Git create one special folder ‘.git’, where all the project related information is kept.
Description | Command |
---|---|
convert local folder to git repository | git init |
clone locally an existing repository with URL 'github.com/repo' | git clone github.com/repo |
Local: add, rm, commit & status
In Git there are 3 types/states of files as describe here:
Description | Command |
---|---|
add file 'Readme.md' to staged | git add Readme.md |
add files 'Readme1.md', 'Readme2.md' to staged | git add Readme1.md Readme2.md |
add all files to staged | git add . |
remove file 'readme.md' from committed to staged | git rm readme.md |
remove file 'readme.md' from staged to unstaged | git rm --cached readme.md |
remove file 'readme.md' from staged & delete it from folder | git rm -f readme.md |
commit staged files with message | git commit -m "message" |
commit the changes in all unstaged(only already tracked) files skipping staging | git commit -a -m "message" |
discard the changes made in tracked file 'readme.md' | git checkout readme.md |
check status of all files | git status |
see the changes in all unstaged(only already tracked) files | git diff |
see the changes in all staged files | git diff --cached |
see all the commits | git log |
see last 3 commits | git log -3 |
see the difference/modification in each commit | git log -p |
Remote: push, pull & fetch
Remote repository is hosted on internet/network. Each remote repository will have a short name and URL linked to it.
Description | Command |
---|---|
see all remote shortname | git remote |
see all remote shortname & its URL | git remote -v |
add new remote with shortname 'origin' & URL 'github.com/repo' | git remote add origin github.com/repo |
see more info about remote 'origin' | git remote show origin |
rename remote 'origin' to 'heroku' | git remote rename origin heroku |
remove remote 'origin' | git remote rm origin |
fetch all the branches from remote 'origin' but don't merge | git fetch origin |
fetch 'master' branch from remote 'origin' but don't merge | git fetch origin master |
merge 'master' branch from remote 'origin' into current branch | git merge origin/master |
fetch 'master' branch from remote 'origin' & merge into current branch | git pull origin master |
fetch 'master' branch from remote 'origin' & merge into current branch & rebase commit message | git pull --rebase origin master |
push branch 'master' to remote 'origin' | git push origin master |
Branch: checkout & merge
Every git repository will have at least one main branch, generally called ‘master’ branch, which will have stable code base. To prevent ourselves from accidentally pushing bad code into the master code base, we create a new branch(namely dev, feature etc.) from ‘master’ and whatever ‘code changes have to be done’ is done in this new branch. Once we are done with the changes in this new branch, we test our changes and on a successful test, we merge this branch with ‘master’ branch.
Description | Command |
---|---|
list all the branches: 'stared' branch will be current branch | git branch |
create new branch 'dev' from current branch, but don't switch to it | git branch dev |
Delete branch 'dev' | git branch -D dev |
switch to existing branch 'dev' | git checkout dev |
create a new branch 'dev' & switch to it | git checkout -b dev |
merge 'dev' branch into current branch | git merge dev |
to see files on which merge conflict has occur | git status |
to fix merge confilict: open conflicted files with any editor & fix manually | vi [conflicted files] |