VSTS, git and squashed commits

Status
Not open for further replies.

hongooi

Ars Tribunus Militum
2,244
Subscriptor
I'm in an interesting situation with source control in a current project, and I'd like people's opinions on it.

The setup: we use VSTS with a git repo. I'm using Visual Studio as my IDE, and my colleague is using Intellij (long story). I use the git integration in VS to handle my commits, while he does things from the commandline. So I'm a noob, sue me.

I encountered a weird problem when I tried to merge my pull requests into the master. The first PR would succeed, but subsequent PRs from the same branch would almost always run into file conflicts. These would always be the same files as I'd changed the first time; there were no issues with other people updating the repo.

We think it's because of the fact that he's enabled a policy to always force merges into the master to be squashed. What happens is that after my first (successful) merge, the history for the master will contain a single squashed commit, while the history for my branch will still have all the original commits. In particular, the SHA hash for the squash will be missing from the list of SHAs for my branch; and the upshot is that VSTS thinks my branch is now one update behind. This makes things go out of sync, and future merges will tend to fail.

This is the first time I've seen people use squashed commits when working with git. He claims that it's necessary to keep attributions straight when you have multiple overlapping commits from different people, but it sounds a bit dubious to me.

Is this a recommended practice? Does the fact that we're using VSTS and not GitHub/Bitbucket/whatever make a difference?

A kind-of related issue is that he works on a local copy of the master, and pushes to a new branch on the origin; whereas I create a new local branch and push to the same branch on the origin. This lets him avoid the problem, but I'm not sure if pushing to a different remote branch is possible with VS.


Got an email from him, it turns out he's really more of an SVN guy. But one point that he raised does seem pertinent: rolling back a big PR, that's made up of several small commits. This would be straightforward if the PR is a single squash, but less so otherwise.
 
Hmmm. A couple of things here.

The most important concept that distributed VCS systems enable is a distinction between "public" and "private" commits. Public and private here in the sense of access modifiers in a programming language: commits you make on your own branch and don't share are developer-private. Commits you push to a shared location are public.

Your private commits are implementation details and there for your convenience. Your public commits should be cleaned up and suitable for consumption by others. By the time a commit is shared, it should be a logical whole that atomically brings the repo from valid state to another. It should build, pass tests, and introduce a single conceptual change to the code base, such as a new feature or bugfix.

In that sense, squashing is good. The history of your private branch is worse than neutral on the shared branch; it's noise that makes history harder to follow. For that much, he's right.

However, automatically squashing seems kind of dumb. You can't automatically clean up a private history and turn it into a single, logically coherent commit, with a coherent commit message. That needs to hand-crafted by the author of the branch.

As for dealing with the conflicts over and over, you should rebase your local branch onto trunk after you push a squash. IIRC (been a few years), that can be configured to happen automatically. Maybe you need to just configure your client to always squash on push.

The weirdest thing in your post is the last paragraph. It doesn't make sense to push to a new branch on origin for every change. Everything has to eventually merge back into master somehow.
 

hongooi

Ars Tribunus Militum
2,244
Subscriptor
I think what he does is:

1. Make changes to local master
2. Push to remote branch X
3. Make PR from remote branch X to remote master
4. Merge PR
4a. delete remote branch X (this is done automatically by VSTS after merging)

and subsequently for later changes...

5. Make more changes to local master
6. Push to remote branch X (recreating it)
(etc)
 

Apteris

Ars Tribunus Angusticlavius
9,593
Subscriptor
He claims that it's necessary to keep attributions straight when you have multiple overlapping commits from different people
Not entirely sure what "attributions" means, but on the whole, no. Every git commit, squashed or otherwise, has an author. It's part of that commit's hash. This Git feature provides "only" data integrity, not an assurance that the person claiming to be Bob in commit aefc32451 is actually Bob, but you nevertheless have a clear provenance for every commit.

Is this a recommended practice?
It depends. masonk's arguments are valid. My opinion is, my commit history is a record of my thought process while implementing the thing, and might be useful in the future. And I kinda view squashing commits as busywork.

A kind-of related issue is that he works on a local copy of the master, and pushes to a new branch on the origin; whereas I create a new local branch and push to the same branch on the origin. This lets him avoid the problem, but I'm not sure if pushing to a different remote branch is possible with VS.
I don't see a meaningful difference between the two workflows. You both branch from master at a given commit, you both push your local changes to a remote branch that is not master, and you both use PRs to get your changes merged back into master.

rolling back a big PR, that's made up of several small commits. This would be straightforward if the PR is a single squash, but less so otherwise.
It depends. A big PR that merges cleanly into master can be rolled back straightforwardly, you just have to choose the correct parent commit (should be -m 1). If the big PR was intermittently updated with master, and now needs to be reverted, I'm not sure how that will shake out: perhaps a squash commit will be easier to revert here, perhaps not.

masonk":33av5j6y said:
However, automatically squashing seems kind of dumb. You can't automatically clean up a private history and turn it into a single, logically coherent commit, with a coherent commit message. That needs to hand-crafted by the author of the branch.
This.

TL;DR: ask him if he can disable the automatic squash. If not, squash your commits yourself. Also, don't add new commits to a branch once that branch has been merged to master. Once a branch has been merged to master, the branch should be deleted, the ticket should be closed, and further development should happen on a new ticket and branch.
 
Public commits need to be atomic. That's important for rollbacks, bisect, CI buildserver utilization, and for making history sane for other users.

If your private commits aren't atomic, it's not busywork to make them atomic before you push.

If you want a record of your thought process, just tag head before you squash. I guarantee that your private commits will never, ever be useful to anyone but you.
 

Jehos

Ars Legatus Legionis
55,560
Public commits need to be atomic. That's important for rollbacks, bisect, CI buildserver utilization, and for making history sane for other users.

If your private commits aren't atomic, it's not busywork to make them atomic before you push.

If you want a record of your thought process, just tag head before you squash. I guarantee that your private commits will never, ever be useful to anyone but you.
This is my thought too. If your master repo is squashing automatically to (presumably) ensure atomicity, go ahead and squash first so the auto-squash does nothing. I can't imagine ever caring what other devs did or didn't commit before they were done with a change.
 

Apteris

Ars Tribunus Angusticlavius
9,593
Subscriptor
Public commits need to be atomic.
All commits are atomic. Did you mean changesets?

That's important for rollbacks
I suppose. We roll back quite rarely at work. Then again, I suppose the value of squashed commits will be more apparent in larger companies.

A bigger commit is better for bisect than a smaller one?

CI buildserver utilization
Eh? Why?

and for making history sane for other users
I dunno, man. I've reviewed PRs with dozens of commits and I've reviewed PRs with a large number of changes but only a few commits. I prefer the former.
 
Public commits need to be atomic.
All commits are atomic. Did you mean changesets?

Not sure what you're asking here. In this context, a commit is a changeset. What I meant to say, whether I was clear or not, is my original post. A commit is atomic iff it is: "a logical whole that atomically brings the repo from [one] valid state to another. It should build, pass tests, and introduce a single conceptual change to the code base".


A bigger commit is better for bisect than a smaller one?
A commit that doesn't compile because you committed at 5:37 pm just before you went home for the day so that you could pull it onto you laptop later that night is going to break bisect. Commits need to build and pass smoke tests before bisect starts to work well.

CI buildserver utilization
Eh? Why?
So that you don't waste CI resources trying to regression test a codebase that's just broken. For instance, it takes more than hour just for my CI build to finally stop doing work if I check in a missing semicolon.

and for making history sane for other users
I dunno, man. I've reviewed PRs with dozens of commits and I've reviewed PRs with a large number of changes but only a few commits. I prefer the former.
PRs should be as small as possible, but not smaller.
 

Jehos

Ars Legatus Legionis
55,560
Public commits need to be atomic.
All commits are atomic. Did you mean changesets?

Not sure what you're asking here. In this context, a commit is a changeset. What I meant to say, whether I was clear or not, is my original post. A commit is atomic iff it is: "a logical whole that atomically brings the repo from [one] valid state to another. It should build, pass tests, and introduce a single conceptual change to the code base".

and for making history sane for other users
I dunno, man. I've reviewed PRs with dozens of commits and I've reviewed PRs with a large number of changes but only a few commits. I prefer the former.
PRs should be as small as possible, but not smaller.
Masonk gets git.

Especially on the first point, this is one of the fundamental differences between distributed source control and centralized source control. When you have your own repo, commit whatever you want. Stuff that won't build, stuff that is half-done, whatever. As long as you're committing to your private repo, treat it like Dropbox. I consider private commits to be no different from saving a file to my file system when working on it. It doesn't matter if it builds, I'm just saving my work so it won't get lost.

Public commits are different--those need to not break the build. So no half-built features, nothing that won't compile, nothing that won't pass tests. Ideally your public commit feeds right into CD/CI solution, so when your pull request or merge is accepted, the assumption is it's in production at that point.
 

ShuggyCoUk

Ars Legatus Legionis
10,388
Subscriptor++
If you're using github/github enterprise their "github flow" (NOT gitflow) is simple, sensible and easy to follow.

never work locally on master, always work on a branch (let's call it feature_x). Do whatever the hell you want there locally till you are either:
A) happy it's good to push to the rest of the world
B) happy there's enough of a skeleton of what you want to do that getting feedback from other people is a good idea.

While you're doing that feel free to merge from the remote master, "git fetch;git merge origin/master" will do that for you and regularly doing it will make your life easier

In both A and B cases push feature_x branch to the remote ("git push -u" should do that for you by default). Then open up a PR for it (hopefully all your automatic CI tooling is kicking in now telling you problems if they exist. Some other developer(s) get involved and help/make suggestions.

If there's change to do fo them locally and then re-push.

Eventually you (and your CI tooling and code reviewers) are happy it's ready for master.

Now at this point your branch is _technically_ public in that other people have seen it, and might have taken a copy of it, but at this stage it's basically about to become close to worthless. this is the only reasonable point for me where editing public history is reasonable. At this point if your branch is genuinely a series of 'atomic' changes that each produce a reasonable result and could be taken individually (as in you eliminate the last one for now and you'd still be happy with it hitting master) then a normal merge commit to master is fine.
Alternatively just squash the whole thing down into one commit, but do this yourself, taking care to write a decent commit message for the whole thing ignoring the temporary parts "add a test I missed", "fix typo" that don't matter to the rest of the world once this hits master. Keep the "(#N)" PR pointer - it makes life nicer.

As masonk said, if you followed the _very_ important advice that PR's should be small, but no smaller then this shouldn't be a big deal at all.

github will then strongly suggest you delete the branch. This is fine. If you need it again you can get it back after not too long, if you really want to keep it just keep it locally for a while yourself.

The resulting view of history on master is much easier to read.

At this stage the only thing you can screw up is forgetting that you haven't done a git pull (git fetch;git merge master) on you local master. It's easy to for get this, just get in that habit anytime you want to start a new branch, be cause you want to be branching from as close to the possible of the tip of master anyway.

If you accidentally commit to local master it's not a big deal. In most cases you didn't do anything else, so just branch now (to feature_y), then reset you view of master to exactly match the remotes ("git checkout -B master origin/master")

I have found this model extremely simple to work with. When I fuck things up, it's always locally and following the steps on https://blog.github.com/2015-06-08-how- ... -with-git/ solves it easily
 
Status
Not open for further replies.