Using Git is hard and messing up is easy.
Refer to Oh Shit, Git!?! (or the profanity free version: Dangit, Git!?!).
Tower offer a free ebook and also have a comprehensive FAQ.
Agree on a branching strategy that fits the team's workflow.
- Gitflow - a popular strategy for feature based development.
- OneFlow - an evolution of Gitflow to be more efficient and linear.
Trunk-based development is more suited to DevOps teams using CI/CD.
-
Checkout the branch and rename it.
git branch -m new-name
If the branch has previously been pushed to the remote, it will need renaming there as well.
-
Push to the remote:
:old-namedeletes the branch with the old name.new-namecreates the branch with the new name.
git push origin :old-name new-name
-
Set the remote tracking reference for the new branch.
git push -u origin new-name
git checkout master
git merge --squash feature/my-feature-
Start an interactive rebase on the commit prior to the commit that other commits will be squashed onto.
git rebase -i HEAD~4
-
Select the commits to be squashed, by replacing
pickwiths.pick ca4b127 Working on new feature s 2050f54 Some changes s 77e50eb Some more changes s 1853c79 Fixed stuff
-
Save the choices (
:xif using vi). -
Update the commit message and save to finish the rebase.
-
Force push to the remote, if necessary.
Sometimes it's necessary to reset the commit author an time, e.g. when squashing commits on a feature branch before merging to master.
git commit --amend --reset-author --no-edit