Git: Comparing two files in two branches [duplicate] - java

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.

Related

Gitignore like file convention

I'm developing a script with (Groovy/Java) which compare two folders and generate a diff patch file in zip format. The thing now is i have to ignore some file within the iteration because they should not be included in the patch file.
Requirement:
Create a configuration file (like gitignore file) which list the path of ignored files in the comparison process.
The problem:
I like to stick at convention/codestyle guidelines but now i cannot find any reference on how should this be done (not the code, i know how to do it) in terms of:
the name, the extention and/or location of the file.
the codestyle, how files should be listed (maybe i should copy gitignore pattern).
Other things to consider.
I hope i've been clear and my question is within SO's topic scope.
Thanks in advance
1 and 2 are addressed in gitignore man page: the .gitignore file would be part of the source files you are scanning and would apply to files from the .gitignore folder and below.
It uses a glob pattern, like fnmatch does (meaning: no regexes).
If it was an actual git repo, git check-ignore -v -- afile could tell you if that file is ignored or not.

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.

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.

JGIT: git checkout --

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.

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