Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pull a commit that someone else made on your branch?

Tags:

git

github

I have some work done on a branch: WIP-my-branch. I pushed up my branch and made a PR (yet to be approved b/c it's in progress). I got stuck, then asked for help; a co-worker then pushed up a commit on to my branch. I can see her commit on Github, but doing git pull on my branch does not get her commit (the most recent commit on the branch). How can I accomplish this?

like image 259
TheRealFakeNews Avatar asked Jul 24 '19 16:07

TheRealFakeNews


People also ask

How do you pull changes from someone else's branch?

Pull new changes from remote: git checkout master , git pull upstream master . Sync dev branch: git checkout new_feature , git merge master . Push changes to your remote repository: git push origin new_feature . Open a pull request on GitHub merging your changes with the upstream (original) repository.


Video Answer


3 Answers

I think it helps to realize that branches are specific to one particular Git repository. Your co-worker did not make a commit on your branch. She made one on a different branch that may or may not have had the same name.

You're using two repositories. One is yours, on your computer. The second one is the one on GitHub. These two different repositories have different branches. These different branches may have the same names, and once you fetch (get commits from them) or push (send commits to them), the different branches can have the same commits too: after that, you can call them "the same", but they're still different in an important way: changing one won't affect the other (yet).

You co-worker is also (probably) using at least two repositories: hers, and one on GitHub. You probably can't reach hers directly, but you should be able to reach the one on GitHub. That one on GitHub may even be the same one on GitHub that you already use.

If your co-worker is using the same repository on GitHub that you are using on GitHub, you already have a name in your own Git for that repository. You call it origin. You can run git fetch origin to obtain commits from origin. That other Git—the one on GitHub—has various branch names in it. Your Git will get their commits, then rename their branches to origin/whatever in your repository, so that no matter what names they have, yours are different—yours don't start with origin/. (Technically, your origin/* names are remote-tracking names rather than branch names, but the important thing here is that they start with origin/.)

If your co-worker is using a different repository on GitHub, you will need to create another remote in your own Git repository:

git remote add <name> <url>

where the name part is whatever name you want to use—I don't know your co-worker's name; I'll just use alice here—and the url part is the URL needed to reach that Git on GitHub:

git remote add alice <url>

Now you can run:

git fetch alice

which will bring over any commits in that repository that you don't have, and rename those branches to alice/master, alice/develop, and so on—basically, all your new "remote-tracking" alice/* names.

Now you will have the commit she made. She made it in her own repository, where it was totally private, but she then sent it to her GitHub repository, where you could get it. You got it, and now it's in your own repository, so now you can use it. If Alice made it on a branch named WIP-my-branch, then sent it to her GitHub repository under the branch named WIP-my-branch, you now have it under your alice/WIP-my-branch.

like image 55
torek Avatar answered Oct 20 '22 16:10

torek


You need to update your branch git pull origin your branch name

like image 38
Piyush Gupta Avatar answered Oct 20 '22 16:10

Piyush Gupta


You need to add your co-worker's remote and merge in their changes.

git remote add coworker_remote url_to_their_repo
git remote update
git merge coworker_remote/WIP-my-branch
like image 38
EncryptedWatermelon Avatar answered Oct 20 '22 16:10

EncryptedWatermelon