Skip to Main Content
TWIL
git
Image for 'This Week I Learned' blog post featuring tips on managing Git branches including renaming and syncing with remote changes.

Dive into this edition of 'TWIL,' our weekly expedition into the ever-evolving landscape of tech wisdom. This week, Emily enlightens us on Git, sharing savvy techniques for Managing and Updating Branches with Rewritten History. Discover how to align your local branch with remote changes, seamlessly renaming branches to preserve your work or to freshen up your repository for that crystal-clear PR.

Managing and Updating Branches with Rewritten History

There are times where you may need to overwrite your local branch to match a remote - maybe the branch was cherry-picked or rebased by another person to fix conflicts, etc.

# Update local branch 
# - Retrieve the current version of the branch
git fetch origin <branch_name>
# - Force your local version of the branch to match the new version you fetched
git reset --hard origin/<branch_name>

If you need to force-update a local branch but don't want to lose your history locally, you can rename the branch first:

# - Rename the branch you currently have checked out
git branch -m <new-branch-name>

# - Rename a branch by name
git branch -m <old-branch-name> <new-branch-name>

Alternatively, you can push a local branch to a different name. Often, I'll checkout a new branch with the clean-prs prefix and cherry-pick to that. That way, my original local branch remains intact.

# Push a branch as a different name
# I usually read the ":" as "as"
# e.g. "push clean-prs/feature/new-thing as feature/new-thing"
git push clean-prs/feature/new-thing:feature/new-thing
  • Git
Emily Morehouse's profile picture
Emily Morehouse

Cofounder, Director of Engineering

Related Posts

Digital pioneer journeying through technological quirks, reminiscent of vast waterfalls and rocky terrains
April 5, 2017 • Kyle Misencik

Infinitely dispatching actions + React, Redux & React Router v4

While implementing JWT for an application, we ran into a quirk with React Router v4. Here, we hope to show how to implement protected client-side routes while avoiding some of the pitfalls we encountered.

Graphic illustration of Emily sharing tips on Remote Branch Rebase using Git and creating Markdown-Powered Slides with DeckSet, symbolizing workflow optimization for developers and presenters.
May 10, 2019 • Frank Valcarcel

TWIL 2019-05-10

Join us in this episode of TWIL for a deep dive into workflow efficiency with Emily. Elevate your developer and presenter skills with these insightful tips.