git push to specific branch
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
Solution 1:
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 just do it:
git push origin localBranchName:remoteBranchName
Solution 2:
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.
Solution 3:
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.
Solution 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 calledorigin
on remote-branch calledamd_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 usegit 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.