Getting up speed with Git
A scratchpad of ideas
In the previous chapters, all basic topics were convered. Git has many command options, and in this final page some of those are listed.
Feel free to pick only a few to improve your workflow.
Tricks by category
Adding files
git add -u# add all updated filesgit add .# add all files in the current folder onlygit add -n# see what will be added (--dry-run)
Removing files
git rm --cached# remove a file from git without deleting itgit clean# remove untracked files
Committing
git commit --author=".."# commit a patch with correct authorgit commit -v# show the diff within the commit message editorgit commit --amend# fix a previous commit (only if not pushed!)git commit -c commit# use an old commit message (--reedit-message=...)
Branches
git branch -a# view all branchesgit branch -r# view remote branchesgit branch --merged# view branches which are merged into the currentgit branch --no-merged# view branches which are not merged into the currentgit branch name commit# create a branch with different start pointgit checkout -b branch# create a branch, and switch to itgit checkout -t origin/name# quickly start working from a remote branchgit checkout -# switch to the previous branch (like cd -)
Diffs
git diff .# show diffs of the current folder onlygit diff --stat# show a diffstatgit diff --name-status# show filename + status like svngit diff --diff-filter=R# show renamed files onlygit show commit# show one commitgit show v2.6.15:a.txt# show old version of a file
Logging
git log --decorate# show branch/tag labelsgit log -p -C# show patches, detect file renamesgit log --name-status# show filename + status like svngit log --stat# show a diffstat for each commitgit log --all --graph --decorate# show all branches in a graphgit log --simplify-by-decoration# show branch start points onlygit log --no-merges# hide merge commitsgit log --oneline --name-only from^..to# show changed files per commitgit log --format='%h %an %ar - %s'# timeline with x days ago, etc...git log origin/master..HEAD# show new changes in HEAD, not found in origin/mastergit log HEAD..origin/master# show newly fetched changes (HEAD can be omitted)git shortlog ..origin/master# summarize fetched changesgit shortlog -n 10# summarize last 10 commitsgit shortlog -n 10 -s# show authors of last 10 commitsgit blame file# find who changed which linegit blame -C file# detect lines which are moved or copied between files
Undoing changes
git checkout -- file# discard all changes of a filegit checkout --patch -- file# discard parts of a filegit checkout commit -- file# extract an old file versiongit revert# make a commit which reverts somethinggit reset HEAD# unstage something (uses --mixed)git reset --hard HEAD# discard all local changes
Rewriting history
git commit --amend# correct the previous commit, with the staged changesgit reset HEAD^# forget about a commit, load it back into the staging areagit reset --soft HEAD^# remove a commit, keep staged and working changesgit reset --hard HEAD^# remove a commit, delete working copy changes and staged changesgit reset --hard ORIG_HEAD# forget about a merge or rebase, get back to the old HEADgit rebase -i origin/master# rewrite history of new commits
Patch files
git format-patch -o outputdir from..to# export each commit as patch filegit apply patchfile# apply a patch to the working treegit am *.patch# apply patches from mbox filesgit am maildir/# apply patches from a maildir
Searching
git grep# search through tracked filesgit grep -p# show function name (--show-function)git grep v2.6.15 "foo()"# search old tree for "foo()"git log -Stext# search through all commits for a changed textgit bisect# find which revision contains a bug (see 1, 2)
Shares repositories
git clone --bare dir1 dir2# convert a repository to a "bare" repositorygit remote add aliasname url# add a remote repositorygit push origin master# push master branchgit push origin master:master# the samegit push origin :branch# delete remote branchgit push origin :# push matching branches (the default)git push -f# force overwriting remote changes (careful!)
Stashes
- Stash implementation detail
- Note that the git stash save works internally by creating a temporary branch. This is visible in the output of git log --all.
git stash branch branchname # make a new branch, apply the stash thereListing files
git ls-files --others# show untracked filesgit ls-files --deleted# show deleted filesgit ls-files --unmerged# show unmerged files
Making releases
git archive --format=tar --prefix=myproject-2.0 v2.0 \ | gzip > mycode-2.0.tar.gz# export the tag v2.0 as archivegit tag# list all tagsgit tag name# create a light tag (just a reference)git tag -a name# create a annotated tag (with message)git tag -s name# create a GPG signed tag, with messagegit tag -d name# delete taggit push --tags# push tags
Useful aliases
Here is an overview of all aliases, including those used in the previous pages:
# for convenience:git config --global alias.st "status" git config --global alias.staged "diff --staged" git config --global alias.unstage "reset HEAD" git config --global alias.up "pull --rebase"# memnonicsgit config --global alias.diff-all "diff HEAD"# nice output info:git config --global alias.log-graph "log --all --graph --decorate" git config --global alias.log-refs "log --all --graph --decorate --oneline --simplify-by-decoration --no-merges" git config --global alias.log-timeline "log --format='%h %an %ar - %s'" git config --global alias.diff-stat "diff -b --stat"# finding new commits:git config --global alias.log-local "log --oneline origin..HEAD" git config --global alias.log-fetched "log --oneline HEAD..origin/master"
Referencing commits
- More referencing syntax
-
The rev-parse manual provides more obscure syntax notations, including:
HEAD@{date} select a date.
HEAD@{1.month.ago} starts a month ago.
:/str commit message starting with str.
Branch names
The name HEAD always points to your current location; e.g. the last commit of your current branch. Remote locations are written in the form location/branch, for example origin/master.
Visiting parents
A parent commit can be referenced by HEAD^, the grantparent is HEAD^^ or HEAD~2.
When a commit has two parents, HEAD^2 selects the second one.
Previous values
The syntax HEAD@{1} refers to the previous value of HEAD. This is used by the reflog.
Ranges
The syntax first..second takes all commits which lead to second, except those which lead to first. It's the same as: ^first second. Likewise, first^..second takes all commits which lead to second, except those with lead to the parent of first; theirby including first.
The syntax first...second takes all commits from both branches, except those from the common branch point.
blog comments powered by Disqus