Git Cheatshell - Pro Git
A git cheatshell based on the book: http://www.git-scm.com/book/en/v2.
Repository Configuration
git init - initialize an empty repository
git clone https://github.com/libgit2/libgit2 - clone the remote repository to a local target named libgit2
git clone https://github.com/libgit2/libgit2 mylibgit - clone the remote repository to a local target named mylibgit
/etc/gitconfig: contains values for every user on the system and all their repositories. git config --system will read and write from this file
~/.gitconfig: specific to your user. git config --global will read and write from this file
.git/config: specific repository configuration
git config --global user.name "Jonathan Lim" - set global user to "Jonathan Lim"
git config --global user.email jonathanlim@example.com
git config --global core.editor emacs - set your default editor
Track Changes
git status - to view current status of your working directory
git add README - add the file README to staging area
git commit - commit changes in staging area. In this case, a text editor will pop up to input commit comments
git commit -m "commit message" - commit with commit message
git commit -a -m "commit message" - makes Git automatically stage every file that is already tracked before doing the commit
git commit --amend - Amend the last commit, e.g. commit more files/modify the commit message
git rm - remove the file from the working directory and from the index. If the file is modified already and added to the index. Use git rm -f to force the removal
git rm --cached - keep the file in the working directory but remove it from the index
git mv file_from file_to - rename a file in Git
git log - list all the commits made in that repository
git log -p -2 - shows difference introduced in each commit. -2 limits the output to only the last two commits
git log --pretty="format string" - format the log
git log -Sfunction_name - Find the last commit that add/remove a reference to a specific function
git diff - to see what you've changed but not yet staged
git diff --staged - to see what you've staged that will go into your next commit
git diff --cached - just like git diff --staged
git checkout -- [file] - discard all the changes on the file since last commit
Working with Remote
git remote - show remote repository name
git remote -v - show remote repository name and URL
git remote add [short name] [url] - Add a new remote Git repository as a shortname
git fetch [short name] - pull down all the data from that remote project. After doing this, should have references to all the branches from the remote. Note that, git fetch just pulls the data to the local repository - it doesn't do the merge automatically.
git push [short name] [branch name] - push local branch to remote server
git remote show [short name] - see more information about a particular remote
git remote rename [old short name] [new short name] - change a remote's shortname
git remote rm [short name]
Tagging
Typically people use this functionality to mark release points (v1.0, and so on)
- Lightweight tag: like a branch that doesn't change (it's just a pointer to a specific commit)
- Annotated tag: stored as full objects in the Git database
git tag - list the available tags
git tag -l [pattern] - search for tags with a particular pattern
git show [tag] - show tag data along withe the commit that was tagged
git tag -a [tag] -m [message] - create a annotated tag
Branching
Tracking branch/upstream branch - local branches that have a direct relationship to a remote branch.When you clone a repository, it generally automatically creates a master branch that tracks origin/master
git branch [branch name] - create a new branch
git branch -v - show last commit on each branch
git branch --merged|--no-merged - show which branches are already merged/not merged into the current branch
git branch -d [branch] - delete a branch
git branch -u [remote branch, e.g.origin/hotfix] -
git log --oneline --decorate - show where the HEAD pointer is point
git log --online --decorate --graph --all - show the history of the commits, showing where the branch pointers are and how history has diverged
git checkout [branch name] - switch to an existing branch
git checkout -b [new branch name] - create a new branch and switch to the new one
git checkout -b [branch] [remote branch, e.g. origin/hotfix] - create a local branch that mapping to the remote branch
git merge [branch] - merge the target branch into current branch
git push [short name, e.g. origin] [local branch]:[branch on the remote] - push local branch to the branch on the remote project
git branch -vv - show what tracking branches you have set up
git rebase [branch] - take the patch of the changes that was introduced in C4 and reapply it on top of C3.
git rebase [base branch] [topic branch] - check out the topic branch and replay it onto the base branch