Useful Git commands reference
TODO: formatting, organize, index/TOC
git add [files]
git commit -m [message]
git push
git diff [filename]
git diff
git status
git rm [files]
git reset --hard origin/mybranch
git fetch origin; git branch -a; git pull --all;
OR
git fetch origin --depth=10000 $(git ls-remote -h -t origin); git pull --all;
git fetch origin <source branch name>:<destination branch name>
git clean -f
If you want to also remove directories, run git clean -f -d
or git clean -fd
If you just want to remove ignored files, run git clean -f -X
or git clean -fX
If you want to remove ignored as well as non-ignored files, run git clean -f -x
or git clean -fx
git checkout --[theirs|ours] [file]
git add [file]
git merge --strategy-option theirs
git reset HEAD^ -- [file]
git commit --amend
git push origin HEAD --force
git rebase -i HEAD~2
git push origin HEAD --force
git reflog
git reset --hard HEAD@{5}
git remote -v
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
git remote -v
git fetch upstream
git checkout master
git merge upstream/master
git fetch upstream {{branch}}
git checkout {{branch}}
git rebase -i upstream/{{branch}}
git branch -d the_local_branch
git push origin :the_remote_branch
git fetch -p && for branch in `git branch -vv | awk '{print $1,$4}' | grep 'gone]' | awk '{print $1}'`; do git branch -D $branch; done
git stash
git stash pop
git stash -u
git rev-parse HEAD
git submodule status
git submodule update --remote --init
git add {{submodule directory}}
git commit -m "Update submodule"
git rebase -i --root
git remote rm upstream
git remote add origin [NEW_UPSTREAM_URL]
https://help.github.com/articles/changing-author-info/
echo "<NEW-ROOT-SHA1>" > .git/info/grafts`
# to check
git log
# to make permanent
git filter-branch -- --all
git reset --hard origin/[branch]
cd path/to/local/repo
git config user.email "[email protected]"
open .git/config
git checkout -b myFeature dev
git checkout -b myFeature
git checkout -b new-feature master
[make commits, do work,...]
git checkout -b new-feature-candidate new-feature
git rebase -i master
[mark all commits except first one with ‘s’ for squash]
[modify the commit message]
[ :wq to save the file]
git checkout master
git merge new-feature-candidate
git rebase --preserve-merges
# create patch file from last commit on current branch
git format-patch -1 HEAD
# check for error before applying patch
git apply --check file.patch
# show stats regarding patch to be applied
git apply --stat file.patch
# amend patch
git am < file.patch
# just apply the patch normally
git apply mypatch.patch
# if there are issues applied the patch run with -3 argument to initiate 3-way merge
# and allow resolving conflicts using a tool like kdiff
git am -3 < file.patch
# find the commit to revert to
git log
# reset to that commit
git reset --hard commit_sha
# force push
git push origin HEAD --force
git config --global mergetool.kdiff3.path /Applications/kdiff3.app/Contents/MacOS/kdiff3
git mergetool -t kdiff3
git clean -f
git reset --hard origin/master
git rebase --edit-todo
git merge --squash
Display what is in branc_2 but not in branch_1
git diff branch_1..branch_2
Display what is in branch_1 XOR branch_2 (either branch_1 or branch_2 but not both)
So git b1...b2 is equal to git b2...b1
git diff branch_1...branch_2
git merge-base feature master
git checkout feature
git checkout -b temporary-branch
git rebase -i master
# clean up/edit the history
# ‘<,’>s/pick/squash
git checkout master
git merge temporary-branch
Use git clean -xdn
to perform a dry run and see what will be removed.
Then use git clean -xdf
to execute it.
Remove all files then re-add them to vcs:
git rm -r --cached .
git add .
git checkout --orphan <branchname>`
git commit --allow-empty -m "initial commit"
git push -u origin <branchname>
git config credential.helper store
git reset HEAD~
git rm --cached [-r] path/to/file/or/directory
# Create a mirrored clone.
git clone --mirror {{old-repo-url}} {{new-repo-name}}
# Remove origin (ignore warnings if shown about not all branches removed)
cd {{new-repo-name}}
git remote remove origin
# Add the new origin.
git remote add origin {{new-repo-url}}
# Push everything (or alternatively just specific branches).
git push origin main # only main branch
git push --all # all branches
git push --tags
# Replaced the mirrored clone with the actual repo so commits can be made.
cd ..
rm -rf {{new-repo-name}}
git clone {{new-repo-url}}