Issue with adding an existing git repository using EGit - java

I'm a beginner when it comes to version control. I've cloned a repository and started working on it, also for versioning am using EGit on eclipse because it's much easier.
So my problem is when I try to add the existing local git repository (Git Repositories -> Add an existing local repository to this view) to eclipse it says that no repository found (screenshot below). I think the reason is that he didn't find the .git folder on the project directory, because somehow the .git folder disappears and just after that I faced this issue. Maybe some of you will say that I need just to clone again the project and copy the .git folder and put it on the existing project, but the problem is that the remote project received many commits after my first clone, so can that affect my local project?
Thank you :D
click here to display the image

Clarification about .git folder
Without a .git folder you have not really a git repository!
The folder contains all repository data.
So your "repository" is currently only a normal file structure and has lost all git information - it's only the last working copy.
Wanted
As far as I understood, you want to apply your changes to the repository (and maybe later create a PR to origin/remote one...) but you lost the git data completely and you want to fix this.
Suggestion
Maybe you could do following
clone the remote repository to another, new location at your machine
checkout same branch where you formerly did your changes
search for the exact commit id where you started your uncommitted changes
(it's important to exactly checkout the same commit as before, so you have no merge problems later)
do a "git checkout $commitNumber"
now create your own branch from this point
(At this point we are on the same position as when you started your
former local changes - means same base)
copy files from your old location (containing your changes) recursively
into the new location - but ensure relative pathes do match!
(so GIT will recognize file changes as diff...)
open your Eclipse IDE
try to add the new location as an existing local repository inside
Eclipse
(this will work now)
open the "Git Staging" view
Now you should see your delta to your own branch, means your changes.
add your changes to index and commit them to your branch
merge your branch into wanted target main/master branch
IMHO this should solve your problem.

Related

Git is taking renamed package as new folder - Intellij Java

I am having an issue while renaming a package and then pushing it to git remote repo. I used Intellij > Refactor > rename to rename the package, in local Git status is showing as renamed file but in github when raising a pull request it is showing that old package as deleted and new package as added.
How to resolve this and tell git to consider them as renamed ?
Example:
com.test.examplePackage ===> renamed to com.test.examplepackage (we have almost 500 files in this packge)
Now github is showing
com/test/examplePackage/Xyz.java -- deleted
com/test/examplepackage/Xyz.java -- added
Instead of com/test/examplePackage/Xyz.java ===> renamed to com/test/examplepackage/Xyz.java
Similar question has been already answered here:
How to make git mark a deleted and a new file as a file move?
In short, Git will consider the file/directory deleted if the content was modified beyond certain treshold. For this reason you may want to do separate commits for just renaming, and separate for changing file content inside.
Another factor that may be relevant in this case, is that when merging using squash your separate commits are bulked into one, which may explain the different behaviour locally and in github - commit squashing may be turned on by default in your repo.

How to compare same file from 2 different repositories in Intellij?

Suppose I made changes to a file a.java in repository repo1 and committed my changes to a local branch branch1. Next I came to know that I can not directly push my change to repo1. Rather I have to fork the repo and then create a branch and PR from there. So now I forked repo1 to repo2 and created a branch. origin/branch2 and checked it out. Now how can I compare the file a.java from repo1 branch branch1 and bring the changes to repo2 branch branch2? It would be nice if I can do it in Intellij.
If the file is visible from your local repository (as in the other two repos are set as remotes) then git doesn't care.... you can provide the path to the two files and it will diff them (it really doesn't require any relation between the files... it can be 2 completely different projects altogether):
git diff repo1/branch1:path-to-file repo2/branch2:path-to-file2
That should work.
Now.. getting one IDE to do that kind of stuff? I don't think you will be able to pull it off. Might need to consider checking out the file from one branch to your local branch so that then you can pick the parts you want to keep and the ones you want to discard:
git checkout repo1/branch1 -- path-to-file
Or
git show repo1/branch1:path-to-file > path-locally # if the files do not have the same path in the 2 branches
Then feel free to check the things you want to keep on the IDE.
important
I am assuming your working tree is clean and you have no pending changes on the files you want to play with.

Can I reimport a deleated .git file without loosing code?

I deleted my .git file from my project. But, I haven't deleted my project folder. Can I still import ONLY .git file without reimporting the whole project?
I haven't tried anything I don't want to lose the code I have when deleting the folder and reimporting the complete project
To recover a file/folder from your system you need to look for some recovery tools.
But here is the steps, how you can save your uncommitted effort safely:
1- clone your project into another folder.
2- you will have the latest code with .git stuffs.
3- copy paste all files from old folder to new.
4- now you have overwritten only those files which are having change.
5- run git status to see the efforts intact.
Alternatively:
1- clone your project into another folder.
2- you will have the latest code with .git stuffs.
3- copy paste all .git stuffs from new folder to older.
4- now you have git back into your project.
5- run git status to see the efforts intact.
.git is the directory which maintains the version control for your source code. Delelting this does not affect to your source code + local changes.
If you need the git version control back again, easiest thing you could do is get a fresh clone of the remote repository and use a merging tool(ex:winmerge, araxis merge) from which you can identify your local changes) and then you can do a commit

Creating a new root directory in an Eclipse project under version control

I'm working on a group project for my software engineering course, and my professor has indicated that I set up the directory structure incorrectly on Github. I'd like to correct the issue now to avoid a grade penalty, but I'm very new to using Git. Basically we have a structure that is as follows on Github.
src
--main
----java
------Source Files
--test
-----java
------Test Files
Various Ivy/Ant build files
.classpath
.gitignore
.git
.project
README.md
I need to create a new directory called CodeComp that will contain the directory structure above except for the README.md, which should be on the same level as the CodeComp directory.
Since our grade is dependent on the commit history, this would need to be preserved. Is there an easy way to do this or are we better off just taking the small deduction on the final project grade?
UPDATE:
In case anyone else has to do this in the future:
Go to the folder holding the project and create the new directory.
Move all files needed into the new directory except .* files. Refresh Eclipse.
Commit and Push the directory changes to the repo regardless of Eclipse errors.
Manually delete the .classpath, .gitignore, and .project files from the repository.
Back-up your .classpath and .gitignore files. Delete your local copy of the repo and re-clone it.
Import the Git project and choose to put the project files in your newly created directory.
Copy the .classpath and .gitignore you backed up into the new project directory. Push the changes.
In theory you should be able to use a standard file browser to move everything but your .git and Readme file into your new sub-folder and then commit that.
The git history will still there but it may see it as two separate files, one that has been deleted and one that is just created. In my experience Git doesn't really like moving files.
In any case, you commit history will still be in the repository, it will just be 2 separate sections from when you moved the files.
But, hey... It may pay to show that move anyway. I know for sure that I was moving files all around when I first learnt about version control. It's all learning.
EDIT: You can test this by committing the change locally and checking the history before you push it upstream to Github.

How can I commit only a single .java file to git from an eclipse project?

I want to commit only a .java file to an already existing repository. I am using github for a class, and we are each given access to a private repo containing project information. I am supposed to add only a .java file to this repo, but I can't figure out how to do this. I have come close: I committed only the .java file, but it is inside a directory with the same name as my eclipse project.
Is there a way to commit only the .java file, without any of the project information, while keeping the project usable?
Thanks.
edit:
My goal is to have \git\repoName\MyFileName.java
as well as keeping the files already in \repoName\ and keeping the MyFileName.java in a usable eclipse project.
Is there a way to commit only the .java file, without any of the project information, while keeping the project usable?
yes, you will need to create a .gitignore file, and put the rest of your project into the gitignore file, (and also gitignore itself), that way, the only files that are tracked by git are the .java files.
As for the files being inside the directory with the same name as your project, take a look at where you cloned the git repo to, and make sure it is on the same level as your project files.
Take a look at this documentation
http://git-scm.com/docs/gitignore
https://help.github.com/articles/ignoring-files/
I think its much simpler than what you are expecting :)
You can do, git add, docs:
git add yourFileName.java
Now you can commit the .java file
git commit -m "I am updating only .java"
You can either reset or let it be as it is!!
git reset
reset will not have any history. So, be careful if you want to reset. In this scenario I think you dont have to reset. See here for some help.
See my answer on how to push/pull changes from eclipse into github. Let me know for any help on how to interact with github on eclipse.
You want to remove a subtree and create a new repo?
This is definnitely possible but you have to be careful. Creating a subtree only for file like yourFileName.java surprizes me a bit, but why?
For whatever reasons, see this for an easy way, and the original post is this Detach (move) subdirectory into separate Git repository
This blog might also help you.
I figured it out!!
The key was to add a source. You can do it when you create a project, one of the options will say "Link Source" or something along those lines.
You can also do it after a project is created:
Right click the project in Project Explorer. Go to Properties -> Java Build Path (on the left) -> Source (the first tab) -> "Link Source..."(button on the right)
Then just browse for the git repo folder that you already have and you're good to go! Just create new class files within /repoName in Project Explorer.
If anyone needs clarification or anything, let me know.

Categories

Resources