Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I highlight changes from current branch in VsCode?

VsCode highlights lines of code with green color (it is ~2px vertical stripe on the left) that haven't been committed to git repository yet.

It's very convenient, but I like to do git add . && git commit -m "Save" to save my progress. So this highlighting drops.

I do all of my changes in my own branch. Is there any way in VsCode to highlight lines of code that have been modified in current branch?

like image 784
Roman Pushkin Avatar asked Oct 09 '18 21:10

Roman Pushkin


1 Answers

There likely isn't a way to do this. This isn't specific to VsCode either, I would be surprised if any editor can do this because that's not how git works. Commits don't 'belong' to a branch. What has been 'modified in the current branch' depends on what you're comparing the current branch to.

Instead of commits 'belonging' to a branch, all commits in the repository form a tree* structure, and branches just point to a location on that tree.

Consider the following 2 examples:

    A---B---C---D master
         \
          E---F  feature1
               \
                G---H  feature2
    I---J---K---L master
         \
          \       O  feature3
           \     /
            M---N
                 \
                  P  feature4

Which branch do commits E and F belong to? What should we compare feature2 against? Should we compare feature2 against F? D? B?

Which branch do M and N belong to? What should we compare feature3 against? N? L? J?

Hopefully you can see that it's not quite right to say that E, F, M or N 'belong' to any one branch. We might instead say that E and F are contained in feature1 AND feature2, or that M and N are in feature3 AND feature4.

There's also no 'right' answer to the questions about what to compare feature2 and feature3 against. The answer is going to depend on the context in which you're asking the question, such as what the contents of E, F, M and N are, and what it is you want to know about feature2 and feature3.

Whatever the context and information you're looking for, you can probably ask git for that information from the command line. See e.g. Get all files that have been modified in git branch, git - changes to branch since created?, and How to get the changes on a branch in Git. However, an automated process to display that information in an editor is a different matter.

Getting back to your question now, the editor doesn't know in the above examples I gave what it is you're looking for. Theoretically it might be possible for an editor to provide a configurable setting. Instead of always showing unstaged and uncommitted changes (i.e. compare the working tree to HEAD), maybe you could tell it to always compare the working tree to the merge-base of origin/master and HEAD, but I'm unaware of any editors which provide such an option. Such an option would also likely run into some weird edge cases, like when the commits don't have a common ancestor.

*: If you want to get technical and you know a little graph theory, it's actually not a tree but a Directed Acyclic Graph because the parent-child relationship between commits is asymmetric (i.e. the graph edges are directed), commits can have more than one parent (merge commits), and you can have multiple root commits (commits with no parent).

like image 196
Daniel McIntosh Avatar answered Oct 13 '22 16:10

Daniel McIntosh