From StackOverflow.
There are three types of diff you can ask for:
-
git diff --cachedThe diff between what is in the index and the last commit. It shows you changes that will be in the next commit. -
git diffThe diff between the index and the working tree. These are the changes that you have made to files since you last added them to the index. -
git diff HEADThe diff between the files in the working tree and the last commit. There is a gotcha here: if you've made changes, added them to the index, and then removed these changes in the working tree:- you'll get no results for
git diff HEAD, because there is no difference, - you'll get output for
git diff --cached, because there are still changes in the index.
- you'll get no results for
Comparing against previous commit (HEAD~1):
HEAD~ can be replaced with any other reference to a commit one wants to compare against.
The following alias extends upon clean command. It allows to preserve
select non-versioned files, while removing all other non-versioned files.
In the example below, the generic project config files for Qt Creator IDE
are kept intact during repo clean up.
[alias]
clean-x = clean -x -e *?.creator* -e *?.files -e *?.config \
-e *?.includes -e *?.cflags -e *?.cxxflags
To fully clean up the repo, without confirmation, type:
$ git clean-x -df
$ git cherry -v local local_old | grep ^\+
NOTE
The equivalence test is based on the diff, after removing whitespace and line numbers.
$ git rebase -i HEAD~3
$ cd path/to/current/repo
$ git --git-dir=/path/to/another/repo/.git format-patch -k -1 --stdout <commit> \
[--relative=<some/dir>] | git am -3 -k [--directory=<root>]
The following two options are useful when applying a patch in a tree where the destination directory name differs from the source one:
-
--relative=<some/dir>Instructformat-patchto exclude changes outside the directory and show pathnames relative to it. Note:=between the option and its value is required. -
--directory=<root>Instructamto prepend<root>to all filenames. If a-pargument was also passed, it is applied before prepending the new<root>.
A dog:
$ git log --all --decorate --oneline --graph
$ git show :path/to/the/file
$ git show HEAD:path/to/the/file
NOTE
Note the colon ":" in front of the file path. Note that placing "--" before the path does not work as a separator here.
$ git show -- ':!some/path'
$ git log -1 --format=%B <COMMIT>
$ git restore --staged -- <FILE>
$ git log -1 --pretty= --name-only <COMMIT>
$ git stash push -k -u
$ git checkout stash@{0} -- README
See here for details.
$ git checkout stash@{0}^3 -- untracked/file.txt
$ git diff --cached stash@{0} -- README
git filter-branch -f --env-filter "
GIT_AUTHOR_NAME='New Name'
GIT_AUTHOR_EMAIL='new@email'
GIT_COMMITTER_NAME='New Name'
GIT_COMMITTER_EMAIL='new@email'
" HEAD
- Find the commit the new commit should be injected after:
$ git rebase -i HEAD~3
- Choose
edit. - Make changes and do
git commit, but without--amend. git rebase --continue.
IMPORTANT
Save the original commit's message before
reset, since it will be lost otherwise.
Start an interactive rebase:
$ git rebase -i COMMIT~1
Mark the commit to be split with edit.
Then, reset the state to the previous commit:
$ git reset HEAD~1
Use git add and git commit to incrementally add changes.
Then resume rebasing:
$ git rebase --continue
Source: Embedded Artistry: Workflow for Splitting git Commits
$ git branch <BRANCH_NAME> <COMMIT>
$ git branch -f <BRANCH_NAME> <COMMIT>
$ git remote add alt-remote-name alt-remote-machine:/path/to/repo.git
$ git remote update
$ git pull alt-remote-name master
$ git remote rm alt-remote-name
$ git branch -r
$ git remote rename <REMOTE_NAME_CUR> <REMOTE_NAME_NEW>
for example:
$ git remote rename origin production
$ git remote prune [-n] <name>
n: dry run
And test if there are any uncommitted changes in the index.
If there are such changes, execute specified command.
Note that : is required, because foreach stops upon
first command that returns a non-zero return code.
$ git submodule foreach 'git status --porcelain=v1 | \
((grep -q -v ^$ && git checkout -b mlgtint-2099_qcsapi_headers) || :)'
In the above example, the conditionally executed command is:
$ git checkout -b mlgtint-2099_qcsapi_headers
$ git diff --irreversible-delete ...
$ git fetch-pack --thin --keep <REPO-URL> 9c8f89caec0a42f10cfb58aee836992f173c4f3e
remote: Counting objects: 1, done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1/1), done.
keep 0912222d08511df41f9f37c861ec5d75dcfdb26f
9c8f89caec0a42f10cfb58aee836992f173c4f3e 9c8f89caec0a42f10cfb58aee836992f173c4f3e

