Home > Software design >  How to move multiple committed, but unpushed commits to a newly created branch, keeping all commit i
How to move multiple committed, but unpushed commits to a newly created branch, keeping all commit i

Time:11-23

Context

I have a repository with hundreds of committed and pushed commits. Now I have 10 committed but not yet pushed commit on my local git repository, I would like to create a new branch locally, and move all the 10 commits to that branch, doing all this locally. Then when I will be online next time I would like to sync my remote with that exact state. (As far as I see this case is not the same as moving committed (but not pushed) changes to a new branch after pull because that is only one single commit)

Question

Is this possible at all? If yes what is the most efficient way? (please be aware I am not a git guru)

CodePudding user response:

It is as easy as creating a new branch at the top commit.

git checkout -b your_new_branch_name

Now you are on the new branch at the same commit. If you like, you can reset your old branch (possibly master or main) to the upstream after that.

# after creating a new branch reset your master or main
# assuming current branch is master (or main)
git reset --hard origin/master (or main)

When you want to push your changes on this new branch, you can either merge master to your new branch (possibly after rebasing on master) and push, or create a pull request from new branch to upstream master.

CodePudding user response:

There are very practical solutions for your problem. There is no need to write it all here so check the following links:

Git create branch from range of previous commits?

How to copy commits from one branch to another?

How to cherry-pick a range of commits and merge them into another branch?

They worked for me, and sure, will work for you.

  •  Tags:  
  • git
  • Related