Install Bash git completion
brew install git bash-completion
Caching GitHub password in Git
git config --global credential.helper 'cache --timeout 72000'
- Remove cached password (to reenter new password when it’s changed)
git credential-osxkeychain erase
Temporarily ignoring files
git update-index --assume-unchanged file-path
git update-index --no-assume-unchanged file-path
Remove file from the repository but keep it locally
git rm --cached -r somedir/somefile
then add them to .gitignore
Reset current HEAD to the specified state
undo last commit
git reset --soft HEAD~1
- Undo commits permanently:
git reset --hard HEAD~3
- The last three commits (HEAD, HEAD^, and HEAD~2) were bad and you do not want to ever see them again.
- Undo a merge or pull inside a dirty working tree: git reset –merge ORIG_HEAD
- return to any previous revision
git reset --soft commit_hash
- Reset committed changes to local changes
git reset HEAD~
undo git pull
git reset --hard
Reset local branch to be same as remote branch
git fetch --all && git reset --hard upstream/master
- If need delete everything(just like re-clone): git clean -x -d -f
~ vs ^
- Both ~ and ^ on their own refer to the parent of the commit.
- ~2 means up two levels in the hierarchy, via the first parent if a commit has more than one parent
- ^2 means the second parent where a commit has more than one parent (i.e. because it’s a merge)
- HEAD~2^3 means HEAD’s grandparent commit’s third parent commit.
rebase
Edit your X most recent commits interactively (squash, fixup, reword, drop)
- git rebase -i HEAD~X
Modify a specific commit
git rebase --interactive 'bbc643cd^'
# change pick to edit, Make your changes and then commit them with the same message
git commit --all --amend --no-edit
git rebase --continue
Split a commit in two with Git
git rebase -i <oldsha1>
# mark the expected commit as `edit` (replace pick in front of the line), save and close
git reset HEAD^
git add ...
git commit -m "First part"
git add ...
git commit -m "Second part"
git rebase --continue
Delete a commit
- If it is the last commit this is very straight forward. git reset HEAD
git rebase --onto <branch name>~<first commit number to remove> <branch name>~<first commit to be kept> <branch name>
# e.g to remove commits 2 & 3
git rebase --onto repair~3 repair~1 repair
Branch
- Delete local branch:
git branch -d <local_branchname>
- Delete remote branch:
git push origin --delete <remote_branchname>
orgit push origin :<remote_branchname>
- Rename local branch name:
git branch -m <new-branch-name>
Checkout remote brach
- git branch shows local branch, sometimes some remote branches are not in local even after git fetch -all (when have multiple remotes), in this case:
- which git branches are tracking which remote / upstream branch?
- git branch -vv # doubly verbose!
- How to change the remote a branch is tracking?
- git branch branch_name –set-upstream-to your_new_remote/branch_name
git log
- git log –oneline [–pretty=oneline]
Show commit id only
- git log -1 –pretty=format:“%H”
View change in a specific commit
- git show
Undo anything
Wipes your unstaged changes
- git checkout
undo rm -rf * - git stash - if no local update
undo git commit –amend
- git reset –soft HEAD@{1}
Checkout remote file after merge conflict
- git checkout HEAD the_file
shallow clone
git clone --depth 1 https://path/to/repo/foo.git -b bar
- –no–single–branch instead -b bar, to download first commit form ALL branches.
- –no-checkout(-n): No checkout of HEAD is performed after the clone is complete.
ls-remote
git ls-remote --tags remote_git_url
Tags
- git tag
git clean
- -n: –dry-run
git clean -x -d -f
git commit
- Add change to your last commit, instead of making a new commit
git commit --amend
Commit messages
<type>(<scope>): <subject>
- type: feat(new feature), fix, docs, style(format code, no code change), refactor, test(add test cases), chore
git remote
- List all remotes:
git remote
- Add another remote:
git remote add upstream the_url
git remote show
git remote show upstream
git stash
- -u|–include-untracked
- List all stashes:
git stash list
git stash
- Also stack untracked files:
git stash -u
- Apply the stash:
git stash apply <stash@{n}>
- Apply last stash and remove the stash:
git stash pop
git stash clear
git checkout <stash@{n}> -- <filePath>
git diff
- Ignore the white space:
git diff -w
- Show local staged change(added but not committed):
git diff --cached
- Show committed but not pushed change:
git diff origin/master..master
emoji
Misc
- Show recent changes:
git whatchanged --since='1 weeks ago'
- git status - show tracked files only
git status -uno(--untracked-files=no)
- Delete all stashes:
git stash clear
- git add –interactiveSymbolic links in Git
- The following untracked working tree files would be overwritten by merge
- The problem is that you are not tracking the files locally but identical files are tracked remotely so in order to “pull” your system would be forced to overwrite the local files which are not version controlled.