Question

I have a four unsynced commits in Git, one ahead and three behind. I want to push the commit that is ahead to remote and delete the other commits from the remote branch. How can I do this?

Était-ce utile?

La solution

For history consistency not recommended to delete commits already exists in repository. Especially if there is multiple users. Right way is to revert unneeded commits in separate branch, push revert-commits to repository, than merge/rebase branch with ahead commit with repository branch and push result. Here is example of commands (you need to modify it for your needs):

git branch revert-commits origin/yourremotebranch
git checkout revert-commits
# check hashes of needed commits
git log --oneline
# revert them in reverse order
git revert 473f879
git revert 473f878
git revert 473f877
git push
# checkout to branch with ahead-commit
git checkout ahead-commit
# rebase reverts in ahead-commit branch to resolve possible conflicts
git pull --rebase origin yourremotebranch
git push
# delete unneeded revert-commits branch
git branch -D revert-commits

Autres conseils

A log of the commits and branches would help (like git log --graph --decorate --pretty=oneline --abbrev-commit --all), in order to understand what you want to do.

But considering that commits in git are like a 'dependency chain', and having an idea of what you want to do, the easiest way may be just to create a new branch from the point you last synced, cherry-pick just the commits what you want and use that (this applies if you are working pushing branches, like with github).

Following this, if you're working directly in master, you would remove the commits you don't want and then merge back the previously created branch.

But again, I'm just guessing with the information you posted.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top