gitgitbucket

Will master branch have changes if I will push my new created branch(see details)?


I was working on some quick update ticket. We decided to do this in master branch and push code directly to repository. But I end up doing more than quick update and we realize that this will effect many places. I have committed my code to master branch locally while working on update. And now, we decided that we cannot push this code directly to master because we end up not having enough time to test. We will create separate branch and push this branch with this changes and after release will merge them and do our testing.

My question - if I will create new branch from my current master branch and will push that new branch, will this effect our master branch or not? (because I have committed to the master branch locally)

I'm going to follow this steps:

git branch upgrade_branch
git checkout upgrade_branch
git add (all upgrade work)
git commit -m "upgrade for xxxx"
git checkout master
git push origin upgrade_branch

Thank you in advance!


Solution

  • As long as you avoid asking the upstream (origin) Git repository to update its master, you will be fine. The steps you have outlined should work well. You might, however, want to adjust your own master branch name back once you have created the new branch, just so that you can't ask the origin repository to adjust its master.

    This particular sequence:

    git branch upgrade_branch
    git checkout upgrade_branch
    

    can be shortened to:

    git checkout -b upgrade_branch
    

    You can then do your add and commit as you outlined. After you do:

    git checkout master
    

    though, you can run git status. If this says that your master is ahead of origin/master, you can use git reset --hard origin/master to rewind your own master back to the same commit as identified by your origin/master. That will mean that even if you accidentally run git push origin master, nothing will happen.

    If your push.default is set to simple (this is the usual case), you will need to use:

    git push -u origin upgrade_branch
    

    initially to create the new branch upgrade_branch on the Git over at origin.

    Note: the way to understand this is that every Git has its own branch names. Your branch names need not be the same as theirs. Normally, you will use the same branch names that they will, because it's very confusing to remember that your branch name fred corresponds to their branch wilma, your barney goes with their betty, and so on—but in principle, at least, your names and their names are entirely different. So even if you've changed your master around, that has no effect on their master unless and until you have your Git call up their Git and ask them to change theirs.

    This is what git push is about: your Git calls up some other Git, gives them new commits if needed, and then asks or tells them to change some of their names. The names you ask or tell them to change are up to you, at git push time, and git push origin update_branch asks them to change (or create) their name update_branch based on your name update_branch.