Print service provided by iDogiCat: http://www.idogicat.com/
home logo





Home > IT > Programming > Git in Visual Studio

Git in Visual Studio

Create Branch

  • Team Explorer | Branches | New Branch
  • give it a name like 'my.name/000001-a-desc-of-item'. By providing 'my.name/', it creates a folder called 'my.name'
  • choose 'master' for the new branch to based on
  • click 'Create Branch'

Delete Branch

Note: when a branch is checked out, it cannot be deleted. You can check out another branch (eg. master), and delete it.

  • Team Explorer | Branches
  • check out another branch if the branch is checked out
  • right-click on the branch to be deleted, and click 'Delete'

Work on files of the branch

  • check out branch
  • in 'Solution Explorer', if a solution has already been opened, and it's not the one you want to work on, then close the solution. Only when no solution is opened, you can see the 'Show Folder View' mentioned below
  • Team Explorer | click on 'Home' icon, in 'Solutions' part, there is a 'Show Folder View' link. Click on it, and browse to the file you want to work on (of course it's also fine to navigate to it using File Explorer). If you want to work on a solution, then double click on the solution file.

Commit

  • Team Explorer | Changes
  • enter a comment
  • choose 'Commit All and Push'

Note that you can also only do 'Commit' without the push. After doing several commits, you can push all of them together.

Combine ('squash') multiple commits into one

Git encourages multiple and frequent commits, but if you push them all to remote, all the tedious small commits will be pushed. You can choose to combine them into one commit then push, this process is called 'squash'.

In Visual Studio: Team Explorer | Home | Sync | Outgoing Commits, there should be multiple commits you made, if they have not been pushed yet.

Click 'Actions' and choose 'Open Command Prompt', a command prompt is open at project home dir.

Execute the following command:

git rebase -i HEAD~5

where the '5' is the number of commits to be combined. Press 'Enter', a new window is popped up, showing the commits starting with 'pick'. Edit this file to keep the first 'pick' while changing all the following 'pick' into 'squash', click the 'Save' (floppy disk icon) button, then press 'Esc'. (Note: if there is no 'Save' button, then just press 'Esc', and then type ':x!' to save.

Eg: in the window popped out:

pick fc0102f02 my 1st commit
pick e20f220c9 my 2nd commit

edit it into:

pick fc0102f02 my 1st commit
squash e20f220c9 my 2nd commit

At this time, another window is popped up with all the comments of these commits. Keep the one you want, or remove all of them and enter a new one, then save and exit.

Now the commits are combined into one in 'Sync' window of team explorer, with the new comment you just entered. You can then push it to remote.

Code Review and approval

  • Team Explorer | Home icon
  • at 'Team Foundation Server', click the link to open it in web browser
  • click 'Code | Pull Requests | Create a pull request'

Merge from master to branch

I am not very clear why sometimes it doesn't seem to work to do this in Visual Studio. So this can be done reliably with Gitext:

  • check out the branch
  • do a 'Fetch all'
  • 'Open pull dialog...', and in 'Remote branch', enter 'master', then click 'Pull' button.

Note that sometimes it shows that there are some changes that need to be reverted/stashed/commited before doing the merge. If you do a diff, you'll see that they are basically line ending issue (supposing that Visual Studio doesn't show any change existing). The way to work around this is:

  • confirm that only line ending differences are there: the following command launches a diff GUI tool instead of diff itself, and it tells you that file versions are the same but not binarily identical.

# diff
git difftool <filename>

# undo the local change on this file:
git checkout -- <filename>

# confirm it again that it's been un-done successfully:
git diff <filename>