1

Hypothetical:

I'm working on a project and let's say I create a branch table-settings off of master. I commit changes to three files:

  • plate.swift
  • bowl.swift
  • fork.swift

Someone comments on my pull request and says, "We don't need those changes to plate.swift right now.

Either on github, the command line, or a source control desktop app, is there a way that I can revert all changes to a single file in a commit or pull request, back to the state it was in when I created the branch?

What makes this different from "How do I revert a file to the previous commit", is that I'm wondering if I can revert a file that's been edited and committed multiple times, to the state that it was at when I created the feature branch it's on.

ArielSD
  • 679
  • 8
  • 23
  • 3
    Possible duplicate of [Reset or revert a specific file to a specific revision using Git?](https://stackoverflow.com/questions/215718/reset-or-revert-a-specific-file-to-a-specific-revision-using-git) – Oliver Charlesworth Nov 30 '17 at 16:32
  • Yes, but if they are off in a branch, who cares? – matt Nov 30 '17 at 16:33
  • @OliverCharlesworth So after reading the other answer, I can just git log, and get the hash of the commit on `master` before I started committing? – ArielSD Nov 30 '17 at 18:16

2 Answers2

4

If you want to make it to a state that's in master then do following from your branch

git checkout master -- /path/to/plate.swift

above command will make it to a state, that's in master. Then commit and push normally. e.g.

git commit -m 'reverting not-required changes'
git push;

Thanks!

Azhar Khattak
  • 635
  • 7
  • 11
2

The duplicate link in the comments basically has you covered, but in this situation I might be inclined to do the following:

git checkout HEAD~1 path/to/plate.swift

This will reset plate.swift to the version one revision prior, when you created the branch. Then you can amend the commit via

git commit --amend

Now your commit will contain all the previous changes minus the changes to plate.swift, which file will appear unchanged. You would then have to force push using this:

git push --force origin table-settings

The reason the force push is needed is because we have rewritten the last commit in the branch. But this approach is possibly nice for the reviewers, who will see a feature branch which looks very similar as when they commented.

If your feature branch has already been checked out by reviewers, then a safer option would be to just do a normal commit:

git commit -m 'reverted Plate.swift'

But I have found that amending can be useful when you end up having to make a number of tweaks to a pull request but you don't want to uglify your branch's history with many commits.

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263