How do I get git push to do what I want?
I’ve used git
for years. And for years I forget that what I think it should do is not what it actually does.
Summary of What to do
- First, set up my default git config so it has a default for ‘git push’
git config default.push git config default.push current
Important Note: I have a .gitconfig file in myenv repo at myenv/dotfiles/.gitconfig.
So whenever possible, I clone myenv
and then create a symlink to that file as follows. Any changes I make I can then check into git which makes it
easier to update all my development environments. (e.g., laptop, VMs, Cloud, Work)
Below are the commands I use to create the symlink:
cd $HOME
ln -s myenv/dotfiles/.gitconfig .gitconfig
NOTE: In Windows I need to have admin rights and to setup fsutil to support symlinks. For details, look for the
command mklink
in this document in github.
- With Step #1 completed, then when I run git push it does what I expect it to do which is to push my current branch to the remote branch of the same name!
The reason this is needed is because it didn’t used to work this way.
Example Scenario
I’m working on a code base and have several branches. Branches are in different states of “done” but not yet deleted.
I finish up a change on a branch and I run
git push
What does it do? It used to push all of the branches that I had locally to the corresponding remote git branches.
So I was taught/told (by a more experienced git developer) to always use
git push -u origin MY-BRANCH-NAME
Later I learned that this is the equivalent of
git push --set-upstream origin MY-BRANCH-NAME
which creates a reference to this branch so future git pull
commands get data from the associated branch.
Internally, this updates your .git/config file as follows:
[branch "MY-BRANCH-NAME"]
remote = origin
merge = refs/heads/MY-BRANCH-NAME
This behavior is called “matching”
How can I push my current branch to my remote git repo?
The following will work even if you haven’t setup up git config default.push
because it tells git to push your current branch to the remote branch
(of the same name) and to set up a branch tracking entry.
git push -u origin $(git rev-parse --abbrev-ref HEAD)