git-worktree is awesome
I’m still mad I haven’t found this out before. Imagine you’re working on …
Git has a lot of features, and I bet that 90% of who use it (including me) doesn’t know half of them. Well, maybe, someday, one of those “unknown features” can “save your life”.
So, I decided to compile a small list of useful git commands, tips and tricks that I use or used sometime in my work.
Of course, if you have yours tricks/tips (and you probably have), comment above the post or ping me at twitter/mail/anything, I’ll be glad to add your tip and put your name in it.
Let’s do this.
$ git blame path/to/file
This will show the log of your local operations in the git tree, useful to get revision code of a specific action (see next item).
$ git reflog
reflog will only have the logs of actions in your local repository, more specifically, it will log each time the
HEAD pointer changes, so, if clone a repository right now, the reflog will be empty. Also, the reflog is maintained for 30 days by default.
$ git rev-parse HEAD # last commit $ git rev-parse HEAD~5 # 5 commits back from last commit $ git rev-parse develop # last commit from develop branch
$ git log path/to/file # to check the revision code $ git checkout revision_code path/to/file
$ git checkout HEAD path/to/file
$ git checkout HEAD~3 path/to/file
$ git show HEAD~3:path/to/file > path/to/file_3_commits_ago
According to the docs:
Create a shallow clone with a history truncated to the specified number of revisions. A shallow repository has a number of limitations (you cannot clone or fetch from it, nor push from nor into it), but is adequate if you are only interested in the recent history of a large project with a long history, and would want to send in fixes as patches.
So, if you want only the last revision:
$ git clone https://github.com/twitter/bootstrap --depth 1
This can drastically reduce the clone. The bad thing is that you will have NO HISTORY BEFORE THE HEAD WHEN YOU DO THE CLONE.
This is pretty useful when you’re working on something, commit, and them realize that it would fit better in another branch (or that you’re in the wrong branch)… this is how to move it to another branch.
$ git branch new $ git reset --hard HEAD~1 #go back 1 commit, YOU WILL LOST UNCOMMITED CHANGES $ git checkout new
Useful when you do some crap and want to throw it all away.
$ git reset --hard HEAD
You can also specify something like
HEAD~3 to get back 3 commits from HEAD.
$ git clean -df
This is useful when you want to switch branches, but don’t want to commit a half-done work just to get back to it later.
$ git stash # save the current state # later... $ git stash pop # apply the stash to your current HEAD and remove it from your stack.
More info can be found here.
This will do the merge without creating the “merge commit”, acting much like SVN.
$ git pull --rebase
You can setup aliases in git, so get smaller commands and save time. Examples:
# configuring... $ git config --global alias.co checkout $ git config --global alias.s status # using $ git s $ git co https://github.com/caarlos0/up
Useful if you want a file that was introduced or modified in other branch. Example:
$ git checkout gh-pages $ git checkout master -- teste.js $ git commit -m "Update test.js from master"
Show the commit log with the deleted files for each commit:
$ git log --diff-filter=D --summary
$ git branch -d name $ git branch -D name # force delete unmerged branch
$ git push origin :branchname
If you do a wrong commit, revert it, but maintain the changes in your staging area.
$ git reset --soft HEAD^
HEAD^ is a pointer to the parent of current
HEAD, so, you can use
HEAD~3 to go 3 commits back, for example. Also, check the
reflog part of this post.
PROTIP: Don’t do this if you already pushed the commit.
For example, if you want to remove the
$ git rm wrongfile.txt $ git commit --amend
cding each module,
checkout master and
pull, in git 1.8.2 you can simply do this:
$ git submodule update --remote --merge
This is useful if you have a file in two different repos for whatever reason, or if you have 2 very similar files in the same repo:
$ git diff HEAD^ -- hello.test > /tmp/patch $ patch -p1 hi.test /tmp/patch
! c15fe9e..eee5f38 feature1 -> origin/feature1 (unable to update local ref)`
This error can vary by remote and branch name. Usually, a clean up will do the job just fine:
$ git gc --prune=now $ git remote -v | cut -f1 | uniq | xargs git remote prune
I strongly recommend the reading of the “Pro Git” book. The book will teach tons of things: basics, importing SVN projects, advanced, branching, servers, and git internals.
Let’s make this list bigger! Have your own tip/trick? Something I forgot to add? Share it with us!