4

I am using Visual Studio (Professional 2019) and the Git integration there through Git Changes. I have always been using TFS, but we have decided to use Git projects on new development. I have added some stuff to the .gitignore file, like for ignoring the .vs folder etc. And it seems to be working as long as things are not already added in before. So far, so good.

But now we are hitting an issue where we have different changes in files like launchSettings.json and appsettings.Development.json files as well as the App_Data folder.

The problem I don't understand is that we all want these files to be controlled in Git, to exist in the repo. But we want to exclude local changes form ever being pushed anywhere.

So we found information about the .git/info/exclude file. And that's great. But it does not seem to solve the problem, because once a file is controlled, we can't exclude our own changes to that file. It only applies when the files is not controlled at all. Here's where I'm not sure at all, but it seems that way.

When we used TFS control, we were able from Visual Studio to just right-click the file and choose "Exclude" and it would appear in the "Excluded changes" list and not being checked in with the changeset.

But that's not possible in Git. At least not from Visual Studio. The only options available are Undo changes in addition to Blame (Annotate) and Stage. But we don't want to undo our changes. We just want to push our changes without including the changes.

What am I doing wrong here? Please enlighten me. I'm clearly missing something here.

Mats Magnem
  • 1,375
  • 1
  • 10
  • 21
  • 1
    Stage the changes you want to push. If you don't stage anything, VS automatically stages everything. – fredrik Mar 29 '21 at 10:21
  • Does this answer your question? [How to stop tracking and ignore changes to a file in Git?](https://stackoverflow.com/questions/936249/how-to-stop-tracking-and-ignore-changes-to-a-file-in-git) – PWND Mar 29 '21 at 11:07
  • @PWND - I would prefer to not having to write commands rather than having it in Visual Studio. To me, that would be inefficient. But if there are no other options, I may have to start using commands. – Mats Magnem Mar 29 '21 at 11:22
  • 1
    @fredrik - What impact of staging? To me, staging means something completely different, as it's a step before putting to production. – Mats Magnem Mar 29 '21 at 11:22
  • 2
    In git, you have three states for a file under changes. First there's modified unstaged - when you've changed a file but not staged it for commit. Then there's modified staged, when you've told git that you want to commit the file next time you do a commit. And then there's committed, which can in turn be pushed to a remote. – fredrik Mar 29 '21 at 11:40
  • The analogy with pushing to production is quite apt - but this is all locally, you stage it to commit. – fredrik Mar 29 '21 at 11:41
  • @MatsMagnem - if you want to ignore some files for everyone - just use .gitignore. You can use a default .gitignore file for VS - https://github.com/github/gitignore/blob/master/VisualStudio.gitignore – PWND Mar 29 '21 at 11:41
  • You stage files by using [`git-add`](https://git-scm.com/docs/git-add) - in command line. VS has equivalent things as you've seen when right clicking the files. – fredrik Mar 29 '21 at 11:42
  • @PWND I have tried adding things to .gitignore. But because I want the file to be controlled and exist in Git, it will always try yo apply my changes. I want the file to exist in Git, but to ignore my local changes within the files. I'm not sure how to explain better than in the post. – Mats Magnem Mar 29 '21 at 14:11
  • @fredrik - Then something may be strange with the Git integration in Visual Studio. Because if I stage a file, I am not able to do anything else than to commit the stage. But I want Visual Studio to not include my changes when I commit and push. Do it with everything else, but leave certain file changes out of the commit+push. There is really something I'm missing here. – Mats Magnem Mar 29 '21 at 14:15
  • I have been too long in the TFS world. This is so easy in TFS control. I just right-click the file and choose "Exclude", then I can check in all my changes except that file, ad I don't have to undo anything. It would be very strange if Git does not support this :-) – Mats Magnem Mar 29 '21 at 14:18
  • Read https://learn.microsoft.com/en-us/visualstudio/ide/git-with-visual-studio?view=vs-2019 or ignore VS and use command line – fredrik Mar 29 '21 at 14:49
  • Thank you guys, for all the input. I'm looking in to the commands now. – Mats Magnem Mar 30 '21 at 17:26

1 Answers1

2

Team Foundation Version Control (TFVC) is a centralized version control system. In the meanwhile, Git is a distributed version control system. They are different version control system thus own different actions. See: Choosing the right version control for your project for details.

In TFVC repository, we could just right-click the file and choose "Exclude", then we check in all changes except that file as this thread: How to ignore files/directories in TFS for avoiding them to go to central source repository? described.

However, this action is not available in Git repository, we could follow this thread: How to stop tracking and ignore changes to a file in Git? as PWND suggested to use Git commands to implement this. Or we could use Git stage command to avoid this file to be commit as fredrik suggested, see: https://www.nobledesktop.com/learn/git/stage-commit-files for details.

In addition, you could try to set up branch policies, so code merging to master must via pull request, and then protect the target files.

Edward Han-MSFT
  • 2,879
  • 1
  • 4
  • 9