Git has become a fundamental part of our developers' daily routine that it’s hard to remember our lives without it. And yet, most of us use a limited set of commands and options. Today, I want to focus on two commands most developers probably use every day and look at the defaults behind them.
git push
After git commit
, git push
is probably the second most used command.
I don’t think I’ll teach you anything with this excerpt from the documentation:
git-push - Update remote refs along with associated objects
The default options pushes the current branch to the origin
remote;
it assumes a branch of the same name as the local branch exists on the latter.
If it doesn’t exist, you need to use -u
option to create it.
Also, the default assumes a single upstream named origin
.
If you want to push the current branch to another upstream, you need to specify it explicitly:
git push other_upstream
Likewise, the default assumes pushing to a remote branch with the same name. To push to another branch, we also must specify it along with the upstream.
git checkout my_branch
git push other_upstream master
git rebase
The default git push
options are straightforward.
The default ones for git rebase
aren’t the reason why we probably use one of them all the time.
git-rebase - Reapply commits on top of another base tip
Let’s try the git rebase
command on a simple tree:
A B C D o---o---o---o master \ E F G o---o---o branch1 [HEAD]
Nothing happens.
Or more precisely, "it depends"™.
If we didn’t set an origin
remote, git
complains.
There is no tracking information for the current branch. Please specify which branch you want to rebase against.
Imagine the following remote repo:
A B C D o---o---o---o master \ E F G H o---o---o---o branch1 [HEAD]
Let’s configure the remote and bind the local branch to the remote branch.
git fetch
git branch --set-upstream-to=origin/branch1
If we call rebase
again, git tries to apply every commit from the remote branch, starting from the root one.
Since H
doesn’t exist on the local branch, it just adds it to the tip of it.
Let’s try with master
and test again:
git rebase -i master
-i
allows rebasing interactively.
Here’s the proposal.
pick 5529dc4 E pick 93af602 F pick 7f79811 G pick c6f853b H
As per the documentation, the command switched branch, got the commits from master
, and now applies the commits in the current branch.
The result is the following:
A B C D o---o---o---o master \ E F G H o---o---o---o branch1 [HEAD]
Git is a huge beast. Most developers, including me, only use a fraction of its features. In this post, we described the default of two of the most common Git commands. I hope it sheds some light on them.