1

I am using git on VS2019

I was wondering if there is a way to remove old commits but always keep the latest code. The reason is that when I work on a big change, I do a lot of commits after checking every small step but at the end it is annoying when I click on view history and need to search an old commit to compare the changes. I would like to remove specific commits but leave the changes. In that way I will see on history only the main milestones of the developing process. There is a way to do that??

3 Answers3

0

It seems that you want the git rebase feature.

If you know how many commits you want to squash together, you can use

git rebase -i HEAD~N

You can also do it in interactive mode:

git rebase --interactive HEAD~N

Be aware that if you merging commits you have already pushed, you may have trouble with your next push.

For more details, see: https://www.internalpointers.com/post/squash-commits-into-one-git

gmatht
  • 835
  • 6
  • 14
0

You can follow a workflow similar to "Git better with fixups " from Atul Sharma

For the commits which are incremental improvement for the same step, use git commit --fixup small step, which allows you to "do a lot of commits after checking every small step".

(You can see it in Git Extension in Visual Studio)

But at the end, you can cleanup all those small steps with git rebase --interactive --autosquash <First step commit> (assuming you have not pushed those commits yet)

That way, you end up with a cleaner history.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

It sounds like in these scenarios, you're developing a new feature, in which case you should be using a branch+pull request approach to development. During the pull request, you can do a squash merge. It's not all in the command line, but that might be for the best anyway.

If you're already working within a feature branch, then I like VonC's approach.

zhanga
  • 89
  • 2
  • 10
  • how can I do it in visual studio? –  Jun 19 '20 at 15:02
  • 1
    Unfortunately, I have no experience with VS. The best I can do is point you to the documentation from Microsoft: [branch creation](https://learn.microsoft.com/en-us/azure/devops/repos/git/branches?view=azure-devops&tabs=visual-studio) and [pull requests](https://learn.microsoft.com/en-us/azure/devops/repos/git/pullrequest?view=azure-devops) – zhanga Jun 19 '20 at 22:16