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:

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

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:

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:

You can also group remotes to simplify commands.

Finding branch which contains commit

Just use:

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.

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.

Find the commit by message text

or find last merge commit:

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.

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:

But what with the remote?

But what about extremely rare situation where you need rename master branch. You need to use commands as follow:

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.