5

While we are working in different branches in git repo is it possible to accidentally push your changes from a branch say demo to master. I do know in order to bring the changes to master branch we have to do a merge the require branch to the master.

Sometimes when I push to a branch I have these doubt if I'll push it to master accidentally so just to clear that up...

Nithin P.H
  • 671
  • 1
  • 9
  • 29
  • 1
    What do you mean by `push to a branch`? – tkausl Aug 20 '18 at 10:22
  • Possible duplicate of [How git push other branch to remote/origin?](https://stackoverflow.com/questions/16166713/how-git-push-other-branch-to-remote-origin) – phd Aug 20 '18 at 10:43
  • 1
    My understand is that you cannot push to the wrong branch in your remote by accident - unless of course you add in the relevant flags to forcefully do it. – BenKoshy Aug 20 '18 at 10:56
  • 1
    We need to know exactly what you expect to happen when you "push changes to master branch" means. – Lasse V. Karlsen Aug 20 '18 at 11:17

3 Answers3

8
 git push origin  local-branch-name:remote-branch-name

In this case, it should be:

 git push origin  demo:master

If you're using gitlab/github, you can set master branch as protected to avoid pushing some branch to master accidentally

XX 吕
  • 186
  • 1
  • 8
1
git push origin master 

This pushes any branch you are on to the master branch in the origin repository.

the shortcut

git push

only works, if the current branch is linked to some remote branch via --set-upstream. For example with

git push --set-upstream origin master 

Note: There are more ways to set an upstream branch


It is perfectly possible to accidentally push to a wrong branch. For example when you were tracking a wrong branch, or because there was a typo in the push-command, or because you forgot what branch you were on.

wotanii
  • 2,470
  • 20
  • 38
1

No need to worry

Because if you are in other branch you made changes and unfortunately pushed to master

Ex: Your in slave branch you pushed to master (git push orgin master)

It will show Everything up-to-date

If you push recursively also (git push -u origin master) master branch won't affect (If you are in other branch only)

Master branch wont change until unless you merge with other branch's or till changes made in master

                                      **cool**
sachin_ur
  • 2,375
  • 14
  • 27