2

So I created a branch using Jira and I checked out that branch on my local system. Now I'm trying to do a git pull on my local system but I'm getting this message

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> feature/branch-name                                                                                                                                        mark-violation

What do I need to do? I have seen this link, but it wasn't clear to me.

user1232138
  • 5,451
  • 8
  • 37
  • 64
  • 1
    Does this answer your question? [What does git remote mean?](https://stackoverflow.com/questions/20889346/what-does-git-remote-mean) – jonrsharpe Nov 09 '20 at 18:37
  • 1
    If the branch was created through jira, then you need to run `git fetch` so you are able to see it, then you can check it out: `git checkout name-of-the-branch`. Given that the branch does not exist locally, git will check if there's a remote branch with that name. If there's only one remote branch with that name, it will create it locally and associate it with the remote branch. – eftshift0 Nov 09 '20 at 18:37
  • 1
    Also https://stackoverflow.com/q/4693588/3001761, https://stackoverflow.com/q/34921623/3001761, https://stackoverflow.com/q/32056324/3001761 – jonrsharpe Nov 09 '20 at 18:38
  • @eftshift0 I didn't do ```git fetch``` but I did ```git checkout -b ```. Would that work. If not, what do I do now? – user1232138 Nov 09 '20 at 18:40
  • Oh... ok... I quite didn't get the whole situation. What you want to _pull_ is whatever changes are done on the base branch? (say, `main` or `master`) – eftshift0 Nov 09 '20 at 19:18
  • @eftshift0 but I'm getting that message when I do a ```git pull``` – user1232138 Nov 09 '20 at 19:21
  • Well.... sure... if you created the branch with `checkout -b` then it has no upstream branch set. You can specify from which branch to pull changes with `git pull the-remote the-upstream-branch` (replace the two relevant values). That is, a only once thing. If you want to fix the upstream branch you can set it like this: `git branch --set-upstream the-remote/the-upstream-branch`. – eftshift0 Nov 09 '20 at 19:27

1 Answers1

7

There are two Gits in this story. Let's call them Jira's Git and your local Git.

  • When you created a branch on Jira's Git, you did just that: created a branch on Jira's Git.

  • When you said git checkout -b branch-name on your local Git, you created a branch on your local Git.

Those two branches have no relationship to one another at all. They may happen to have the same name, but that's just a coincidence; they know nothing of one another.

You have not even told us if there is any relationship between your local Git and Jira's Git in the first place. But if there is — if you created the local Git by cloning Jira's Git, or if you have attached Jira's Git to your local Git as a remote — then the correct procedure was to git fetch in order to create remote-tracking branches in your local Git that are tied to Jira's Git. You could then have checked one of those out as a local branch.

If it's too late for that because you've created a local branch already, you can tie it to the remote-tracking branch after git fetch by saying

git branch -u origin/branchname branchname

The -u means "set-upstream" and ties the local branch, branchname, to the remote-tracking branch, origin/branchname (assuming that origin is the local name of the remote that designates Jira's Git). Please note that this is exactly what Git already told you to do when it said you should say

git branch --set-upstream-to=origin/<branch> feature/branch-name

You could have just done that and you'd be home by now.


Note that there are actually three branches in this story: the branch on Jira Git, the remote-tracking branch in your local Git, and the local branch in your local Git. The branch on Jira Git and the branch in your local Git will typically have the same name, let's say branchname; the remote-tracking branch in your local Git will typically have that name preceded by the origin name, let's say origin/branchname.

So, roughly speaking, the purpose of the remote-tracking branch is to act as a bridge between the other two:

  • When you say git fetch, you sync Jira's Git's branches down to your local remote-tracking branches. Therefore you don't usually need to tell Git how to tie a remote-tracking branch to the Jira Git; it already knows how to do that, because it was set up for you that way when you said git fetch.

  • When you say git checkout with a branch name that happens to match the name of a remote-tracking branch, Git assumes that what you really mean is, "Make a local branch and tie it to the remote-tracking branch of the same name." So it does that. That is why fetch and then checkout would have been the right thing to do originally. Instead, you made a local branch manually, and so it had no relation to the remote-tracking branch.

  • When you say git pull, you sync a Jira Git branch down through to the local remote-tracking branch to the corresponding local branch. The first step, involving the remote-tracking branch, just makes a copy; but the second step merges (by default) the remote-tracking branch into the local branch.

  • When you say git push, you sync the other way: the local branch is merged into the remote-tracking branch, which is then merged up into the corresponding Jira Git branch. Those merges are guaranteed to be fast-forward merges, because if they were not, Git would resist; you can overcome that resistance by using "force". If you say git push when the local branch is not tied to a remote tracking branch, you can tie it by adding the -u attribute: git push -u origin branchname is a common way to do a first push for a branch that you created locally, and after that you don't need the -u for that branch because the association has been made.

I have greatly simplified, but hopefully that's enough to give you a more accurate mental picture of what generally goes on.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for the answer. So I went out of the branch I created on my git and did a ```git fetch``` and then checked in to the branch. And I'm hoping this time I checked out the remote branch as a local branch unless I checked back into the same branch that I had created previously but I'm still getting the same message. – user1232138 Nov 09 '20 at 19:17
  • Because you didn't check out the remote branch. You have a local branch and a remote branch, by the same name, but you still have not established any relationship between them. You could do that by saying `git branch -u origin/branchname branchname` if `origin` is the name of the remote. – matt Nov 09 '20 at 19:25