Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » With command-line jgit, how to checkout a single file back to its git version?
With command-line jgit, how to checkout a single file back to its git version? [message #1767187] Mon, 03 July 2017 10:42 Go to next message
Borna Novak is currently offline Borna NovakFriend
Messages: 1
Registered: July 2017
Junior Member
I'm crossposting here because putting a 200 rep bounty on SO isn't getting me anywhere :) Link:

For example jgit diff returns 1 changed file, I want to revert it back to the versioned version. In normal git I would just

$ git checkout path_to_file

I tried

$ jgit checkout origin/master -- path_to_file

but nothing changes, no error, no nothing and the file remains


I tried:

1. `$ jgit checkout origin/master -- path_to_file` - no change
2. `$ jgit checkout -- path_to_file` - no change
3. `$ jgit checkout path_to_file` - error: `error: pathspec path_to_file' did not match any file(s) known to git.`
4. `$ jgit clean` - wants to clean untracked files, not untracked changes
5. `$ jgit checkout -f origin/some_other_branch; $ jgit checkout -f origin/master` - hoping that this would squash the changes, but then simply the 2nd command fails because "changes would be overwritten"
6. ...and many more things I don't remember any more ofc :)

[Updated on: Mon, 03 July 2017 16:00]

Report message to a moderator

Re: With command-line jgit, how to checkout a single file back to its git version? [message #1767254 is a reply to message #1767187] Tue, 04 July 2017 08:36 Go to previous message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 267
Registered: July 2009
Senior Member
You have to be more precise and tell to what content you want to go back: the one of the last commit or the one which you added to the index. Often they are the same but not always (see the example below). To go back to the previously added content say 'jgit checkout -- <fileName>'. To go back to the content of the last commit (HEAD) you should do 'jgit checkout HEAD -- <fileName>'. I tried it out:

> jgit init
Initialized empty Git repository in /private/tmp/z/.git
> echo HeadContent >a
> jgit add a
> jgit commit -m initial
[master d891314c0a66142827058f9c33e329ee56396847] initial
> echo IndexContent >a
> jgit add a
> echo FileContent >a
> cat a
> jgit checkout -- a
> cat a
> jgit checkout HEAD -- a
> cat a

Previous Topic:Unable to retrieve label for Repository
Next Topic:Porject directory stucture wierdness
Goto Forum:

Current Time: Sun Feb 23 12:26:09 GMT 2020

Powered by FUDForum. Page generated in 0.01874 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top