How to avoid Visual Studio rebuild after switching Git branches back and forth?
Asked Answered
S

3

6

I use Git for source control and build with Visual Studio 2008. Mostly I build on one branch (master). Often I need to do a code review and switch to another branch (develop) temporarily. I do not build code that I review and after review is finished I switch back to the original branch:

  1. develop on master
  2. commit everything (branch is clean)
  3. build
  4. switch to develop
  5. do code review
  6. switch to master
  7. continue to develop on master
  8. build (recompiles many files, not only ones modified in previous step)

If master and develop have different versions of one file, the modification date and time for that file are updated to the moment of checkout after switching branches in step 5. This causes Visual Studio to rebuild them in the step 8 despite the fact that souce code have not changed.

How can I avoid massive rebuilds when changing branches?

Singley answered 10/2, 2012 at 0:41 Comment(1)
There is a good discussion about it here: #10356665Schaerbeek
B
1

You could script a checkout of just the differring files. So don't do a real checkout but alter the working directory to look like the other branch.

Brianna answered 10/2, 2012 at 6:29 Comment(5)
Sounds like a fairly complicated script. It is like reimplementing checkout functionality that preserves modification time. Or can I do this simpler? Existing script?Singley
Does Git actually store modification times or will I have to preserve them manually in such a script?Singley
It would not be a complicated script. You can get a list of files from git diff --statBrianna
Thanks for the tip. I will try to write one.Singley
Thanks. I didn't write the tool yet, but I guess this is the best answer I could get given my question.Singley
M
3

As a workaround, it might be useful for you to do code reviews in a different clone from your development clone. That way, switching to a review branch won't change the files in your master clone, which won't cause VS to unnecessarily rebuild them.

Myiasis answered 10/2, 2012 at 0:46 Comment(2)
Thanks for the proposal, but my repository is huge - over 3GB. I would like to avoid wasting space.Singley
git clone on a local machine hardlinks files by default when possible, so a clone might take up less than 3GB on your disk.Deadly
B
1

You could script a checkout of just the differring files. So don't do a real checkout but alter the working directory to look like the other branch.

Brianna answered 10/2, 2012 at 6:29 Comment(5)
Sounds like a fairly complicated script. It is like reimplementing checkout functionality that preserves modification time. Or can I do this simpler? Existing script?Singley
Does Git actually store modification times or will I have to preserve them manually in such a script?Singley
It would not be a complicated script. You can get a list of files from git diff --statBrianna
Thanks for the tip. I will try to write one.Singley
Thanks. I didn't write the tool yet, but I guess this is the best answer I could get given my question.Singley
R
0

git worktree works. It let you work on multi-branches and without switching.

Reckoner answered 8/4, 2020 at 3:7 Comment(1)
Can you explain how to use git workitree to fix the original post problem?Ineslta

© 2022 - 2025 — McMap. All rights reserved.