Git Reference
Basics
Configure Git
git config --global user.name "Your Name"- set user namegit config --global user.email "user@domain.com"- set emailcd /path/to/repository- set directory pathgit config --global core.editor /path/to/editor- set default editorgit config --global color.ui auto- turn on colors when possible
Create Repository for New Project
mkdir some-repositorycd some-repositorygit init
Create Repository for Existing Project
cd /path/to/some/directorygit init
Clone Repository
git clone https://github.com/username/repo.gitgit clone git://github.com/username/repo.gitgit clone git://github.com/username/repo.git cloned-repo- clone to specific pathgit clone --depth 50 some-repository- shallow clone with last 50 commits
Stage Files
git add .- add all filesgit add -Aorgit add --all- add all files not explicitly ignoredgit add -uorgit add --update- add changed tracked files, does not add new filesgit add myfile.*- add all files beginning with “myfile.”git add -porgit add --patch- specifically choose which changes to commit, select ‘y’ or ‘n’ for each change- git does not track empty directories (as of v1.7) but adding an empty dot file will force tracking (i.e. “.include_in_git”)
Check Status and Commit Changes
git statusgit commit -m "initial commit"
Ignore Files
- add files to .gitignore: i.e.
cache,*.RData git config --global core.excludesfile ~/.gitignore- set up global excludes file in .gitignore in home directory
Undo Changes
git reset HEAD -- myfile.R- unstage myfile.Rgit checkout -- myfile.R- undo uncommitted changes to myfile.R
Share Changes
git fetch reponame- get changes from remote repositorygit pull reponame- pull from remote repositorygit pull origin- pull and rebase local changes on top of remote changesgit pull --rebase origin remotebranchgit push remotename branchname- push changes to remote repository
Create Branches
git branch somebranchname optionalcommitIDorTag- create branchgit checkout somebranchname- switch to branchgit checkout -b somebranchname- create and switch to branchgit branch --track newbranchname- default tracking for remote branchesgit branch --no-track new branchname- turn tracking off
View Branches
git branch- view local branchesgit branch -r- view remote branchesgit branch -a- view all branchesgit branch --merged- view merged branches in current branchgit branch --no-merged- view unmerged branchesgit branch --contains somecommitID- view branches with some commitID
Merge Branches
git checkout mastergit merge developmentbranch- merge new branch into mastergit merge --no-commit developmentbranch- merge without commitgit merge --no-ff developmentbranch- force merge commit, no fast forwardgit merge -m "custom commit message" developmentbranch- merge with custom commit messagegit rebase master- update development branch with new master commitsgit rebase commitIDorTag- update parent for development branchgit reset --hard ORIG_HEAD- undo a rebasegit branch -d mergedbranch- delete unused merged branchgit branch -D unmergedbranch- delete experimental unmerged branch
Tag Commits
git tag- list all tags, remote and local treated the samegit tag v1.0- create tag on latest commit in current branch
View Log
git log- see reverse chronological list of commitsgit log --oneline- see shortened commit ID and messages in one linegit log -5- see last 5 commitsgit log -1 -p HEAD- see changes in most recent commitgit log -- filename- see log for single filegit log --after="7 days"- see commits in last weekgit log --grep="some regular expression"- view log entries with some string
View Changes
git diff- diff current working tree and staging areagit diff --stages- diff staged changes and repositorygit diff HEAD- diff working tree and repositorygit diff firstcommitIDorTag secondcommitIDorTag- diff two commits
Other Commands
git mv README.md README.txtorgit mv README.md wiki/- rename or move README filegit rm -- outdated.txt- remove filegit rm -r -- outdated/- remove directorygit remote add branchname webURL- add new remote repositorygit remote rm branchname- remove remote repositorygit pull origin remotebranch:localbranch- pull remote branch into local branchgit push origin remotebranch:localbranch- push local branch into remote branchgit push origin :beta- delete remote branch called beta (replaces with empty branch)git revert --no-edit commitID- revert commit with default revert commit messagegit reset --soft HEAD^- reset last commit and stage changesgit gc- run garbage collection by default removes orphans older than 2 weeksgit gc --prune="1 week"- remove loose objects older than 1 week
Miscellaneous
Working with Forks
Reset origin from original upstream repository to personal fork
git remote set-url origin https://github.com/username/myrepo.git
Add original upstream repository as another remote and merge upstream commits into fork
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.gitgit pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git BRANCH_NAMEgit push origin master
Squash Commits
Example of squashing last 3 commits with message “squash commits”
git reset --soft HEAD~3 && git commit -m "squash commits"
Force push
git push -f
Untrack Changes
In a directory
This command will cause git to untrack your directory and all files under it without actually deleting them:
git rm -r --cached <your directory>
- The
-roption causes the removal of all files under your directory. - The
--cachedoption causes the files to only be removed from git’s index, not your working copy. By default git rmwould delete .
For a specific file
Use the following command:
git update-index --assume-unchanged FILE_NAME
To track the changes again use this command:
git update-index --no-assume-unchanged FILE_NAME
Merging Branch
After the merge, it’s safe to delete the branch:
git branch -d branch1
Additionally, git will warn you (and refuse to delete the branch) if it thinks you didn’t fully merge it yet. If you forcefully delete a branch (with git branch -D) which is not completely merged yet, you have to do some tricks to get the unmerged commits back though (see below).
There are some reasons to keep a branch around though. For example, if it’s a feature branch, you may want to be able to do bugfixes on that feature still inside that branch.
If you also want to delete the branch on a remote host, you can do:
git push origin :branch1
This will forcefully delete the branch on the remote (this will not affect already checked-out repositiories though and won’t prevent anyone with push access to re-push/create it).