0

My co-worker added some .ps1 files some time ago (several commits since, he might have modified only one of them again later etc. - so we can't assume it was all done in one commit). Let's say they were added in commit1 and later one of the files was modified in commit5.

I then accidentally committed some local changes (specific to my environment) to those .ps1 files, in addition to other changes I really wanted to commit. I had simply staged all changes and forgot to exclude my changes to the .ps1 files (which I want to keep locally, but not commit). We can call this commit14.

I then made another commit (to other files, commit15) and pushed my changes.

Basically it looks like this:

commit15   me         Foo.cs, Bar.cs
commit14   me         Foo.cs, Bazz.cs, [File1.ps1, File2.ps1, File3.ps1]
commit13   co-worker  SomeCode.cs
...
commit5    co-worker  File2.ps1
...
commit1    co-worker  File1.ps1, File2.ps1, File3.ps1 (added)

I have since tried to revert these three .ps1 files (marked in square brackets above) by discarding any local changes in Bitkraken and then cd'ing into the directory containing the files and running:

$ git checkout commit14~1 -f -- File1.ps1 File2.ps1 File3.ps1

There was no output, but the files were then automatically listed as staged in Bitkraken. That got my hopes up - but then I realized the file contents were identical (including the changes I wanted to revert).

I have also tried like this:

$ git reset commit14~1 -- File1.ps1 File2.ps1 File3.ps1

The output was as follow:

Unstaged changes after reset:
M       My/Path/File1.ps1
M       My/path/File2.ps1
M       My/Path/File3.ps1

Again the result was the same, the file contents were not changed. I have even tried specifying "commit13" explicitly instead of "commit14~1", but that didn't work either.

What am I doing wrong?

All of this happened on a branch called "dev", in case that matters.

And I'm not sure about the actual history of the .ps1 files, but I would strongly prefer if I didn't have to find out the last commit for each of them. This could easily have been more than 3 files. Surely there must be a way to say "Change these files back to the way they were before I accidentally committed them, no matter when they were last updated".

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TravelingFox
  • 484
  • 5
  • 18
  • I think you don’t know when your ps1 files changed. Just do a log with diff to find out. – matt Aug 14 '19 at 16:33
  • Based on your comment to the answer provided, I believe this is what you need: https://stackoverflow.com/questions/10755655/git-ignore-tracked-files – joanis Aug 14 '19 at 17:38
  • @matt I would strongly prefer if I didn't have to do that. Imagine if it had been 30 files, instead of 3! Is there no other way? – TravelingFox Aug 15 '19 at 07:56
  • 2
    @joanis My question is about _keeping_ a previous revision of the files, not about ignoring/excluding them. I just want to revert my own changes to those files. So it's not a duplicate. – TravelingFox Aug 15 '19 at 11:14
  • _Surely there must be a way to say "Change these files back to the way they were before I accidentally committed them, no matter when they were last updated"._ Ok now we’re getting to the heart of the matter. You should make that the topic of the question. – matt Aug 15 '19 at 12:56
  • 1
    @TravelingFox sorry for my misunderstanding, I'll retract my "is duplicate" vote. Fortunately, no one agreed with me. – joanis Aug 15 '19 at 13:15
  • Anyway I'm not saying you need to do it for all of them. I'm saying the reason you are not seeing any change when you reset a file might be that you have not gone back far enough for that file. Studying the history of _one_ file might be sufficient to assist you to understand what's going on. You have not given _us_ enough information advise you more specifically, so _you_ have to do some work at your end to figure this out. – matt Aug 15 '19 at 15:23
  • @matt I agree about the topic. Is there a way to change the title? Anyway, I think there should be enough information now. I also mentioned in which commit I screwed up the files, I just don't want to find out the last "good commit" for each of the files I messed up. It's just 3 in this case, but it could also have been 30 or 300. – TravelingFox Aug 15 '19 at 16:15

2 Answers2

0

The easiest way to remove files from version control is

git rm <file name>
git commit

If you still need these files as part of your local configuration, just back them up to another folder and copy them back to your project's folder after you run these commands.

You should also add configuration files to .gitignore so that you avoid this mistake in the future.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Sorry, I was probably not clear enough. These files contain some configuration. I want to keep them. They were added by a co-worker several commits ago. I just don't want to keep my own changes to these files (as they are specific to my local environment and would be useless for the rest of the team). – TravelingFox Aug 14 '19 at 17:06
0

I took at look the git rebase -i HEAD~2 command option for you, but it looks like that will only let you edit commit messages and does not let you remove files added during that commit. Give that to be the case you will need to roll-back the commits and re-apply them one at time. That would be done by performing the following:

Reset your previous commit and stash it:

git reset --soft HEAD~1
git stash

Then reset the commit previous to that (the one with the extra files):

git reset --soft HEAD~1

After resetting that commit, those mal-commited files will still be staged for commit. Remove them from the upcoming staged commit using:

git reset My/Path/File1.ps1
git reset My/Path/File2.ps1
git reset My/Path/File3.ps1

Now that the files are removed from the staged commit, updated (or create) a .gitignore file in the root of your repo. In the .gitignore file add the following line to ignore all .ps1 files:

*.ps1

Alternatively, if you don't want to ignore all .ps1 files you can explicitly path out the files you want ignored by adding these three lines:

My/Path/File1.ps1
My/Path/File2.ps1
My/Path/File3.ps1

Once your .gitignore file is updated, make sure to add it to your upcoming commit:

git add .gitignore

You can then complete you commit, this time without the concern of the .ps1 files ever being accidentally committed again.

To reapply the other commit you stashed, pop your stash and complete that commit also, using these commands:

git stash pop
git commit -m "relevant commit message"

The steps above should have undone, corrected, and reapplied those commits using the git commands: git reset, git stash, and git commit

If you are afraid of messing something up, create a branch first to save your previous work by executing these commands before starting the steps above:

git checkout -b [new branch name]
git checkout [previous branch name]

This should store all your messed up commits away in an old branch in case you want to come back to them.

benhorgen
  • 1,928
  • 1
  • 33
  • 38
  • Thank you. I do not want to remove any files. My original question must have been really poorly worded as you're the second person who recommended this. I'm going to edit my question. – TravelingFox Aug 15 '19 at 07:20
  • One key aspect of my answer, I don't believe it will remove the file from source control, it will just prevent from your local changes (or any other engineers') getting committed/push to your team's repo. If you ignore the steps for adding it to your `.gitignore` file... the rest will work and allow for changes to those files to later get committed. – benhorgen Aug 15 '19 at 17:40
  • That's not what I want either (or not necessarily). I'm fine with having my changes on my own branch or so. We might have to make other changes to the files. But the important question here is: How do I get the previous version of the files back? I'm really surprised that this seems to be so extremely hard in Git. – TravelingFox Aug 15 '19 at 18:38
  • Did you push your changes to your `remote` already? If so.. the only way to get the changes back would be to check out the commit with the correct values in the `ps1` files, copy those changes to a temp folder, then checkout the HEAD of that branch again and apply the changes from that temp folder and push the changes back to the remote. Sorry I couldn't help more. – benhorgen Aug 15 '19 at 21:40
  • @TravelingFox The reason everyone is having so much trouble helping you is that resetting the file to an earlier commit that contains this file _is_ how you get it back. Now, if what you did is change the file _and_ add it for the first time, there is no earlier version to go back to, as it wasn't previously under version control. But you didn't tell us if that's what you did. – matt Aug 15 '19 at 22:14
  • No, I most definitely did not add the files. They were already under version control. And yes, I had already pushed my changes when I realized my mistake. – TravelingFox Aug 16 '19 at 04:33