Home > Mobile >  How can I get rid of "This branch is 4 commits ahead" message in a forked repository on Gi
How can I get rid of "This branch is 4 commits ahead" message in a forked repository on Gi

Time:10-09

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:

Well, the question of whether you want to get rid of your commits was less rhetorical. Synching a fork is different from getting rid of unwanted commits. It also happens to me that I have commits in a repo I don't want anymore.

Going back to the original state

If you want 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 several options:

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

You'll find more information below.


What is a fork?

from: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/about-forks

A fork is a copy of a repository that you manage. Forks let you make changes to a project without affecting the original repository. You can fetch updates from or submit changes to the original repository with pull requests.

The fork appears as a repo in your own account. The fork being ahead of the forked repository is normal when you push commits to the fork. You can create pull requests to get your changes in the original repository.

Syncing a fork

Sync a fork of a repository to keep it up-to-date with the upstream repository.

See https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork for details on how to do this.

It will happen that the original forked repository will be ahead of 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). Btw, my fork was behind about 5000 commits :)

Undo the commits in your fork

Run git log to see your fork's history:

You can use the previous commit ID or the HEAD syntax.

Use git reset HEAD~1 --hardto go back 1 commit,git reset HEAD~2 --hard` will go back 2, and so on. Then, push the changes back to your repo.

Example

My fork is ahead by 1 commit (simple addition to the readme file for testing):

enter image description here

I cloned it and ran git log:

enter image description here

Then, to remove the commit I used:

git reset HEAD~1 --hard

enter image description here

Finally, I used git push --force to push the removed commit to my fork.

The fork is in sync again (no commits ahead).

enter image description here

  • Related