Home > Enterprise >  How to get rid of "This branch is 4 commits ahead" message in a forked GitHub repository?
How to get rid of "This branch is 4 commits ahead" message in a forked GitHub repository?

Time:10-13

I forked a repo, started pushing some commits and get a strange message on GitHub:

screenshot

How I can get rid of this message?

CodePudding user response:

That just means your fork has changes from the repository it was forked from.

CodePudding user response:

When you fork a repository, the fork appears as a repo in your own account. The fork is ahead of the forked repository when you push commits to the fork. And this is what the message says.

When the original repository is ahead of your fork because other changes were merged, you need to pull the changes to your fork. You can sync the fork with the original repo quite easily today using the Sync button using the GitHub UI below the green code button (see step 3 on the linked page).

Synching a fork is different from getting rid of unwanted commits.

Going back to the original state by removing commits

To go back to the state of the fork where it was when you originally forked it (and remove the commits you see your fork being ahead), you have multiple options:

  • Delete the repo in your account and fork it again (easy)
  • Remove the commits in your fork (more elegant)

Example

My fork is ahead by 1 commit (modified the readme for testing):

enter image description here

  1. I cloned my fork to my local computer.
  2. I ran git log to check the history
  3. I removed the commit (you can also use the previous commit ID here instead of HEAD~number) with the command: git reset HEAD~1 --hard
  4. I used git push --force to push the removed commit to my fork.

enter image description here

Note: git reset HEAD~2 --hard will go back 2, and so on.

As you can see, the fork is in sync again (no commits ahead).

enter image description here

  • Related