JGIT: git checkout -- - java

I have a not staged modified file and I would like to discard the changes.
In git it would be something like
git checkout -- .
How can I emulate this behavior with JGit ?
Thanks in advance.

To revert a single file, you could use the CleanCommand:
Set<String> paths = new HashSet<String>();
paths.add( ... );
git.clean().setPaths( paths ).call();
Unfortunately there is a bug that prevents the CleanCommand to reset files in sub-directories.
If I interpret the '.' in git checkout -- . correctly you want to revert all changes in the work directory. The ResetCommand does that:
git.reset().setMode( ResetType.HARD ).call();
This would also override the index with the contents from HEAD.
If you don't care about the index, you could also read the file-contents from the HEAD commit and write them to the work directory yourself. Let me know if that is of interest for you and I will try to assemble a snippet that does so.

Related

Git: Comparing two files in two branches [duplicate]

Is it possible to open a file in a git branch without checking out that branch? How?
Essentially I want to be able to open a file in my github pages branch without switching branches all the time. I don't want to modify it, just want to view it.
This should work:
git show branch:file
Where branch can be any ref (branch, tag, HEAD, ...) and file is the full path of the file. To export it you could use
git show branch:file > exported_file
You should also look at VonC's answers to some related questions:
How to retrieve a single file from specific revision in Git?
How to get just one file from another branch
UPDATE 2015-01-19:
Nowadays you can use relative paths with git show a1b35:./file.txt.
git show somebranch:path/to/your/file
you can also do multiple files and have them concatenated:
git show branchA~10:fileA branchB^^:fileB
You do not have to provide the full path to the file, relative paths are acceptable e.g.:
git show branchA~10:../src/hello.c
If you want to get the file in the local directory (revert just one file) you can checkout:
git checkout somebranch^^^ -- path/to/file
A simple, newbie friendly way for looking into a file:
git gui browser <branch> which lets you explore the contents of any file.
It's also there in the File menu of git gui. Most other -more advanced- GUI wrappers (Qgit, Egit, etc..) offer browsing/opening files as well.
If you're using Emacs, you can type C-x v ~ or M-x vc-revision-other-window to see a different revision of the file you're currently editing (tags, branches and hashes all work).
Add the following to your ~/.gitconfig file
[alias]
cat = "!git show \"$1:$2\" #"
And then try this
git cat BRANCHNAME FILEPATH
Personally I prefer separate parameters without a colon. Why? This choice mirrors the parameters of the checkout command, which I tend to use rather frequently and I find it thus much easier to remember than the bizarro colon-separated parameter of the show command.

How to checkout a certain folder with JGit

FetchResult fr = git.fetch().setCredentialsProvider(credentials).setCheckFetchedObjects(true).Call();
git.checkout().setCreateBranch(true).setName("origin/" + branchName).setStartPoint("origin/" + branchName + "path/to/folder").call()
This is the code I'm using to check out a single folder from a remote repository.
Equivalent git commands are:
git fetch origin
git checkout origin/branch -- path/to/folder
But, the Java code doesn't work for me, I was only able to initialise the local repository and configure remote repository. The checkout didn't work and I couldn't find out what mistake I'm making.
Try making changes in the checkout part like this
"origin/"
and give it a try.
Otherwise you want to do is a sparse checkout
How do I implement sparse checkout in JGit?
In order to check out a particular folder with JGit (a sparse checkout), you need to tell the CheckoutCommand which folder to check out.
For example:
git.checkout().setName( "branch-to-check-out" ).addPath( "path/to/folder" ).call();
addPath() can be called multiple times to check out each of the given paths. The path is interpreted relative to the work directory.

Sticky tag for file is not a branch - CVS

I'm trying to commit my changes to the repository server (using CVS) in Eclipse Kepler, but when I do I get the following error which I've never seen before:
The server reported an error while performing the "cvs commit"
command. ProsperityMobile: cvsntsrv server: sticky tag 1.6' for file
src/com/prosperity/mobile/controller/UserController.java' is not a
branch ProsperityMobile: cvsntsrv server: sticky tag 1.14' for file
src/com/prosperity/mobile/service/UserService.java' is not a branch
ProsperityMobile: cvsntsrv [server aborted]: correct above errors
first!
And honestly I don't even know where to start trouble shooting this or what it even means. Any point in the right direction would really be appreciated!
I just came upon this too. This may happen, when you checkout a specific version of a file or at some specific date, see Sticky tags for more.
In my case, the files had a sticky tag, but were also at the HEAD. So I could just remove the sticky tag with
cvs update -A file.h file.cpp
and then proceed with cvs commit
And again when you're on a branch, it works more or less the same. Just update to the relevant branch with option -r
cvs update -r <branch-name> file.h file.cpp
In order to remove sticky tag from a file in CVS, easily use:
cvs update -A filename
A Tag applies to a specific revision of a file or tree of files. Trying to Commit changes to that wouldn't make sense, and in fact isn't supported by the server. This is why you check things out from a Branch, make changes and then check them back into the branch. A branch is expected to change over time while tags are expected to always point you back to the specific revision.
http://commons.oreilly.com/wiki/index.php/Essential_CVS/Using_CVS/Tagging_and_Branching
I had this same problem in Eclipse, and updating would not work.
What worked was:
Right click on file
Replace with > Another Branch or Version
Confirm (this will override local changes, therefore you should backup them)
Select HEAD (or the branch you need)

svn remove nested/inner class java

I suppose the answer to my question is really simple, I haven't figured out that though.
I had a class with an inner/nested class inside. After I decided to remove this class.
The only problem is that now I can't remove it from my svn. If I execute svn status I receive this ouput:
? .directory
? trunk/.directory
! trunk/classes/org/evaluation/UserProfilesReader$Query.class
I have to delete this file from svn, but if I execute:
svn remove trunk/classes/org/evaluation/UserProfilesReader$Query.class
I receive this output:
D trunk/classes/org/evaluation/UserProfilesReader.class
svn tries to remove the main class and I have to revert this change.
Did anyone have that problem?
The problem is quoting. Run this instead, with the path parameter enclosed within single quotes:
svn remove 'trunk/classes/org/evaluation/UserProfilesReader$Query.class'
$ is used by the shell for variables. So without quoting, the shell expands $Query to empty string, the filename effectively becoming UserProfilesReader.class.
Btw, it's not normal to add build products (*.class files and others) to version control.
I recommend to remove the entire classes directory, and mark the directory to ignore to avoid adding it to the repository by accident.

Git --amend in Eclipse JAVA

I tried to Push some files in git
For example a,b,c,x,y,z files..
I did commit ammends after i found some mistakes int he files...
I did commit amend for same files and pushed same files a,b,c,x,y,z
After Some commits I realized that , i dont want file "C" instead I want file "D".
I added file "D" and did commit amend..
Now everything goes fine but now I could see all files after commit amend
a,b,c,d,x,y,z
but I dont want to send C file to git
I want remove file C after doing commmit amend and
want to see only
a,b,d,x,y,z files
I am using Eclipse,JAVA for doing this process
Any help appreciated
Git's --amend option only allows you to amend the last commit. If you want to amend commits you have done sometimes ago. You need to follow this:
How to modify a specified commit in git?

Categories

Resources