Jan Müller
Jan Müller

Reputation: 423

Revert subset of changes from git branch

I have two different git branches: master and experimental. Both branches have their own changes and commits. In the experimental branch I tried out various different things (through different files and commits).

Now, I want to revert all the changes in the experimental branch and only keep one feature. I want to keep the (pruned) experimental branch and keep working on that until it is ready to be merged into master.

So far, I did a diff between the two branches (to see the changes) and did then ''git checkout master'' on the files in experimental I wanted to revert to master.

Is there a more efficient way?

What is the simplest way to revert the changes in all but a few files in the experimental branch to master?

Upvotes: 1

Views: 473

Answers (4)

Andrew2M
Andrew2M

Reputation: 248

Very similar to what janos suggested but ensure that you first add and commit all of your changes on experimental branch and then checkout master. Once on master checkout out all the files that you want the changes from your experimental branch. You will then have everything on master to which you can once again add and commit changes to. The git flow would look as follows:

branch: experimental >$ git add .
branch: experimental >$ git commit -m "including ALL of my changes"
branch: experimental >$ git checkout master
branch: master       >$ git checkout experimental fileWanted1.py fileWanted2.py
branch: master       >$ git add .
branch: master       >$ git commit -m "master now includes all my desired changes from experimental"

Upvotes: 0

Slawomir Jaranowski
Slawomir Jaranowski

Reputation: 8517

First rebase your experimental with current master.

gir rebase master

after this all your commits from experimental will be on top of master commits.

Now you can reset all your commit to current master by git reset master after this all your commits in experimental will be droped but your working files dosen't be touched. Now you can create new commit with changes you want and the last run git reset --hard to clear uncomited changes.

Upvotes: 0

Paul L
Paul L

Reputation: 938

If all the "various different things" you tried out in experimental were in their own separate commits, then it's a simple matter of git rebase -i and just delete those commits you don't want.

Upvotes: 0

janos
janos

Reputation: 124646

What is the simplest way to revert the changes in all but a few files in the experimental branch to master?

Create a new branch from master, and checkout from experimental the few files that you want:

git checkout -b exp2 master
git checkout experimental -- path/to/file/or/dir

At this point, you are on new branch exp2, which is mostly identical to master, except the few specific files/dirs you checkout from experimental.

Upvotes: 3

Related Questions