this post was submitted on 31 Jan 2024
211 points (98.6% liked)

Git

2958 readers
20 users here now

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Resources

Rules

  1. Follow programming.dev rules
  2. Be excellent to each other, no hostility towards users for any reason
  3. No spam of tools/companies/advertisements. It’s OK to post your own stuff part of the time, but the primary use of the community should not be self-promotion.

Git Logo by Jason Long is licensed under the Creative Commons Attribution 3.0 Unported License.

founded 2 years ago
MODERATORS
 
all 15 comments
sorted by: hot top controversial new old
[–] [email protected] 14 points 11 months ago (2 children)

Merge. One of the values of a VCS is to preserve history in detail, and merge is the only method that does that. Also, it's easy to foul a remote branch with the other methods if someone has already pushed changes to branch 1.

[–] [email protected] 3 points 11 months ago

Shared branches should always only move forward. Most Git-* systems support stuff like protected branches.

I personally like tidying up your own feature branch with rebasing and then merging it into main (preferably using only FF merges). However this is not scalable for some larger projects, and for example monorepos also make this hard to accomplish. In those cases the solution ends up being squash+merge.

The extra information about the squashed commits is usually persisted to these systems (GitHub PRs, GitLab MRs, etc) so you don't really lose much, I guess. Although I do prefer keeping it all in plain git.

[–] [email protected] 2 points 11 months ago* (last edited 11 months ago)

I feel it's a balance. Each operation has a purpose.

Rebasing makes sense when you are working in a feature branch together with other people so you rebase your own commits to keep the feature branch lean before you finally merge it into the main branch, instead of polluting the history with a hard to follow mess of sub branches for each person. Or when you yourself ended up needing to rewrite (or squash) some commits to clean up / reorganize related changes for the same feature. Or when you already committed something locally without realizing you were not on sync with the latest version of a remote branch you are working on and you don't wanna have it as a 1-single-commit branch that has to be merged.

Squashing with git merge --squash is also very situational.. ideally you wouldn't need it if your commits are not messy/tiny/redundant enough that combining them together makes it better.

[–] [email protected] 7 points 11 months ago

I've tended to rebase before anything else. If I feel unsure about the massive interactive rebase fuckery I'm about to embark on, I just make another branch where I start. Then undoing it's fairly trivial.

[–] elrik 4 points 11 months ago

Just use merge with informative PR titles, descriptions and linked work items. Reviewing history is then trivial and it has none of the pitfalls for less experienced devs.

[–] [email protected] 3 points 11 months ago

I'm only just embarking on my git journey as a hobbyist. When programming was my career, I was a solo programmer and subversion was almost overkill.

When I look at the diagram of "merge" I see what I would have thought to be perfection itself, not something pain inducing.

As I said, I'm just getting started. Is there no tooling to make this graph painless and useful or is it left to mental visualisations?

[–] HeapOfDogs 3 points 11 months ago

Squash4Lyfe

[–] [email protected] 3 points 11 months ago (1 children)

I was trying to learn this again last week. I just play around with this stuff for fun.

If I want to consolidate all the commits into a a single message (to create a changelog sort of), which kind of merge do I use?

Another question: I'm torn between wanting to keep a complete history of my work, for my own benefit, and not wanting anyone to see how messy and crappy everything is. I've been trying to work in one branch then merge only when a task is "complete". But it's a bit confusing for me especially if I leave a project for a while then come back to it. Especially especially if submodules are involved. Is there some sort of convention about how to do this? Or am I thinking about it wrong?

[–] AnyOldName3 4 points 11 months ago (1 children)

You want to have commit history, not a commit fairy tale. Once you start rewriting history, it's not really history any more. The stuff people want to hide tends to be some of the most useful to someone looking through the history to find out how things became the way they are and what was going through the author's mind when it was written. If things are messy and crappy, it's better to know that rather than have it covered up.

[–] [email protected] 2 points 11 months ago

commit history, not a commit fairy tale

I'll remember that! Makes sense.

[–] [email protected] 2 points 11 months ago* (last edited 11 months ago)

I personally love the graphs merge creates. A branch clearly leaves the main line and clearly gets pulled back in.

That graphic makes it look worse by not using clean straight lines.