简体   繁体   中英

How do I update my branch in git?

(1) I fork a person's repo in Github, let's call the person's repo as remoteRepo, my github's repo as myRepo. (2) I clone it to my local pc. I use this command,

$git clone [remoteRepo] -b [branch_name] /my/local/folder

Now,the remoteRepo has changed. I am going to update my local files in order to keep same source code with his.

I did like this,

$ git remote add upstream [remoteRepo]
$ git fetch upstream
$ git fetch upstream 
$ git merge upstream/[branch_name]

But it doesn't work. Nothing update, what's the reason? I follow the document from github help

$ git remote add upstream [remoteRepo]
$ git fetch upstream
$ git fetch upstream 
$ git merge upstream/[myBranch]

You haven't made any local changes yet. You're already up to date with the remote repo, so there's nothing else to do.

I don't quite understand your question. If you want to change the location of the remote repository, you can go into the .git folder and open the config file and change your remote.

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@remote.com:folder/git_location.git

There are also commands to change a remote from the command line. github help - git remotes

How do you know that it didn't work? Try 'git log' to confirm the automatic merge commit message and 'git diff HEAD^' to see what the merge did. Also you could slightly simplify the original merge as 'git pull upstream myBranch'.

[edit]

It works for me. Here is a complete log below. To diagnose your problem - when you did 'git fetch' you should see something that confirms the fetch. After the 'remote add', does 'git branch -a' list the remote branches? You can also run 'git remote show upstream' to confirm that your repository is 'connected' to the other. You are pulling from the repository that you cloned; other people are pulling from it; did they push their work back into it? Otherwise you won't see their changes.

A log:

$ git clone dev1 -b br1 dev2
Cloning into 'dev2'...
done.
$ cd dev2
$ git remote add upstream ../dev1
$ git branch -a
* br1
  remotes/origin/HEAD -> origin/master
  remotes/origin/br1
  remotes/origin/master

# NOW change content in the 'dev1' (the remote repository)
$ cd ../dev1
$ git checkout br1
Switched to branch 'br1'
$ ls
bar.c   foo.c

$ FL='baz.c'; echo "stuff" > $FL; git add $FL; git commit -m "$FL"
[br1 ef45921] baz.c
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 baz.c
$ ls
bar.c   baz.c   foo.c

 # Now return to 'dev2' and 'pull'

$ cd ../dev2
$ ls
bar.c   foo.c

$ git pull upstream
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From ../dev1
 * [new branch]      br1        -> upstream/br1
 * [new branch]      master     -> upstream/master
You asked to pull from the remote 'upstream', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.
$ git pull upstream br1
From ../dev1
 * branch            br1        -> FETCH_HEAD
Updating 207c1a2..ef45921
Fast-forward
 baz.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 baz.c

 # Confirm 'baz.c' exists on 'dev2'
$ ls
bar.c   baz.c   foo.c

look at the list of branches with git branch

checkout the branch you want to merge the changes to with git checkout <mainbranch>

merge the branches with git merge <theotherbranch>

and push the changes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM