pavjel
pavjel

Reputation: 486

Git add branch starting from N commits before latest commit

I'm working on master branch, lets I've made like 10 000 commits, and what if I want to remove all the commits starting from 9876-th commit and place them into separate branch (of course branch should include my master branch commits already done before making this new branch)?

P.S. The commit count isn't real, I'm just asking what should I do if it'd be an actual commit count?

Upvotes: 1

Views: 86

Answers (1)

wspurgin
wspurgin

Reputation: 2733

If you know the start of the commit hash of the 9876th commit, you can checkout that individual commit. For example:

git checkout 8f3faf -b branch_name

Or if you know how many commits you want to go back you can branch from HEAD. Using your example you'd want to go back 124 commits

git checkout HEAD~124 -b branch_name

In this particular scenario, we want all the current commits on master to be in a separate branch. Then we want to roll back master N-commits.

On master:

git checkout -b branch_name
git checkout -
git reset --hard HEAD~N

Now master will be reset back N commits, and the new branch branch_name will have those N commits ahead of master.

As an aside: The git checkout - is a shorthand to checkout out the last branch (in this case the master branch) you were on (similar to the cd - command which does the same thing but with directories on the filesystem).

Upvotes: 2

Related Questions