简体   繁体   中英

Git push to a remote repository, discard the latest commit and replace with local commit

I have done this:

  • Forked a repository on Github, made some commits there and pushed, but I ran into troubles.

  • I cloned the original repository (not the fork) and did a commit locally.

Now I want to push the local commit to my forked repository in order make a pull request.

So I set the origin of the cloned repository to my forked remote.

git remote set-url origin https://github.com/MY-USERNAME/REPOSITORY.git

Now I want to make my local repository to completely overwrite the remote. What is the correct way of doing this? git push --force ?

The issue is that git can only have one branch named master . If you fork a repo, add a new commit, if someone else also creates a new commit, and then you push it, you will have an error.

If the goal is to make a pull request, create a new branch with a new name, and push this new branch. Go on the web server and create a full request between your new branch and master.

Don't push --force unless you really want to remove the commits online. Even if it's your fork, this commit is part of the original repo, there is no reason to remove it.

The proper way to go is to create a properly-named feature branch and do a pull request.

If I understand you correctly, you have a remote on github, which you want to discard entirely.

If you do not care about losing remote's history
Simply delete the repository on github, fork it again, and push your changes to this clean fork remote. This way you will not struggle with all the traps git push --force usually brings with it.

If you do care about losing remote's history
Pull from your bad-state remote and then create a new commit which contains the good state. Since this newest commit overwrites all (changed) files, it should work equivalently to not having the bad history around.

The problem with git push --force is, that you might change history in such a way that your fork does no longer correspond to the original repository. It's usually better to find a way without force push.

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