Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
881 views
in Technique[技术] by (71.8m points)

git - GitHub keeps saying "This branch is X commits ahead, Y commits behind"

I know there are several similar questions, but I think my situation is a little bit different.

Let's say there is a GitHub repository which I want to contribute to. I fork that repository into my GitHub account and I clone the fork from my account in my PC. Fine.

Before working on an issue, I first want to synchronize my fork with the 'original' repository. I go to my account fork, click on New Pull request, make sure that I select mine as base and the original master as head fork, I see the differences (all the commits that people did in the original repository that are not on mine). Then I create the pull request on my fork and I merge those changes in my fork. I go to my local repo and do a git pull, and I have everything synchronized. Fine.

The problem comes now, in my GitHub account now it's always saying 'This branch is X commits ahead', where 'X' is the number of times that I did the synchronize process I described above. So, every time I do a pull request into the original repository (not my fork), is showing that I'm committing my code plus X more commits, that are the merges I did on my fork to synchronize with the original repository.

Of course, I don't want to push those changes into the original repository, since they already have those changes in place, so I don't understand why GitHub keeps saying to me that I have changes to commit.

I think it's something that has to be solved on my GitHub account, because in my local repository there are no changes or issues, actually I even removed it and re-cloned again.

Do you have any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As you guessed, these extra commits are likely the merge commits from the Pull Requests that you created.

In the future, there's a much easier way to sync your fork with the original repository. In your local repo, after the initial clone do:

git remote add upstream https://github.com/upstream/repo.git

Then, whenever you want to sync the changes from upstream, do:

git pull --rebase upstream master
git push --force-with-lease origin master

(The --rebase and --force-with-lease options will only be necessary if you have commits that haven't been merged into the upstream repo.)

Obligatory warning: Since a rebase rewrites history, this can be dangerous / disruptive for anyone else working on this branch. Be sure you clearly communicate what you have done with anyone you are collaborating with. Since this is a personal fork, I assume this won't be an issue for you.


Now to fix your current issue after the fact.

  1. Add the upstream remote as described above.
  2. Reset your local branch to match upstream:

    git checkout master
    git reset --hard upstream/master
    
  3. If you have created any commits in your fork, you can cherry-pick them onto your updated version of master. If you can't remember or need help finding them, something like

    git log --oneline master origin/master
    

    should show you any commits not in upstream.


Above I assumed that you are only using one branch, master. If you aren't already, I highly recommend that you create a new branch for each feature / bug fix that you work on. Among other benefits, this allows you to start work on another feature / bug fix when you are still waiting for an earlier PR to be merged. If you never commit directly to master, then you can sync without the --rebase or --force-with-lease:

git checkout master
git pull upstream master
git push origin master

To update a feature branch after you have updated master, do:

git checkout myfeature
git rebase master
git push --force-with-lease origin myfeature # if you have already pushed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...