Search code examples
gitgithubatlassian-sourcetree

Cleaning up a Git branch of unneeded commits and extraneous branches


repo tree

How can I get rid of everything and only be left with the last commit at the bottom? Everything after a28fe12 is expendable.

I've tried using git rebase and git reset HEAD~n as I've done hundreds of times but it seems like something got messed up on the way and I got myself into a rut, and I get to a "clean" state with the master branch.

git reset --hard cd3a8cb produces the following result:

repo tree

Thanks in advance for your help.


Solution

  • You've done it locally. Now you need to push it out to remote repositories.

    origin/master and jvk-github/master are showing you where the master branch is on the remote repositories origin and master the last time you looked. If you git fetch origin and git fetch jvk-github you will get an updated view.

    You need to push your update to these remotes. git push --force-with-lease origin and git push --force-with-lease jvk-github. --force-with-lease is a safer --force.

    However, this means losing all the other commits, including adding screenshots and removing redundant dependencies. Only origin/master appears to have messed up, possibly as a result of some combination of rebasing and pulling.

    jvk-github/master seems fine. I'd suggest instead moving your master to jvk-github/master and pushing that to origin.

    git checkout master
    git reset --hard jvk-github/master
    git push --force-with-lease origin master
    

    Finally, to prevent this sort of issue, I recommend pulling with rebase. By default git pull is a git fetch plus a git merge. This can result in duplication if a branch has been rebased after pushing, as appears to have happened with origin/master.

    Instead, update with git pull --rebase. Your local work will be rebased on top of the remote work. I recommend making this the default by setting pull.rebase = merges.