[Git] Git Rebase

1 minute read

Git Rebase

  • Alternative to git merge
    • Rebasing re-writes the history of the commits so that the branch begins at the tip of the main(master) branch
    • In short, it deletes the ‘muddied merge commits’ and makes the commit log cleaner
    • Do not rebase commits that have been shared with others, (i.e. commits on the master), unless it is celar that other people did not include those commits
  • Cleanup tool
    • Rewrite, delete, rename, or even reorder commits(before sharing)
    • -i option is used to enter the interactive mode, whcih allows to edit commits, add files, drop commits, etc.
      • Need to sepcify from how far back the commit will be rewritten
      • Most used commands in interactive mode
        • pick: use the commit
        • reword: use the commit, but edit the commit message
        • edit: use commit, but stop for amending
        • fixup: use commit contents, but meld it into previous commit and discard the commit message
        • drop: remove the commit
# 1) Git Rebase as an alternative to `git merge`
# git rebase {main}
git switch feat/branch
git rebase main

# when merge conflicts happen follow the instructions given
# it usually includes the following steps
# resolve conrflicts & git add 
# then git rebase --continue

# 2) Git Rebase an a cleanup tool
# git rebase -i {commit}
git rebase -i HEAD~4

# pick a00001 msg1
# reword a00001 use_this_message
# fixup a00002 msg3
# fixup a00002 msg4

Tags:

Categories:

Updated: