revert/cancel changes
undo last pull
git pull whoops? git reset HEAD@{1} Note1: ORIG_HEAD is previous state of HEAD, set by commands that have possibly dangerous behavior, to be easy to revert them. It is less useful now that Git has reflog: HEAD@{1} is roughly equivalent to ORIG_HEAD (HEAD@{1} is always last value of HEAD, ORIG_HEAD is last value of HEAD before dangerous operation) Note2: Meanwhile man git-rev-parse describes this. HEAD@{1} is the previous value of symbolic HEAD in the reflog whereas HEAD^ is the (first) parent revision of the current HEAD. These two need nod be equivalent (e.g. after a rebase, a hard reset, a branch switch and such things). Do read the linked article for reflog.
git reflog show
should show you the history of HEAD. You can use that to figure out where you were before the pull. Then you can reset your HEAD to that commit.
check history
check history of a file
gitk filename or git log -- filename git log --follow filename
if need the content of each commit:
git log --follow -p filename
check changed files in a commit
git diff-tree --no-commit-id --name-only -r [commitHash]
check files
ls-files
supports glob
git ls-files
changed files
check changed files in a commit
git diff-tree --no-commit-id --name-only -r [commitHash]
or
git show --pretty="format:" --name-only [hash]
grep files
Branches
To Last branch
@{-1} is a way to refer to the last branch you were on. This is accepted not only where an object name is expected, but anywhere a branch name is expected and acts as if you typed the branch name. E.g. "git branch --track mybranch @{-1}", "git merge @{-1}", and "git rev-parse --symbolic-full-name @{-1}" would work as expected.
git checkout -
is a shorthand for git checkout @{-1}
.
rename branch
Rename = deleting and then re-creating on the remote:
git branch -m master master-old git push origin :master # delete master git push origin master-old # create master-old on remote #for master: git checkout -b master some-ref # create a new local master git push origin master # create master on remote
delete branches
Local branch:
git branch -D <branchName>
Remote branch:
git push origin --delete <branchName> or git push origin :branchname
Diff
working copy & local repo
git diff
stage area & local repo
git diff --cached
local repo & remote repo
git fetch git diff master origin/master or git diff HEAD...origin/master or git diff HEAD..origin
discard changes
ignore changes to already tracked files
- Temporarily ignore file/directories
#ignore changes on file or directory git update-index --assume-unchanged <file/directory> #start tracking change again git update-index --no-assume-unchanged <file/directory>
-
make the
.gitignore
work
= #First: Delete the file from the repository cache = git rm --cached path/file #Second: Add the ignore rule to your .gitignore file path/filename
modified files
#unstaged git co . git co -- #staged & unstaged git reset --hard
untracked
#clean : remove unstaged files from working tree # -d : dir # -f : force git clean -df
revert commited
git revert ...
Misc
git alias
multiple commands and arguments
function way:
files = "!f() { git diff --name-status $1^ $1; }; f"
An alias without !
is treated as a Git command; e.g. commit-all = commit -a
.
With the !
, it's run as its own command in the shell, letting you use stronger magic like this.
git archive
# list supported format git archive -l # do archive: git archive --format tar.gz -o /path/to/foo.tar.gz [branch]
show outgoing/incoming chgs
similar as hg outgoing/incoming
:
#In git, list commits that are on branch B but not on branch A, do git log A..B. #git fetch first #outgoing git log origin/master..master or (FIXME the line below needs to be checked again) git log origin.. #incoming git log master..origin/master #another option: #in git whatchanged ..origin #out git whatchanged origin..
remote
remove branches that were already removed remotely: prune
#show remote branches: git branch --remote #remove not existed remote branches (locally) git remote prune origin