1

From some answers I found(example), I understand that if I do git push, only the current branch commits will be pushed. However when I type 'git push`, I get the following output (partial):

. . . 
Writing objects: 100% (12/12), 999 bytes | 0 bytes/s, done.
Total 12 (delta 4), reused 0 (delta 0)
To https://gitlab.bla-bla.git
   773f335..db28181  doedev_userAgentTest -> doedev_userAgentTest
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitlab.bla-bla.git'
. . .

My current branch is doedev_userAgentTest:

$ git status
On branch doedev_userAgentTest

So what I understand from the above output is that the commits to the current branch were made correctly (I confirmed that the file I have committed is actually in the remote repo) but there was an attempt to push master branch and it failed.

So my questions are (disregard the error while pushing to master):

  1. Am I right in my assumption that there was a push to master made despite the fact I was pushing while on different branch?
  2. If the answer is yes, how do I push only from current branch?

NOTE: I do have all the above-mentioned branches remotely.

Community
  • 1
  • 1
Eugene S
  • 6,709
  • 8
  • 57
  • 91

2 Answers2

2

Git pushes from the branch it has a remote branch for.

To push your current branch, use this syntax:

git push origin mybranch

If your remote isn't named origin, then use the correct name.

Also, please note that this behavior depends on your git configuration. More information here: Does "git push" push all commits from other branches?

Community
  • 1
  • 1
blue112
  • 52,634
  • 3
  • 45
  • 54
1

The behavior of git push without extra options depends on the git config.

The push of master is rejected because the remote master has been updated by others. You could run git pull origin --rebase master to update your local master first.

If you want to push master only, git push origin master:master could always be a safe run.

Updated: if you want to push doedev_userAgentTest only, you could run git push origin doedev_userAgentTest:doedev_userAgentTest. If doedev_userAgentTest is checked out, git push origin HEAD:doedev_userAgentTest also works. And in the former case, :doedev_userAgentTest could be omitted.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53