Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move git commit to new branch

With git I committed some changes and now would like to have them in a new branch. How to acchieve this is explained in several places, for example here

My confusion is more related to the local/remote aspect of this task.

I started by forking a repository on github to myForkOnGitHubRepo. Then I cloned that repository to my local PC:

local: git clone myForkOnGitHubRepo

Then I changed the source, commited and pushed:

local: git add .
local: git commit
local: git push # (actually I used the minGW Gui for that one since the command line push issued a warning)

Now my local changes are visible in myForkOnGitHubRepo. Fine. And now I note that I would have preferred to use a new branch (and to have the change in that new branch at myForkOnGitHubRepo, too).

Is it correct that I can just follow the instructions in the link I posted in the beginning, i.e.

local: git branch newbranch
local: git reset --hard HEAD~1 # only one commit was done

and then -- well, now what? Do I just have to push again? Or do I need to push the new branch explicitly? (Sorry if that's very basic, I never used this before).

And is there anything else I need to do before creating the newbranch? My understanding is that, after the push, local and remote are in the same state, is that correct?

like image 936
Thomas Avatar asked Mar 10 '23 21:03

Thomas


1 Answers

local: git branch newbranch
local: git reset --hard HEAD~1 # only one commit was done

Now, do force(-f) push to remote/master as git history is changed.

$ git push -f origin master

Checkout to newbranch and push newbranch also.

$ git checkout newbranch
$ git push origin newbranch

  • Common scenario is master branch should keep working or not broken. So, when need to work on a new feature, create a new branch(say, feature) from master.

    $ git checkout master
    $ git checkout -b feature 
    
  • Work on feature branch. When done with feature branch add, commit and push to remote.

    $ git add.
    $ git commit -m 'message'
    $ git push origin feature
    
  • Now, if all test is ok with feature branch then create Pull request or merge with master

    $ git checkout master
    $ git pull origin feature   # pull = fetch + merge 
    $ git push origin master    # update remote/master
    

My understanding is that, after the push, local and remote are in the same state, is that correct?

Yes, when you push to remote, your local & remote are in sync (same data)

Now, if you need the changes/commits of feature branch (assume feature is not merged with master yet) in another branch (say, dev) then, just create a branch (dev) from master and pull feature branch into dev

$ git checkout master
$ git checkout -b dev

$ git pull origin feature     # pull 'feature' into 'dev'

# do changes here

$ git commit -am 'Added new feature'     # add & commit
$ git push origin dev
like image 124
Sajib Khan Avatar answered Mar 20 '23 04:03

Sajib Khan