Eclipse Git : auto-synchronising 2 branches - eclipse

I am working on a project with some mates.
Yesterday I cloned the project with the intention to add a functionality.
I have 2 local branches that are develop (the main branch) and pageContent (my feature branch).
The problem I am currently encountering is when I edit something on my feature branch, it automatically edits it on my developp branch too (I did not commit anything).
I checked out on my developp branch to delete the edition and when I checked out on my feature branch, the edition was deleted too ...
The branches seem to be auto-synchronised.

I checked out on my develop branch to delete the edition and when I checked out on my feature branch, the edition was deleted too ...
This is how git works.
In the following diagram you can see the 3 states.
Git has three main states that your files can reside in.
They are all shared between your branches. but when you checkout branch you change the HEAD so you end up with the staging area && working directory shared between your repository even when you checkout branches.
Since you did not commit (i assume that what happened) when you switch branches you see the changes following you to your new branch.
If you don't want the changes to follow you you need to commit (or stash) your work before switching to the branch.
How to checkout different branch with clean working directory and stage area?
If you wish to checkout clean branch without any "leftovers" in your working directory and staging are you can create a new worktree which will result in shared view of your repository (all the content is shared) but with a different working directory and staging area.
From git v2.5
git worktree add <new_path>
Now do whatever you want in any of your branches. It will create 2 separate working folders separated from each other while pointing to the same repository.
Using wortree you don't have to do any clear or reset in order to remove all your staged and untracked content.
Here is demo of how to do it:

Related

Git why newly checked remote branch has changed files?

I am new to Git. I have checked new remote branch at my local. As I am checking out a new fresh branch from remote, I don't expect any local changes in the files of my repository.
But somehow few files show changes. I am using Git on Eclipse IDE.
Why is so? Have I misunderstood the concept of new fresh branch checkout?
Update
Based on comments by Tim Biegeleisen and Sajib Khan my understanding was wrong as it is expected behaviour when you check-out remote as a local branch any previous uncommitted changes in the working directory ( Repository ) will carry to the newly checked-out local branch.
Just to put in simple words let me explain with example
Think that your working in master branch and add some new file (Eg file1.txt and file2.txt). Now you don't stage or commit these 2 files then it will be shown as untracked files.
Master Branch
Untracked files
file1.txt
file2.txt
At this particular point of time you switch to another branch then the files which were untracked will be shown in the newly checkout branch.
To over come this problem you can go for the following 2 ways
Stash the files from master branch and then switch to another branch, now it wont show the untracked files of master in newly switched branches. Stashing is nothing but saving the files and then reusing those at later point of time.
commit amending

How to rebase in TFS using TFVC? (like git rebase functionality)

Using tfs2015 (not git but tfvc)
We have a MAIN branch and I created my development branch from MAIN branch.
__MAIN (branch)
|__MYDEV (my new branch)
After a while, my friends pushed 2 new changesets to MAIN.
__MAIN [change1], [change2]
|__MYDEV [mychange1]
Question: How can I get those 2 changesets onto MYDEV branch as 2 changesets as they are by merging (rebasing) my change on their changes?
If I try merging MAIN onto MYDEV, 2 changesets are coming as pending changes of bunch of files, but I want them as they are as packaged in 2 changesets and put my changes on them.
Finally what I expect,
__MAIN [change1], [change2]
|__MYDEV [change1], [change2], [mychange1-with-merge-fixes]
If you use git-tfs it will give you all the powers of git, highly recommended. Now, visual studio will maybe be a little bit upset when it detects a .git directory, but you can avoid that by setting GIT_DIR and GIT_WORK_TREE environmental variables (see this answer for details).
You can use "Rebase" tool to keep every merged changeset separately in the branch.
To use rebase tool in TFS, follow the steps below if you have Team Explorer installed:
Launch Team Explorer and connect to your project.
Open "Branches".
Checkout the branch (MYDEV) you'd like to rebase.
Right click the branch and select "Rebase Onto..." option.
Select the Onto Branch (MAIN) and then click "Rebase" button. (Resolve the conflict and click "Continue" button if there is any conflict during the rebasing)
Introduction about TFS Rebase: https://msdn.microsoft.com/en-us/Library/vs/alm/Code/git/rebase

Committing changes to branch I got by downloading zip from GitHub

I'm working on a repository that has two branches: Master, and Release1.
The branch I need is Release1. I went to the site and tried to clone it, but no matter what I did I got the Master branch cloned.
So I gave up and downloaded the branche's zip to my file system. I added it to Eclipse and worked on it.
Now I need to commit my changes. But neither the branch nor the repository show up in my visual tool.
When I try to add a local repository the visual tool tells me the folder is not a repository.
Is there a solution? I have quite a few changes on my local project and I have to commit them.
The Zip download is not a Git repository, it's only a collection of the files at that moment in time. Here's a way you might be able to get your changes into the repository:
Clone the repository properly
Copy your current files into the cloned directory
git status and git diff to check that the changes are what you expect
git commit
When you cloned the repository the first time, you were getting both branches - a Git clone is a fully copy of everything: every branch, all history, etc. The default branch is typically master. After you clone, if you want to switch to the other branch, use git checkout Release1.
What GUI are you using? When trying to clone you have to make sure you change the branch you are cloning from (usually in a drop down menu or you may have to type it in). If you can successfully clone from Master, than you should be able to change to or "checkout" Release1 branch.
Just downloading the zip, just gets you the source code and doesn't have any connection to git. To save and push your changes you will need to clone the repository and checkout Release1

Overwrite obsolete solution in tfs dev branch with solution in tfs main branch

In TFS 2013 how can I overwrite all folders and files under a specific folder with data from another branch?
I have a main and two dev branches and different functionality has been added to the same solution in both dev branches. Main is in sync with one dev branch. Now all functionality that has been added to the dev branch which is not in sync is now obsolete and I want to sync the solution in this branch with whats in main. As the solution differs alot between main and the obsolete branch a traditional merge wont work.
Why not just delete the branch (or rename it for archive purposes), and create a new branch off MAIN?
Another option is to rollback all changesets in the Obsolete-Dev branches history, then do a merge from MAIN.

git modifying code on multiple branches using the same eclipse workspace

I kind of feel I am missing something here, but here is the question.
I have a branch A. Work is in progress in this branch. A tag T is created which marks a production release version. Now, a fix has to be done on top of the code in this tag. So I created a branch B out of the tag. The branch would contain stable production code.
My eclipse points to checked-out code in branch A.
Now, I want to make changes to code in branch B. How can I make my eclipse realize that there are 2 different branches and the code is different?
Do I have to clone the branch B and point a new eclipse workspace to it?
Cant I use the same workspace and have 2 different versions of the same file - one from branch A and another from Branch B?
Just checkout branch B in your workspace, make your changes, commit, and then you can checkout again branch A and continue to work on branch A. Eclipse will automatically recognize the content of the other branch
However, you should be aware, that you shouldn't have uncommitted changes in your workspace, when you checkout another branch. Either commit your changes or stash, before checkout.
assuming you have eclipse integrated with your git system, you can use select the project you are working on, and then in the context menu, use "Team->Switch To->New Branch...", and choose branch B.
if you don't have it integrated, you can do so with the following 2 steps.
using the git perspective, in the Git Repositories view, find the icon with the + to perform Add an existing local Git Repository to this view .
in the subsequent dialog, choose the location of your repository.
once it's added, go back to your java perspective, and from there, select the project in git (which you currently have in branch A), and from the context menu, select Team->Share Project