JudyJiang
JudyJiang

Reputation: 2227

git how to push the change of "master" to a branch

Currently I'm working on the master and made some changes. Instead of just push my current working I wanted to push all the things to a branch (Cause several people are working on the same thing, don't want to make a fuss). So following is what I did:

git branch <branch_name>
git push origin <branch_name>
git checkout <branch_name>

What should i do to add the current master changes to the branch? (And leave the master unchanged!!!)

Upvotes: 1

Views: 2968

Answers (5)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40041

If you have already committed changes to your master (by misstake) which should go to a branch instead I see two alternatives.

1. It's fine to create a new (remote) branch with these changes)

In this case you can just create branch from your current master git checkout -b some_new_branch and push that git push origin HEAD:some_new_branch and so creating a new remote branch for others to work in.

After doing that you need to move your local copy of master back to where it should be (ie before any local changes where commited) git checkout master; git reset --hard origin/master.

2. You need to add the changes to an existing (remote) branch

In this case you first need to checkout the branch git checkout some_old_branch and then cherry-pick or possibly merge all changes from master that where committed there by misstake. To find them you do git log in master and write down all hashes for the commits, then for each you do git cherry-pick <some commit> into the branch. After that you basically do the same as alternative one, push from the branch and reset your master.

Upvotes: 2

rdegges
rdegges

Reputation: 33844

Specifically, you should do this:

$ git checkout master # start on the master branch
$ git checkout -b new-branch # this will create a new branch that is a duplicate of master
$ git push origin new-branch # this will push your new branch to your git repo
$ git checkout master # you are now back on master, which is the same as you left it!

Upvotes: 0

K S Nidhin
K S Nidhin

Reputation: 2650

Since you are in master branch and all your current changes are in master branch , you need to the following steps.

  1. git status (this will list all your changed files and also shows the current branch as master).
  2. git stash save "message" (save your changes).
  3. git pull origin master (pulls the latest version from remote).
  4. git checkout -b "new_branch" (creates a new branch with lastest remote master version).
  5. git stash apply stash@{0} (applys your changes to this branch).

resolve any conflits

  1. git commit
  2. git push origin new_branch (pushes your changes wrt pulled version and the new branch to remote)

Upvotes: 1

matt
matt

Reputation: 535717

What should i do to add the current master changes to the branch

It is not clear what the phrase "the current master changes" is supposed to mean.

  • If you mean unstaged, uncommitted changes in the working copy, just add and commit. You are now on the branch (because you checked it out), so the new commit will be on the branch.

  • If you mean you made some commits on the master that you wish were on the branch and not on the master, then (since you have already created a branch from the last commit) check out master and reset --hard back to the last commit that you want to leave on the master. Then check out the branch again and resume working.

By the way, this part of your move is a bit odd:

git push origin <branch_name>

That does "make a fuss", since now the remote repo contains a branch that you presumably intended for your private work in progress.

Upvotes: 0

Ren&#233; H&#246;hle
Ren&#233; H&#246;hle

Reputation: 27305

If you haven't commited the changes make a new branch and change to the branch. Your current changes will be available in the new branch and you can commit them in the new branch.

git branch new_branch_name
git checkout new_branch_name

If you have already commited your changes in the master branch make a new branch from master and revert/reset your master branch to the last origin state.

Upvotes: 1

Related Questions