Git Tips Tricks for Developers


Untitled

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
Split a commit in two with Git
Delete a commit
  • If it is the last commit this is very straight forward. git reset HEAD

Branch

  • Delete local branch: git branch -d <local_branchname>
  • Delete remote branch: git push origin --delete <remote_branchname> or git push origin :<remote_branchname>
  • Rename local branch name: git branch -m <new-branch-name>
Checkout remote brach

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

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)