Pages

Friday, February 26, 2021

Fedora 33 : Bash script for GitHub to fix bad commit branch.

One mistake is to have changed in the [master] branch instead of [mymain]. This can be fixed with this bash script:
# check the branch that you committed to by accident, in this case, is master
git checkout master

# reset the branch back one commit
git reset --soft HEAD^

# use stash to record the current state of the working directory
git stash

# checkout the branch it should be in, in this case, is mymain
git checkout mymain

# apply the stash
git stash apply

# commit the changes 
git commit -am "main commit"

# then push the changes to our main branch
git push origin mymain

# checkout the original branch, in this case, is master
git checkout master

# the last step is to force push the commit deletion to the original branch.
git push --force origin master
Hope this help's !!