by cupofjoakim · 3y ago · 0 comments
Not sure if this solves the issue. I personally recommend using git, but keeping it simple by limiting the allowed operations.
- git switch - git checkout - git add - git commit - git pull - git push - git rebase - git reset
Note the absence of merge and and cherry pick. Most often when a junior has messed something up it seems to be because of merging in a weird order or cherry picking the wrong thing before merging or rebasing.
Either my juniors have stopped asking me for advice (totally possible) or this recommendation helps. As I find myself very seldom needing those commands I hope and believe it's the latter :)
You’re mostly free to do what you want on your dev/name branch. The only thing you really need to teach “juniors” is that they will hate themselves unless they merge main into their dev branches regularly. But if you’re heading home and want to commit something, or you simply commit as often as you click cmd+s, you can do it with no useful comment because it’ll get squashed anyway. Most people where I work use GitKraken, mostly out of habit, but I think more and more are switching to VSC extensions and some might even use the cli.
The result is that we spend almost 0 time on “git” in our organisation. I wonder if we do things wrong somehow since git seems to cause issues for so many people.
Others obsess over history, but personally didn’t find it worth the effort. We look back maybe twice a year, typically fixing issues moving forward.
They should create branch of development/trunk do commits with their work and create PR. Then PR merge in GitHub interface or something.
98% done other 2% get some git specialists.
I use git cherry-pick very occasionally. I agree it's probably a mistake in most cases, but there are valid heavily constrained circumstances where it's fine.
I had never heard of git switch. It looks like just another git checkout?
switch just does what people think checkout does.
It's not, because Git history is immutable [0], you can't change it. But I understand that from the user perspective it appears to change, and the consequences for public history "changes" have to be known to prevent any troubles.
> It is by design impossible to modify or delete an existing commit with regular Git commands.
These are both just straight up incorrect. Make the commit unreachable from all regular branches (this can be as simple as a reset or rebase - and at this point is effectively rewriting history), then gc. Oh, ah you say, there’s a reflog. But that can be pruned/expired with the very same regular reflog command you refer to in your post (or just change the unreachable expiry from 30 days to now). At that point the commit is truly unreachable and a gc will make it go away forever.
FYI the commands git reflog expire --expire=now --all && git gc --prune=now should do the trick about truly deleting commit objects for good.
Also the filter commands allow rewriting history.
This makes a difference because your wording implies that there is some inherent structural invariant that makes git history immutable - and there certainly isn’t, there’s just some default policy for how otherwise unreachable objects are recoverable.
Also I think when most people say (correctly [0]) that git history is mutable it is that by default it is easy to rewrite the history of any branch and basically manipulate the commit graph in any way, and all can be done with the standard command line tools. Bringing in the reflog is mostly irrelevant to the concept of history rewriting.
> But I understand that from the user perspective it appears to change
If you force push to a shared repo, there's no "perspective" about it - the history has changed, somebody else may basically get a completely unrelated graph to what they have in their own repo.
Bottom line: The person you're replying to is correct, rebase is a fundamentally destructive operation, because while the objects in git are immutable the references are not and that's all that matters - because a commit without a reference might as well not exist, it's not a history, it's just a commit. Starting with rebase you can prove to yourself that you can make the repo dispose of commit, tree and blob objects forever. I see the distinction that you are trying to make but I don't see why it is either pedagogically or practically a useful one.
[0] https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History (no scare quotes needed)
You've stated a lot of ways to delete commits that I don't see as "regular" (gc, changing default expire date, filter, reflog, ...). There are even more plumbing commands to delete commits. But yes, I should have explained what I mean with "regular" and also make my point less absolute.
> If you force push to a shared repo, there's no "perspective" about it - the history has changed.
It depends on what "history" and "changing" means to you. If it's the entire Git graph, then every commit changes the history (which it does). What I mean by change in this context is that I can take a graph and alter the existing commits (change order, move, change the content, etc.), and that's not what happens with a rebase for example. I should have clarified this as well. It comes down to the "branches are just pointers" discussion, so for me, moving a branch does not "change" the history (= the commit graph). But I also understand that from a different perspective - viewing the branch as the pointer + the commits it reaches - the history actually changes (but not in the way I've meant it). It's important to state which of those one is talking about.
Don't see what the big deal is.
In my opinion, the reason for all of this is due to how `git log` shows history. If you merge a branch, then every commit in the branch is now scattered among your mainline history, making it confusing to see what is going on.
So, instead of fixing `git log`, you end up rebaseing (and possibly squashing) on top of the main branch, then doing a fast forward commit so you get a "clean, linear history".
But, if git just change the default of `git log` to `git log --first-parent`, then so much of this would go away.
Only top level commits (directly to the branch, fast-forward merged, or a merge commit) show up in the history.
Now, let's say I have a branch that I've been working on for 2 weeks. I have 20+ commits. During that time, I've already needed to merge the main branch into my branch to keep it up to date several times.
I can either rebase the whole thing, to clean up history, remove the merge commits from main, squash unnecessary changes, and so on, just so I can get a "clean" history in main, or, we can just do a merge commit bringing in my whole branch with a good commit message.
I prefer the merge commit because it is then very easy to track _all_ of the changes made in my branch by a single commit. I can do a diff on the merge commit and see all changes. I can cherry-pick just the merge commit and bring in all the changes. If I need to break down what the original developer of the branch was doing, I can walk through all the sub-commits.
There is no need for them to rewrite history and hide their workflow or their process.
But again, the problem is, by default, `git log` shows this as a huge mess, whereas `git log --first-parent` would _only_ show the merge commit, and would hide all the child commits that happened in the branch. After all, the top level merge commit is what is important, we only care about the subcommits when we need to dig in.
Not really.
If you don’t care about the individual commits that lead to a merge (you only care about the final merge) then you could just do `git commit --amend` through the whole process. The end result would be the same.
But presumably these people (who aren’t narrowly focused on a linear history) do care. And (as you seem to be saying) sometimes you want to go down into the second parent of the merge commit.
And in that history (with just merges) there’s a bunch of random “synch. points” where they merged the original branch into theirs. Why? Maybe because it had been three hours, maybe it was the start or end of the day, or something else?
Can you look at the three-way diff and see why?
And then there are all sorts of half thought out commits and missteps that could have been edited out if they used rebase.
Using rebase does have drawbacks. But you can get back value proportional to the time you invest in it. So it’s not a simple matter of discovering that `--first-parent` is a thing and then setting an alias for that (really, you thought that would be it?).
And using merges only (including synch. merges) also has its advantages. But no, using rebase is not just about hiding pull request branches.
In the orgs I’ve worked in, it was important that mainline always (bugs notwithstanding) always be ‘runnable’. Many people (myself included) will happily commit in-progress changes to branches they’re working on. If these in-progress changes get into the mainline branch - even as a result of a merge - they become part of its history and break the ‘always runnable’ property.
The more-complicated but still safe use is to rebase your local branch on top of an advanced upstream before pushing. Some people prefer merge for this case, but it is somewhat impure.
The problematic use of rebase is on pushed branches. Especially if you have collaborators.
Yep. As soon as you have a PR out, and have to address comments, you have a public branch and can't safely rebase it. So you can't roll those new changes in to the "clean" history, If upstream also has changes before your PR gets merged, you'll also have to add a merge commit anyway.
Hint: “git rebase -i HEAD~3” is your friend. So is “git bisect”.
I've used almost every source control system known to humans the past several decades and git for over a decade.
I've heard the git rebase preachers. I tried to get onboard with rebase.
All who read, avoid rebase at all costs at all times, always.
Rebase in my experience at work led to inexplicable conflicts several times I tried. When that happens the rebase preachers will be nowhere to be found, and you’re on your own.
Your preaching is contentless.