Seems that you use git

Posted on Jun 28, 2016

Intro

I used git for several years. If you don’t know git, you should. Including it in resume will increase your value.

Usually I use Intellij IDEA to manage the changes, and sometimes I use Source Tree to make commits.

With command-line, mostly I was like:

git status
git add foo
git fetch
git commit
git rebase origin/develop
git push

Soon or later, a mistake might happen. I’ll explain how to rectify.

Besides, I want to introduce something good for git.

Commands for your situations

Local operations that you regret

Like a merge, a rebase, a wrong commit…

git reflog is a powerful command.

It will output the real history of your local git. It’s real in that the history is maintained by git.

710aa34 HEAD@{0}: reset: moving to origin/master
02a3706 HEAD@{1}: reset: moving to origin/master
262d237 HEAD@{2}: rebase finished: returning to refs/heads/master
262d237 HEAD@{3}: rebase: commmit
82cb9ac HEAD@{4}: rebase: checkout origin/master

You can always use git reset --hard <hash> to set everything back to normal. Like a time machine, huh?

A wrong commit

git commit --amend, you know it, don’t you?

It’s like something everyone will need.

Hide the untracked files in git status

git status -uno, the parameter means untracked no, easy to remember.

Too many untracked files, and you want to clean them

git clean -fdx

This command can clean the ignored files to sync with server.

Always use rebase when git pull

git config branch.autosetuprebase always

You don’t need git pull --rebase anymore, just git pull, your local repo will rebase to latest remote.

I prefer rebase than merge.

Remove added files from git

git rm --cached <file>

To stop tracking a file you need to remove it from the index.

It’s true that .gitignore will prevent untracked files from being added (without an add -f) to the set of files tracked by git.

However, git will continue to track any files that are already being tracked.

Only with the command git rm --cached <file> can those files be untracked.

The removal of the file from the head revision will happen on the next commit.

Can’t wait to apply some changes in other branch

git cherry-pick <commit hash>

You can apply some changes of existing commit to your branch, remember, remove them when you rebase.

Some guy modified the file and you want to know who?

git blame <file>

git log -p <file> will show it even if the file is deleted…

Well you know it, don’t you?

Just don’t quarrel with or blame that guy, but discuss peacefully, blaming helps nothing or nobody.

Ignore the changes of a file

You don’t want to commit a file and you don’t want to revert/reset it.

Well, ignore it!

git update-index --assume-unchanged <file>

Then git will treat the file as if it’s not changed.

Bonus: you can add following code to [alias] section of your .gitconfig. If you don’t see [alias] section, just create one. Then you can use them as if they are native git commands.

ignore = update-index --assume-unchanged
unignore = update-index --no-assume-unchanged
ignored = !git ls-files -v | grep "^[[:lower:]]"
lg = log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

Repo is so big and you want to clean it

Clean up git repo

bfg-repo-cleaner

Faster than git-filter-branch to remove big files…

java -jar bfg-1.11.7.jar --delete-files *.zip myrepo.git
java -jar bfg-1.11.7.jar --delete-files *.log myrepo.git
java -jar bfg-1.11.7.jar --delete-files *.out myrepo.git
java -jar bfg-1.11.7.jar --strip-blobs-bigger-than 1M myrepo.git

Resources

Git ignore template

On Git Ignore you can find .gitignore file template for your project and/or programming language.

git extra

How extra could they be? When they are useful?

git-extra

  • git extras
  • git squash
  • git summary
  • git effort
  • git authors
  • git changelog
  • git commits-since
  • git count
  • git create-branch
  • git delete-branch
  • git delete-submodule
  • git delete-tag
  • git delete-merged-branches
  • git fresh-branch
  • git merge-into
  • git graft
  • git alias
  • git ignore
  • git info
  • git fork
  • git release
  • git contrib
  • git repl
  • git undo
  • git gh-pages
  • git scp
  • git setup
  • git touch
  • git obliterate
  • git feature|refactor|bug|chore
  • git local-commits
  • git archive-file
  • git missing
  • git lock
  • git locked
  • git unlock
  • git reset-file
  • git pr
  • git root
  • git delta

git goodies

git-goodies

git learning materials

Learn git branching with interactive and beautiful UI.