master-wiki/void/Omnivore/2024-03-29 - Let's Save the (Git) Trees.md

6 KiB
Raw Permalink Blame History

id title status tags date_added url_omnivore url_original
228a5a0a-eded-11ee-9840-8b8ae5e4b6f0 Let's Save the (Git) Trees ARCHIVED
read-later
Newsletter
2024-03-29 13:55:23 https://omnivore.app/me/let-s-save-the-git-trees-18e8b23331a https://omnivore.app/no_url?q=b542dc42-874d-446c-84a7-6c02bf819f29

Let's Save the (Git) Trees

Highlights

We start using something called interactive rebase on our feature branches. Its really just a way to clean up our commit history before we merge it into the main branch. This might change the history, but since its just on our own feature branches, it doesnt mess up anything for everyone else.

source


Heres a quick guide on how to do it:

  1. Start on Your Feature Branch:git checkout -b my-feature
  2. Grab the Latest Main Branch Updates Without Switching:git fetch origin main:main (using main:main is a trick to fetch remote main changes to the local copy without switching back and forth)
  3. Interactive Rebase Time:git rebase main --interactive - This lets us pick and choose which commits to keep or squash, change the commits you want to s to make sure git squashes them into the parent commit, while keeping the changes.
  4. Safely Update the Remote Branch:git push --force-with-lease - This flag makes sure we dont accidentally overwrite anyone elses work.
  5. Prepare to Merge:git checkout main
  6. Merge Without Extra Commits:git merge --ff-only - - Keeps our history straight by avoiding merge commits.
  7. Push it Up:git push - And were done!

source


Original

Hi friends,

You know how companies use merge commits when working on projects?

Just go to one of your work projects and checkout the history of the main branch. It may look tidy (or not) but its bad for the environment (the real-world one 😉)

Lots of merge commits

While thats pretty standard, it actually makes things messy.

Every time we do this, it stops us from keeping our commit history nice and clean all the way through from development to production.

Heres why thats a bit of a problem: if weve already built and tested a commit, installing dependencies in the process and storing all its details, theres really no need to go through all that again.

Its like doing the same work twice, which wastes time and resources, which translate to environmental effects that compound and another financial waste if thats not enough.

Plus, a cleaner git history makes it easier for us to figure out issues. It helps us use tools like git bisect much more effectively to find bugs. But well talk more about that another time.

So, whats the solution?

==We start using something called== ==interactive rebase== ==on our feature branches. Its really just a way to clean up our commit history before we merge it into the main branch. This might change the history, but since its just on our own feature branches, it doesnt mess up anything for everyone else.==

Rebasing 3 commits into one with interactive rebase

==Heres a quick guide on how to do it:==

  1. ==Start on Your Feature Branch:====git checkout -b== ==my====-feature==
  2. ==Grab the Latest Main Branch Updates Without Switching:====git== ==fetch== ==origin== ==main====:main== ==(using== ==main:====main== ==is a trick to fetch remote main changes to the local copy without switching back and forth)==
  3. ==Interactive Rebase Time:====git rebase main== ==--interactive== ==- This lets us pick and choose which commits to keep or squash, change the commits you want to== ==s== ==to make sure git squashes them into the parent commit, while keeping the changes.==
  4. ==Safely Update the Remote Branch:====git push --====force====-====with====-lease== ==- This flag makes sure we dont accidentally overwrite anyone elses work.==
  5. ==Prepare to Merge:====git checkout main==
  6. ==Merge Without Extra Commits:====git== ==merge== ==--ff-====only== ==-== ==- Keeps our history straight by avoiding merge commits.==
  7. ==Push it Up:====git== ==push== ==- And were done!==

By making this part of what we do every day, our git trees will stay clean and easy to work with.

Hope you find this helpful! Always happy to hear your thoughts or answer any questions.

Enjoy your weekend!

Whenever youre ready, heres how I can help you

Built with ConvertKit