gitgitlabgit-squash

Partially squash commits


I have a branch in GitLab with 18 commits, with a lot of back and forth trying the right way to escape something.

Ideally, the branch should have three commits, changing three different parts of the file with messages like "read maven coordinates", "created additional environment variables" and "created a release through curl".

What would be a good way to rewrite the branch in this fashion?


Solution

  • Make a backup of the branch (by creating another branch from it).

    git checkout -b backup
    

    Then, run git reset master or whatever the beginning of the branch is.

    Now, you have all the changes uncommitted. You can now use git add -p to select which changes go to which commit. Run git commit once you have them all for a commit.

    If you just need to squash some commits without moving changes from one to another, git rebase -i might be enough.

    If you're satisfied with the result, remove the backup branch. If not, start again from it.