180

Even after reading this question: git-push-current-branch, I am still having difficulty figuring out how I should write my git push command. As mentioned in the question link, it's not clear from the documentation.

I would like to use my 'real world' example. Following is what I see when I run the git status command on the top level of my branch:

On branch amd_qlp_tester

Your branch is ahead of 'origin/amd_qlp_tester' by 5 commits.

etc...

My branch name is amd_qlp_tester but it was "branched" off from the main branch (if I have the terms wrong it's because of my SVN background). But then there is also the name origin/amd_qlp_testser

So how do I phrase my push command?

Is it any of the following?

git push origin/amd_qlp_tester
git push origin amd_qlp_tester
git push amd_qlp_tester
git push origin
git push
code_fodder
  • 15,263
  • 17
  • 90
  • 167

4 Answers4

279

If your Local branch and remote branch is the same name then you can just do it:

git push origin branchName

When your local and remote branch name is different then you can do it:

git push origin localBranchName:remoteBranchName
Imranmadbar
  • 4,681
  • 3
  • 17
  • 30
117

git push origin amd_qlp_tester will work for you. If you just type git push, then the remote of the current branch is the default value.

Syntax of push looks like this - git push <remote> <branch>. If you look at your remote in .git/config file, you will see an entry [remote "origin"] which specifies url of the repository. So, in the first part of command you will tell Git where to find repository for this project, and then you just specify a branch.

interestedparty333
  • 2,386
  • 1
  • 21
  • 35
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • Thanks for that :), can you explain why they are two seperate "words" when the dit status description displays it as a path, i.e. why is it `origin amd_qlp_testser` and not `origin/amd_qlp_tester` – code_fodder Oct 03 '13 at 08:58
  • The default "remote" is based on the current branch's configuration (in this case it will indeed be `origin`). The branch(es) to push defaults (in current versions of git) to `:` if unset, which means `matching`, but that's supposed to change in the future. – torek Oct 03 '13 at 09:08
  • I'm a bit confused, I'm using v2.10, when I type `git push` it tries to push all tracked branches, contrary to what you said ("the remote of the current branch is the default value"). – Roberto Feb 22 '17 at 00:00
  • Git 2.x should use `simple` push strategy which means that will push only current branch. https://blogs.atlassian.com/2014/06/happened-git-2-0-full-goodies/ – Petr Mensik Mar 17 '17 at 16:05
  • `fatal: Couldn't find remote ref branch-name-here` – doug65536 Jul 15 '20 at 16:34
  • @doug65536 You need to specify a local to remote mapping using a colon `git push :` – Mark Langen May 14 '21 at 06:22
  • @PetrMensik You've said that " If you just type git push, then the remote of the current branch is the default value.". Does this means that, by specifying a name, we can name the current branch with different name in the remote? – Feng. Ma Nov 10 '22 at 16:32
10

The answers in question you linked-to are all about configuring git so that you can enter very short git push commands and have them do whatever you want. Which is great, if you know what you want and how to spell that in Git-Ese, but you're new to git! :-)

In your case, Petr Mensik's answer is the (well, "a") right one. Here's why:

The command git push remote roots around in your .git/config file to find the named "remote" (e.g., origin). The config file lists:

  • where (URL-wise) that remote "lives" (e.g., ssh://hostname/path)
  • where pushes go, if different
  • what gets pushed, if you didn't say what branch(es) to push
  • what gets fetched when you run git fetch remote

When you first cloned the repo—whenever that was—git set up default values for some of these. The URL is whatever you cloned from and the rest, if set or unset, are all "reasonable" defaults ... or, hmm, are they?

The issue with these is that people have changed their minds, over time, as to what is "reasonable". So now (depending on your version of git and whether you've configured things in detail), git may print a lot of warnings about defaults changing in the future. Adding the name of the "branch to push"—amd_qlp_tester—(1) shuts it up, and (2) pushes just that one branch.

If you want to push more conveniently, you could do that with:

git push origin

or even:

git push

but whether that does what you want, depends on whether you agree with "early git authors" that the original defaults are reasonable, or "later git authors" that the original defaults aren't reasonable. So, when you want to do all the configuration stuff (eventually), see the question (and answers) you linked-to.

As for the name origin/amd_qlp_tester in the first place: that's actually a local entity (a name kept inside your repo), even though it's called a "remote branch". It's git's best guess at "where amd_qlp_tester is over there". Git updates it when it can.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
  • 1
    Thanks for the explanation, I think I have it clear now. I like to be precise so I will stick to `git push origin amd_qlp_tester` until I have a need to do more advanced stuff :) – code_fodder Oct 03 '13 at 09:46
4

I would like to add an updated answer - now I have been using git for a while, I find that I am often using the following commands to do any pushing (using the original question as the example):

  • git push origin amd_qlp_tester - push to the branch located in the remote called origin on remote-branch called amd_qlp_tester.
  • git push -u origin amd_qlp_tester - same as last one, but sets the upstream linking the local branch to the remote branch so that next time you can just use git push/pull if not already linked (only need to do it once).
  • git push - Once you have set the upstream you can just use this shorter version.

Note -u option is the short version of --set-upstream - they are the same.

code_fodder
  • 15,263
  • 17
  • 90
  • 167