Git tips and tricks you forgot about
Git log with names of branches and tags
As default git log
doesn’t give you information about branches and tags. If you need such info you must add –decorate argument to git log
command, for example:
1 2 3 4 5 6 7 8 9 10 11 12 |
$ git log --oneline --decorate 9bfa493 (HEAD, origin/1.3.x, r, 1.3.x) [#1581] Close input field for authenticityToken ef811e8 [#1577] Remove e.printStackTrace() 6359253 [#1570] Allow setting of SSL ciphers as configuration option 89f1deb remove dead code d795bee Hide language menu on module page 601ead1 [#1493] docviewer cosmetic changes 637f33a Fixed wrong declaration of jquery version 76a624d Fixed wrong version of jquery d1b66e4 [#1493] Done on i18n of docviewer 6d4ee8e Merge pull request #512 from xael-fry/lighthouse-1524-patch |
Autotracking remote branch when pushing
What is tracking? Tracking is a link between a local and remote branch. If you trackremote branch you can use git pull
and git push
without any extra arguments and git will know what to do.
You just add -u
parameter when pushing to do this
1 2 3 |
$ git push -u origin master Branch master set up to track remote branch master from origin. |
However, git push
pushes all branches that have the same name on the remote. you can limit this behavior to just the current branch. You need to set configuration:
1 2 |
$ git config --global push.default tracking |
Configuration entry above prevent accidental pushing into branches which you’re not ready push yet.
Tracking remote branch that you haven’t it already in local
If in the remote repository is branch, that you haven’t on local already. You can easy push it with tracking option:
$ git checkout -t origin/develop
In this case you make local branch develop
from the remote repo with tracking option.
Fetch few branches at once
You can change your default behavior that fetch commits from remotes. For instance you can define list of remotes to be fetched by the remote update
command. You have to change you config a little, for instance if you want fetch origin
, and staging
branches at once, you need:
1 2 3 |
$ git config remotes.default 'origin staging' $ git remote update |
You can also group remotes to simplify commands.
1 2 3 |
$ git config remotes.my_group 'remoteA remoteB' $ git update my_group |
Finding branch which contains commit
Just use:
1 2 |
$ git branch --contains d94d164 |
This command filter the list of branches and display only those which have the given commit among their ancesors. It work also when you display all branches with -a
parameter.
The cherry command
Cherry command is useful if you want to find changes from current branch which are already present upstream. Shorter – cherry is a command that find which commit was cherry-picked from one branch to another.
1 2 3 4 5 6 |
$ git cherry -v master + 48c0042b [#871] Locale for a request is now lazy resolved - d2006c70 [#877] throw NPE with explanation when trying to resolve Message with '&' in template when key resolves to null - 0404c3f9 [#904] Read play id from sytem properties in JUnitTestrunner |
This command compares changes on the current branch to upstream („master”). If changes are present at both are marked with „-„, changes still missing from upstream are marked with „+”.
A little shorter status output
As default git use verbose
status output. It’s fine for beginners, but if you feel more comfortable with git, you no need anymore all the info the verbose
provide. You can use short status output instead.
1 2 3 4 5 |
$ git status -sb ## 1.3.x...origin/1.3.x M framework/src/play/mvc/ActionInvoker.java |
Find the commit by message text
1 2 3 4 5 |
You can find commit which message matches regexp pattern For example if you want find the commit that was `fixme` in it message you should use command: $ git show :/fixme |
or find last merge commit:
1 2 |
$ git show :/^Merge |
Highlight diff changes by words, not by lines
If you use --word-diff
as argument for diff
command, all changes will be displayed as words instead of lines.
1 2 |
$ git diff --w ord-diff |
Other commands that take diff flags can use this argumes as well. For instance: git log -p
and git show
.
Using references to checked out branches history
Git has a special syntax for checked out history.
@{-n}
means „n-th branch checked out before current one”. When we checkout develop
from master
– @{-1}
is reference to master
branch. After rebasing, we need to use @{-2}
to checkout master because @{-1}
is a reference to the same branch.
Rename branch name
You can rename local branch name with command:
1 2 |
$ git branch -m current_name new_name |
But what with the remote?
1 2 3 |
$ git push origin :old_name $ git push origin new_name |
But what about extremely rare situation where you need rename master branch. You need to use commands as follow:
1 2 3 4 5 6 7 8 9 10 11 |
#first change local name $ git branch -m master master_old #remote remote master branch $ git push remote :master #make master_old in remote $ git push remote master_old #make new master in local $ git checkout -b master branch_you_make_new_master_from #make master in remote $ git push remote master |
Add notes to commits
Git notes are annotations for existing commits. You can add notes to any existing commit without aware they don’t change the history. Notes are stored only in your repo, but there are few very interestign ideas how use them.
1 2 3 |
$ git notes add #suddenly editor is opened and you need to edit the note!!! |