17

It is possible in GitKraken to revert changes of a single file to an earlier commit instead of reverting an entire commit?

Erik Sillén
  • 337
  • 2
  • 13
Feofilakt
  • 1,341
  • 2
  • 13
  • 33

6 Answers6

24

Answer

A revert in the git-sense of it can only be performed on a commit. It introduces a new commit that exactly negates the reverted commits' changes. See here. GitKraken supports this: right click on a commit, Revert <branch> to this commit.

What you want to accomplish, however, can be done via git checkout. I do not think GitKraken supports this funtionality for a single file yet. You can, however, use the command line.

Reset single file via command line

git checkout <commit> <file>

Check out a previous version of a file. This turns the <file> that resides in the working directory into an exact copy of the one from <commit> and adds it to the staging area.

Documentation can be found here.

git checkout HEAD~1 <filename> will thus reset a single file to the commit before the current HEAD.

tjpaul
  • 343
  • 3
  • 16
kowsky
  • 12,647
  • 2
  • 28
  • 41
  • 2
    FWIW, GitKraken 7.3.0 (probably older versions too) does allow for effectively running `git checkout` on a single file in the UI: Right click on the file under "unstaged files" and select "discard changes". Works on folders as well. – Kristoffer Sjöö Sep 02 '20 at 06:56
  • 1
    TortoiseGit (Windows only) can do this easily. Right click selected files, select "Revert to this revision" or "Revert to parent revision". – Zhaolin Feng May 12 '21 at 04:49
  • And that, ladies and gentlement, is the reason why TortoiseGit confuses people: Using different wording than anyone else talking git. – kowsky May 26 '21 at 06:31
  • It is also something easy to do in SourceTree. It is a missing feature in GitKraken. – Jona Sep 15 '21 at 08:17
  • @KristofferSjöö You should add that comment as an answer, as this is the real correct answer for the original question. – Metafaniel Feb 08 '22 at 23:49
13

You can accomplish this in the GitKraken UI, but it's a little roundabout:

  • Revert the most recent commit(s) back to where the file was deleted, but when GitKraken asks if you want to immediately commit the reversion, click no.
  • Unstage all changes
  • Stage only the add for the file you're trying to restore
  • Right click in Unstaged, and Discard all

This should leave you with only an add for the one file you wanted to restore. Commit that, and now you've got your one file back.

Note that this can work across numerous commits, not just one... but since it's going to have to roll back everything from all of those commits, and then discard all of the rollbacks (except one) it can be quite slow if involves massive changes. In situations like this, it is probably better to use the git CI as suggested in kowsky's answer.

Mir
  • 2,429
  • 1
  • 29
  • 34
  • I a newbie ... when I do the revert, it fails... saying conflicts were found attempting to merge into master - even though I clicked no. ?? – Yogi Bear Jul 26 '18 at 19:32
  • @YogiBear That does seem strange. Did you have any uncommitted changes before you tried to revert? It might be hitting conflicts between the uncommitted changes and the stuff it's reverting. – Mir Jul 27 '18 at 15:43
  • SourceTree supports this feature. Not only can you reverse individual files, you can pick and reverse individual hunks and lines. – Triynko Oct 09 '19 at 14:58
  • nice tactic there mate – Jean Raymond Daher Aug 27 '21 at 12:16
2

Although it doesn't strictly involves using git commands, GitKraken offers the possibility to visualize the content of any project file at any given commit.

When acting on a single file, it might be much easier to copy/paste the targeted commit file's content than using complicated git commands that might very well end up messing your whole project's commit history.

To achieve this, simply:

  • Open your git project in GitKraken
  • Click on the desired commit in the commit history line
  • In the right panel, check the View all files checkbox
  • Locate the desired project file and click on it
  • The file content will be displayed in the main panel
  • You can now copy/paste the content

Simple and efficient when you only need to revert a very limited number of files...

Thibaut
  • 21
  • 2
1

GitKraken 7.3.0 (probably older versions too) does allow for effectively running git checkout on a single file in the UI: Right click on the file under "unstaged files" and select "discard changes". Works on folders as well.

(This answer was previously a comment to the accepted answer)

0

You can make an UNDO to the last commit edit it and after that make a Force push to overwrite that. Works very well

0

Hope GitKraken can do this, as "TortoiseGit" does.

Revert

Zhaolin Feng
  • 428
  • 4
  • 8