I am new in git
and I am practicing. I created a local branch but I saw that when I did git push
my branch was not uploaded to the repository. I had to actually do: git push -u origin --all
.
Why is this? Isn't a branch a new change to be pushed by default? Why do I need to run the second command?
The actual reason is that, in a new repo (git init), there is no branch (no master
, no branch at all, zero branches)
So when you are pushing for the first time to an empty upstream repo (generally a bare one), that upstream repo has no branch of the same name.
And:
- the default push policy was '
matching
' (push all the branches of the same name, creating them if they don't exist), - the default push policy is now '
simple
' (push only the current branch, and only if it has a similarly named remote tracking branch on upstream, since git 1.7.11)
In both cases, since the upstream empty repo has no branch:
- there is no matching named branch yet
- there is no upstream branch at all (with or without the same name! Tracking or not)
That means your local first push has no idea:
- where to push
- what to push (since it cannot find any upstream branch being either recorded as a remote tracking branch, and/or having the same name)
So you need at least to do a:
git push origin master
But if you do only that, you:
- will create an upstream
master
branch on the upstream (now non-empty repo): good. - won't record that the local branch '
master
' needs to be pushed to upstream (origin
) 'master
' (upstream branch): bad.
That is why it is recommended, for the first push, to do a:
git push -u origin master
Or, using Git 2.37 and the new global option push.autoSetupRemote
:
git config --global push.autoSetupRemote true
git push
That will record origin/master
as a remote tracking branch, and will enable the next push to automatically push master
to origin/master
.
git checkout master
# Git 2.23+
git switch master
git push
And that will work too with push policies 'current
' or 'upstream
'.
In each case, after the initial git push -u origin master
, a simple git push will be enough to continue pushing master
to the right upstream branch.
git push
also expects the branch to already exist? –
Suffumigate simple
': push to any recorded upstream branch, if that upstream branch has the same name as the local one. A simple git push
will be enough. –
Johppah git push origin master
for the first time, how do i subsequently mark origin/master
as a remote tracking branch? Can i still use git push -u origin master
when the remote branch already exists and has been pushed to before? –
Ceilidh git push
. It proposes git push --set-upstream origin master
, which is the long version for git push -u origin master
. –
Softspoken git push --set-upstream origin new_branch
or git push -u origin new_branch
for short. The -all
that the questioner used bypassed naming a specific new branch by including all branches. This is covered by +Klas Mellbourn in his answer. –
Blackfellow You don't, see below
I find this 'feature' rather annoying since I'm not trying to launch rockets to the moon, just push my damn branch. You probably do too or else you wouldn't be here!
Here is the fix: if you want it to implicitly push for the current branch regardless of if that branch exists on origin just issue this command once and you will never have to again anywhere:
git config --global push.default current
So if you make branches like this:
git checkout -b my-new-branch
and then make some commits and then do a
git push -u
to get them out to origin (being on that branch) and it will create said branch for you if it doesn't exist.
Note the -u bit makes sure they are linked if you were to pull later on from said branch. If you have no plans to pull the branch later (or are okay with another one liner if you do) -u is not necessary.
git push -u
–
Blintze Output of git push
when pushing a new branch
> git checkout -b new_branch
Switched to a new branch 'new_branch'
> git push
fatal: The current branch new_branch has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin new_branch
A simple git push
assumes that there already exists a remote branch that the current local branch is tracking. If no such remote branch exists, and you want to create it, you must specify that using the -u
(short form of --set-upstream
) flag.
Why this is so? I guess the implementers felt that creating a branch on the remote is such a major action that it should be hard to do it by mistake. git push
is something you do all the time.
"Isn't a branch a new change to be pushed by default?" I would say that "a change" in Git is a commit. A branch is a pointer to a commit. To me it makes more sense to think of a push as something that pushes commits over to the other repositories. Which commits are pushed is determined by what branch you are on and the tracking relationship of that branch to branches on the remote.
You can read more about tracking branches in the Remote Branches chapter of the Pro Git book.
fatal
but I had already done a commit in the branch.Does this matter? –
Suffumigate git push -u origin
copied it over to the remote repository. –
Mistymisunderstand fatal
msg like the one that you mention in the answer.Does this difference depend on the fact that I committed something to the branch? –
Suffumigate fatal
message. I would guess that the difference depends on exactly what git implementation you are using. My output is from 1.8.1.msysgit.1 running on Windows 8. –
Mistymisunderstand fatal
message even if I make a commit. –
Mistymisunderstand checkout -b name
and commit a file my HEAD
is in name
.If I do git push
I get: warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the current behavior after the default changes, use: etc
and then Everything up-to-date
–
Suffumigate git config --global push.default simple
. The reason you got Everything up-to-date
must be that there already was a name
branch on the remote that is now tracked by your local name
branch. –
Mistymisunderstand fatal
? –
Suffumigate I couldn't find a rationale by the original developers this quickly, but I can give you an educated guess based on a few years of Git experience.
No, not every branch is something you want to push to the outside world. It might represent a private experiment.
Moreover, where should git push
send all the branches? Git can work with multiple remotes and you may want to have different sets of branches on each. E.g. a central project GitHub repo may have release branches; a GitHub fork may have topic branches for review; and a local Git server may have branches containing local configuration. If git push
would push all branches to the remote that the current branch tracks, this kind of scheme would be easy to screw up.
It might represent a private experiment
.Ok but what is the big deal? The "main" branch that everyone is working i.e. master
is not affected. Unless you mean to keep the source code hidden 2)git push, without a remote, pushes to the current branch's remote
I lost you here :( –
Suffumigate git fetch
hundreds of half-working branches every time. 2) I'm referring to git push
's default behavior. It pushes to the remote that the current branch is tracking, if any. –
Syrupy HEAD is short for current branch so git push -u origin HEAD works. Now to avoid this typing everytime I use alias:
git config --global alias.pp 'push -u origin HEAD'
After this, everytime I want to push branch created via git -b branch I can push it using:
git pp
Hope this saves time for someone!
At first check
Step-1: git remote -v
//if found git initialize then remove or skip step-2
Step-2: git remote rm origin
//Then configure your email address globally git
Step-3: git config --global user.email "[email protected]"
Step-4: git initial
Step-5: git commit -m "Initial Project"
//If already add project repo then skip step-6
Step-6: git remote add origin %repo link from bitbucket.org%
Step-7: git push -u origin master
I just experienced a further permutation of this issue.
I had a branch named feat/XYZ-1234-some-description
because I was working on Jira issue 1234. During the work I created a new Jira issue to track a smaller piece of work, and when I came to push I decided to push to a branch name with this new issue number in:
git push -u origin feat/XYZ-5678-a-different-description # failed
This gave me the error being discussed in this SO thread. But since I was trying to push to a different branch name from my current branch, my problem was different to the one described here. I ended up renaming my local branch before I could push it:
git branch -m feat/XYZ-1234-some-description feat/XYZ-5678-a-different-description
git push -u origin feat/XYZ-5678-a-different-description # now works
After a bit more reading around I realised that I could have set a src
on the
git push
, either to the current branch name, or just HEAD
if appropriate:
git push -u origin feat/XYZ-1234-some-description:feat/XYZ-5678-a-different-description # also works
If you enable to push new changes from your new branch first time. And getting below error:
*git push -f
fatal: The current branch Coding_Preparation has no upstream branch.
To push the current branch and set the remote as upstream, use
git push -u origin new_branch_name
** Successful Result:**
git push -u origin Coding_Preparation
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 599 bytes | 599.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'Coding_Preparation' on GitHub by visiting: ...
* [new branch] Coding_Preparation -> Coding_Preparation
Branch 'Coding_Preparation' set up to track remote branch 'Coding_Preparation' from 'origin'.
© 2022 - 2024 — McMap. All rights reserved.
push.default
, seeman git-config
). If you dogit config --add push.default current
, thengit push
will automatically create the branch in the remote repo if necessary. Why this is not the default is explained in the answers. – Sequestercurrent
' and 'upstream
', see my older answer https://mcmap.net/q/13444/-git-push-current-vs-push-upstream-tracking. – Johppah