Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is git pushing to two branches in this git push?

What would possibly cause a 'git push' to try and commit to two branches? I have my own branch I'm working on, which is on the shared repo... and a master branch. Right now I just wanted to push to my personal branch which went through just fine, but it also tried to push to master and got rejected. Looked something like this:

foo$ git push
Counting objects: 38, done.
Delta compression using 2 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (21/21), 9.73 KiB, done.
Total 21 (delta 14), reused 0 (delta 0)
To ssh://example.com/project.git
   8184634..86b621e  mybranch -> mybranch
 ! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to 'ssh://example.com/project.git'  

My config looks like this:

remote.origin.url=ssh://example.com/project.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.mybranch.remote=origin
branch.mybranch.merge=refs/heads/mybranch  

Esko notes that it's pushing to both because they're in my config. What if I want to push to both, just not simultaneously? When I have mybranch checked out and I git push, I clearly mean to push mybranch and not master. There's times when I'll checkout master, edit code, and want to commit/push that also. Is there a way for both to co-exist?

like image 902
Coocoo4Cocoa Avatar asked May 06 '09 23:05

Coocoo4Cocoa


People also ask

Does git push push to all branches?

No, git push only pushes commits from current local branch to remote branch that you specified in command.

What is the problem with git force -- push?

The Risks of Git Push ForceBecause you have failed to pull those changes, they are not reflected in your local repository. In this case, if you perform a Git push force, you will replace the remote repository with a copy of your local repo, effectively deleting your team member's work.

What happens if two people git push at the same time?

If server detects a conflict when someone pushes data (and if two users are doing this "simultaneously" one of the pushes will be conflicting, because it will be applied only after the other one completes), the server will reject it, and the unlucky user shall then resolve conflicts and try to push again.

Does git push automatically push to current branch?

By default, Git chooses origin for the remote and your current branch as the branch to push. If your current branch is main , the command git push will supply the two default parameters—effectively running git push origin main .


2 Answers

When using git push without any arguments, it will push all local branches that have a corresponding remote branch with the same name. Since your local repository has branches masterand mybranch, and also the remote repository has branches masterand mybranch, then Git will push both of them.

If you want to push only one branch, you can say to Git explicitly that which branch you want to push: git push origin mybranch

If you want to push master, you can fix that error by first pulling from master. Git complains about the merge being non-fast forward, because somebody else has pushed a commit to master since the last time that you pulled from master.

like image 191
Esko Luontola Avatar answered Oct 19 '22 07:10

Esko Luontola


If you want to push only current branch, you can use "git push origin HEAD", or perhaps even "git push HEAD" (with modern git).

The default behavior if no refspec for push (you have defined refspec for fetch in your config, but not for push) is to push matching refs. From git-push(1):

git push [...] [<repository> <refspec>...]

<refspec>...

The special refspec : (or +: to allow non-fast forward updates) directs git to push "matching" branches: for every branch that exists on the local side, the remote side is updated if a branch of the same name already exists on the remote side. This is the default operation mode if no explicit refspec is found (that is neither on the command line nor ...

like image 37
Jakub Narębski Avatar answered Oct 19 '22 08:10

Jakub Narębski