How to move unpublished commits from one branch to another?

how to move code from one branch to another in git
git move commit to another branch
git move pushed commit to another branch
git move commits onto a new branch
git move local commit to another branch
git move commit to existing branch
git move pending commits to another branch
git move last n commits to another branch

I have committed my changes on my local master and was about to create a new feature branch for these commits. The local master is up-to-date with the master on my remote. I forgot that the branch already exists both locally and on my remote, so this happened:

git checkout -b myFeatureBranch
fatal: A branch named 'myFeatureBranch' already exists.

The branch is based on an old master and includes couple of commits.

My goal is to get rid of the old commits on that branch, rebase the feature branch on the current master and apply my new commits that are on my local master branch.

What's an elegant way to do this? I think a not so elegant way would be to:

  1. Checkout the old feature branch
  2. Rebase it on origin/master
  3. Replace the files I have edited with the most recent version of themselves (the version of my local master)
  4. force push (Im the only one working on that branch)
  5. 'force pull' master to get rid of my changes that I now have on the feature branch

I'm on git version 2.18.1 (most recent available release on Centos 8) and prefer to use the CLI.

You could just try resetting your feature branch to master:

# from myFeatureBranch
git reset --hard master

After this step, your feature branch would be starting off at whatever point in which the local master branch happens to be.

Note that you would then need to force push your reset feature branch to the remote via:

git push --force origin myFeatureBranch

How to move unpublished commits from one branch to another , Whatever the case may be, let's look at how you can move a commit off one branch and onto another so that it no longer appears in the history of the original � Whatever the case may be, let’s look at how you can move a commit off one branch and onto another so that it no longer appears in the history of the original branch. We will look at how to move the commits to a new branch as well as moving them to an existing branch. The process for moving work off one branch and onto a newly created branch

My goal is to get rid of the old commits on that branch, rebase the feature branch on the current master and apply my new commits that are on my local master branch.

I think the first thing to do is clear up some confusion here. If you've removed the commits from the feature branch, then there is nothing to rebase. rebase is something you do to commits; it means "make a commit (usually with a different parent than the old commit) whose patch relative to its parent is the same as the old commit's patch relative to its parent".

When people say they are rebasing a branch, they mean they are rebasing the commit(s) from the end of that branch, and then moving the branch to the new copies of the commits.

And to be fair, you can use the rebase command without rewriting any commits, in which case it just moves the branch - which works out conveniently in cases where you don't want to worry about whether there are any commits to rewrite or not.

But it's still worth understanding these details, because if you realize that you just want to move the branch without any commits, there are easier ways to do it. (You don't even have to explicitly delete the old commits; it's fine to just leave them behind when moving the branch, if they are in fact no longer needed.)

The one caveat is that if you remove the old commits from the branch (whether by rebasing - which replaces those commits with new copies - or by just moving the branch or by any other means) then all other repos have to deal with an "upstream rebase" condition (see the git rebase docs) and, if they do it wrong, they may undo your work.

That said, the simplest thing to do is to replace your checkout -b command with branch -f - i.e. instead of creating the branch, force it to move. With the new commit checked out (just as it would've been for the checkout -b):

git branch -f myFeatureBranch
git push -f
# all other clones must now recover from upstream rebase

How To: Move Git Commits From One Branch to Another, Moving a commit from one branch to another. So, you have a commit which is in the wrong place and you want to move it from one branch to another. In order to� Moving a Commit to Another Branch in Git. By Mike Farmer June 21, 2012 Perhaps you’ve made the same mistake I have. You’re right in the middle of developing a feature when a request comes up to fix a different completely unrelated problem. So, you jump right in and fix the issue and then you realize you forgot to start a new git feature branch.

A. If you need your old branch commits, then you can simply create another branch instead of myFeatureBranch:

git checkout -b myFeatureBranch2

And then you can push your new branch and do what you want with that branch (eg. give a pull request to master).

B. But if you don't need your old branch commits (neither locally or remotely) you can delete old branch and then use that branch name:

git push -d origin myFeatureBranch
git branch -d myFeatureBranch
git checkout -b myFeatureBranch

C. If you have some useful commits on old myFeatureBranch, and don't want to miss them, then you can go to that old branch and rebase it with master (containing new commits) and push it:

git checkout myFeatureBranch
git rebase -i master
git push -f
git checkout master
git fetch origin
git reset --hard origin/master

On undoing, fixing, or removing commits in git, Solution – Move the local commits to a new branch In other words, any file you modified and did not commit or push, they will be reverted� Use the information in the Commit Details area to make sure these are the changes you want to transfer to another branch. Click Cherry-pick on the toolbar. WebStorm will display the Commit Changes dialog with the automatically generated commit message.

git: How to move locally committed (but not pushed) changes to a , I cannot remember to create a branch first before I start making commits on master. I'll get halfway through whatever edits and I'm like "d'oh! I forgot a branch" so� Step 4: Pull files and history from this branch (containing only the directory you want to move) into repository B. git pull repo-A master --allow-unrelated-histories # This merges master from

Move unpublished commits on master to new branch � Issue #1021 , Unfortunately the original branch is not ready to be merged and you need to promote your branch to a first class citizen, branched off of master. Delete Unpublished Commits. If you haven't yet published your commits to a remote repository, like GitHub, then you can essentially delete previous commits by using the reset command. While this is an effective solution, it's a dangerous one since you're rewriting history and leaving the "deleted" commits unreferenced, or "orphaned".

How to move commits from one branch to another with git rebase onto, “Git move unpushed changes to a new branch” is published by Pete Houston. the current code should be developed under a different branch rather than If you haven't push the commits to server, then this is how to do it. Example: Moving up to a few commits to another branch. Branch A has commits (X,Y) that also need to be in Branch B. The cherry-pick operations should be done in the same chronological order that the commits appear in Branch A. cherry-pick does support a range of commits, but if you have merge commits in that range, it gets really complicated

Comments
  • Then I would need to do a force push to overwrite origin/myFeatureBranch, right?
  • True there is no point in rebasing if I want to get rid of those commits anyways. I guess my gut just wanted to put off discarding old commits as long as possible.