Local branch not created when pulling new remote branches (Git) - eclipse

I am using EGit 2.1.0 in eclipse juno.
A user X has created and pushed a branch FeatureBranch. Next user Y pulls from master. Now in the Remote Tracking the FeatureBranch is visible on user Y's machine. Now user Y want to join the work done on FeatureBranch. But the FeatureBranch is not automatically available as a local branch. So user Y first needs to create a local branch from the remote FeatureBranch (else all commits will end up being seen as detached heads).
Why does user Y have to manually create a local branch from the remote branch before being able to commmit/push on it?

That's just how git works. Remote branches are read-only. If you want to automate the git checkout --track or whatever it is you want to do, you could certainly script it.

Related

How to work on a project fork when having the original in Eclipse?

I'm trying to create a pull request on GitHub for project "original/QWERTY" so I forked the repo to "Mark/QWERTY". In Eclipse, I already have a repository set up for "original/QWERTY" and that project is in my workspace, named QWERTY.
Now if I create a new repository pointing at "Mark/QWERTY", I'll have two projects with the same name and both Eclipse and me won't like it.
I thought that it should be possible to have a branch or another remote under a repository and switch between them instead of having two copies (I mean just store the diffs). The problem is that they are different projects on GitHub so I'm not sure how to do it.
What is the correct way to set up two GitHub projects to create a pull request from my fork to the original one in Eclipse with EGit?
The usual workflow for forked repositories is to have a single local repository with a single work directory that is configured to fetch and push from/to multiple remote repositories.
With this setup, you can switch between branches that originate from different remote repositories.
The Fork a repo documentation of GitHub explains this setup when using CLI Git. Most of it should also apply to repositories hosted elsewhere.
Using the EGit documentation, it should be possible to translate these instructions into the corresponding actions in EGit.
How to manage multiple remotes with EGit is documented here: https://wiki.eclipse.org/EGit/User_Guide#Remote_Repositories
Using the information from Rudiger's comment and answer and my trial and error with branches I made my own steps. This picture also helps with terminology.
First, do these 2 things in any order:
fork the original project in the github website so now you have the original and the fork. They have the same code and branches.
create a local repository pointing to the original repo on github. Let's say you decided to select only the master branch.
Remotes: you get a new remote I'll call origin (default). configure its fetch if it's not done for you, the default specification is +refs/heads/*:refs/remotes/origin/*. This ref spec maps all the repo branches to Remote Tracking branches with the same name. If you want to only fetch the master branch then use +refs/heads/master:refs/remotes/origin/master.
Branches: you get a "Remote Tracking" branch called origin/master and a local branch called master with configuration of "Remote: origin" and "Upstream Branch: refs/heads/master". You will be working under the local master as it's the only branch right now.
Now you want to be able to push to your fork so you can create PR. You can and already did pull from the original to keep getting updates from other people's work.
Right click on "Remotes" and create a new remote, I'll call it fork (call it whatever you need). Configure its push.
the URI points to your fork the same way the origin Remote URI points to the original.
The ref mapping maps the branches. Go to "Advanced" and click "Add All Branch Specs" if it isn't done for you. You should get the spec refs/heads/*:refs/heads/*. It's easy to work with this spec but you can change it to whatever you need.
Create a local branch (right click -> switch to -> new branch) whose source is the local branch named master and the branch name is whatever suits what it does. it can be the master branch or a new branch that let's say fixes a bug, so bug 123. You do not have a Remote Tracking branch because those are used for pulls. If you also pull from fork then you will need to configure that in the Remote fork and get a remote branch.
Now you are working on a local branch bug 123 (you can see a checkmark next to it). Fix the bug in your code and in the Git Staging view you should see the files changed and the title is <Repository name> [bug 123]. Make sure you are going to commit/push to the correct branch! Stage whatever you need and commit (adds the changes to the local branch bug123) and push (creates a branch on the github repo called bug 123 if you stayed with the default spec).
Now go to the GitHub repo page of either the original or the fork and the UI will tell you that you can create a PR. From there GitHub will guide you.
Once the PR is merged into the master branch of the original on GitHubm, you will want to fetch from the master.
Right click on the Remote origin or its fetch "subdir" and choose fetch. The will fetch any changes in all the remote branches because the fetch spec we used maps all the branches (we used the * character).
That's it. Continue to switch to a local branch which maps to your fork based on the updated master, fix bug, commit and push, create PR, wait for merge into the original, fetch and pull from the original.

Sourcetree isn't populating my dev branch in pull

Ever since I had to recreate my dev branch due to some mishaps with Git, Sourcetree doesn't prepopulate the "dev" branch in the pull and push dialogs. Every other branch works fine, but not dev. It's not a big deal, but I have to select dev manually every time. What gives?
Screenshot:
The "Remote branch to pull" dialog is pre-populated based on the remote branch that is being "tracked" by your local branch. (When you check out or push to a remote branch, SourceTree usually sets this up automatically.)
If your dev branch is not tracking a remote branch, then the dialog will load with no selection. To change this, right-click the branch and select 'Track remote branch."

how to clone all branches and not just master in Atom Text Editor

I have a repository on Github which consist of two branches. I am trying to use Git-Plus:Clone in Atom to try to clone all repositories. However, all my attempts have failed and only the master branch gets cloned. I have looked this problem up on SE but could not find a way to do it. Can someone kindly help me figure this out. Thanks in advance !
However, all my attempts have failed and only the master branch gets cloned.
This behavior is not unique to Atom. It's the normal git clone behavior.
All branches are cloned, but git clone will only automatically make a local branch for master or whatever the default branch for the repository is. The rest remain as "remote tracking branches", local copies of the remote. They're on your disk, but they're effectively read-only. Git does this to avoid flooding your clone with possibly a bazillion irrelevant local branches, should the project you're cloning have a lot of branches.
For example, if your remote has master, foo, and bar. You will wind up with origin/master, origin/foo, origin/bar, and master. origin/... are all remote tracking branches. They remember the state of the remote repository the last time you looked at it (with a git clone, or fetch or pull).
master is a local branch of origin/master for you to work on. If you want to work on another branch, make a local version of it. For example, git checkout -b origin/foo foo would create a local foo for you to work on (or however you do it in Atom).
See also this answer.
In Atom, install Git Plus package in Preferences, then toggle command palette (on mac it's cmd+shift+p) and type checkout, select "Git Plus: Checkout Remote" from the suggested list of items in the drop-down menu.
Then you just need to select the target repo and branch you wish to check out and the remote branch will become local and you will be able to work on it and switch between different branches.

Git branching messed up

I'm using Egit on Eclipse Mac and PC to sync a project that has three branches:
master
dev
rendersystem
I've created the project on the Mac and when I created the two branches dev and rendersystem I've used revs/heads/master as the Source ref and as Pull strategy I've used Merge.
Now I've switched to my PC and imported the project with Egit incl. all three branches. But if I change to dev or rendersystem branch it tells me that these branches are remotely tracked (in Branches dialog, Remote Tracking /origin/dev and /orginin/rendersystem).
If I check out dev or rendersystem branch and change my code, then commit it and try to push it to Github, it doesn't push the dev or rendersystem branches, only the master it pushed.
My question is now: How do I change the dev and rendersystem branches so that they are in a state where I can push them to Github from my Mac and PC?
Sorry if this question sounds confusing, but Git is one hell of confusing for beginners.
Remote tracking branches are read-only in git, as they represent remote changes. A Fetch will only update these remote tracking branches. A Pull first executes a Fetch, and then merges the changes with a locally editable branch.
On the source computer there was no need to create this branch, as it was initialized locally, and pushing the branch can create the remote branch.
You can create a local branch from the remote branches by Right clicking on the Remote branch in the Git Repositories view, and selecting Create branch... After that, your branch would be writable.

EGit - set up remote tracking correctly when creating a branch

When I create a new branch from a remote branch using EGit I get the wrong remote tracking set up:
I create the local branch feature1 from the remote branch refs/heads/master and immediately push to upstream. The followign remote tracking gets configured in my .git/config file:
[branch "feature1"]
remote = origin
merge = refs/heads/master
So, pull and push in that branch will pull and pull from/to the remote master branch. I would expect the following tracking configuration instead:
[branch "feature1"]
remote = origin
merge = refs/heads/feature1
I know I can fix it even from EGit, but I'm worried about other developers in my team not realizing this and pushing uncompleted features to the master branch.
Am I doing something wrong?
Egit will, by default, take the upstream branch for the merge parameter when creating a branch from a remote one.
That means, if you want to enforce the policy which is to push to remote/master only from a local branch master, you can try and enforce it locally.
Check if Egit respects a git config push.default current for pushing only the current branch to an upstream branch with the same name (which might actually become the default policy after git1.7.10).
However, that has the issue of making sure every developer has that policy active in his/her repo.
(I don't think you can enforce it at the "central" repo one, where you could add a server-side hook like an update hook: that script take the name of the branch being updated (ie here 'master', not the name of the branch in the downstream repo, ie here 'feature1')
Note: bug 378960 has been marked as resolved (February 2014) in Egit 3.2:
Push Branch / Initial Push wizard simplifies pushing a branch and also allows upstream configuration for new branches created by the push operation:
Maybe you should advise developers to create feature-branches not from the remote-tracking branch (e.g. origin/master), but from the local one (master). This way, EGit will not set up any remote tracking by default.
If you then decide to publish the branch for the first time, it should be possible to set up the remote tracking (the equivalent of git push --set-upstream) after bug 378960 is implemented.