Git with Visual Studio and DevOps

Using git with Visual Studio and DevOps

By Fabio Tavares
|

โœจ Git Installation


To start, you will need to install git on your machine, if you don't have it already.

To test, open the Command-Line or Power Shell and type git.

If you have it installed, the output should look something like this.

PS C:\> git
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

In case you need to install it, go to git-scm and choose your OS version.

After the installation is finished, reopen all console windows and try to run the git command once again. This time you should have the previous output.

Make sure you close all console windows after you install git.

๐Ÿ”‘ Create an SSH key and add it to DevOps


With an SSH key you will be able to do any git operation between your machine and the remote git server without writing your username or password, or if you choose, just the password.

If you already have an SSH key that you want to use, jump to Step 5. To list all your SSH keys run the command "ls -al ~/.ssh" on Git Bash.

The process to create the key on your machine is simple (As described in Github Help ):

  1. Open Git Bash

  2. Paste the text below, substituting in your GitHub email address.

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  3. When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location. The output should be similar to :

    Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
    
  4. When you're prompted to "Enter passphrase", if you leave it black, you won't need to add the password on git operations. It's highly recommended to set a password!

Now that you have created an SSH key, you need to add it to DevOps.

  1. Locate the .ssh folder where the keys are located. If, on step 3 you choose the default location, the location should be "C:\Users[yourWindowsUsername].ssh".

  2. Inside this folder, you will find a file called "id_rsa.pub". Open this file with nodepad.exe and copy the content to the clipboard.

  3. Go to DevOps and click on your avatar, top right, and select Security option and then, on the left menu, select "SSH public keys".

  4. At this point, you should see all your previously added key and an Add button that you need to click and insert your SSH public key stored on the clipboard.

At this point you have your SSH key created. ๐ŸŽ‰ ๐ŸŽ‰

๐Ÿ‘ Clone the project


Now that you have git installed and running on your machine, you will need to get the source files.

First, locate the project inside DevOps and then go to Repos.

On the top right side of the window, you will find the clone button.

repo buttom

Now you have three options to manage your local repositories :

  1. Use the command line.
  2. Use a dedicated app.
  3. Use Visual Studio built-in functionality.

For the sake of simplicity, you will use the third option, Visual Studio built-in functionality.

clone repo

Follow the following steps to clone the project:

  1. On the Popup, copy the SSH link.
  2. Open Visual Studio and navigate to Team Explorer tab (on the top menu View).
  3. Click the clone link under Local Git Repositories and past the Repository link copied previously.
  4. Make sure you select the correct destination folder and press the Clone button.
  5. If a prompt appears, asking for the password, insert it and press ok.

clone repo

After this, you should have all the files on your local machine.

You can open the project and start doing your changes.

๐ŸŒต Create a Branch


Either if you clone a new project or if you are starting a new story on an old project, you will probably need to create a new branch to isolate your changes.

To create a new branch follow the steps:

  1. Navigate to the Branches option on Team Explorer Tab.

Master Branch

  1. Update the Master branch by right-clicking on the master branch (or the branch from where you are branching) and select Pull.

Name Branch

If you want to branch from a remote Branch and you can't find it, right-click on any remote branch and select Fetch.
  1. Click on New Branch.

Name Branch

  1. Insert the name of your branch using any naming conventions you want.
  2. Select to the branch from where you want to create your branch.

Name Branch

  1. Click on Create Branch button.

At this point, you have your new branch created and checked-out, and you can start doing your changes.

๐Ÿ’พ Committing your code changes


Now that you've made some changes, you will need to commit those changes.

To do that, go the Team Explorer tab, and select home (small house icon).

At this point you should be able to see all uncommitted files, that were changed since the last commit.

To commit follow the steps:

  1. Select the files that you want to commit.
  2. Promote them to the staging area, right-click on the selected files, and select the Stage option.
  3. Write a commit message. This message should describe the change that you did.
  4. Click the button "Commit All".

After these steps, the files should disappear.

โ™ป๏ธ Sync local changes with remote Repository


Now that the changes are committed, you need to send your changes to your remote branch.

If more than one person is working on the same branch, there is a risk that your local branch is behind by a few commits.

This means that you first need to pull all changes, merge them on to local branch, solve all merge conflicts if any, and then finally push them.

๐Ÿ‘‡ Pull changes from the Remote Repository

On Team Explorer, go to Sync and you will see only your changes.

It looks like you have your branch updated with the remote branch.

before fetch

But if you click on Fetch you will check if the remote branch has any changes.

after fetch

And if you have select Pull, and there are no conflicts, the changes from the remote branch will be merged into your local branch.

โ˜๏ธ Push changes to the Remote Repository.

Now that your local branch is updated with your remote one, you can push your changes, clicking on Push.

Make sure you do a Push right after the pull, otherwise someone else can do a push to that branch and you will need to do the Fetch/Pull/Push process all over again.

๐Ÿ“š Create a Pull Request.


Once you pushed your branch to your remote branch, you may need to have a code review from one of your peers, to make sure that your code won't break the parent branch.

Azure DevOps, like many other Git providers, offers a functionality called Pull Request.

This allows you, to request a review of your code, and if there are no problems to be merged into the parent branch.

To create a Pull Request on Azure DevOps follow the steps:

  1. In Azure DevOps, navigate to your project and then on the Repos option, select Pull Requests.
  2. At the top right corner, select the New pull request.
  3. On the dropbox select your branch and the parent branch.
  4. You will be able to add a title and description where you provide more information to the person that will do your code review.

pull request

  1. You can also request a person or group to review your code, by searching for them in the Reviewers field.
It's possible that some projects have automatic Reviewers, that will be added to every Pull Request.
  1. Review your Pull Request.
  2. Click on Create.

โœ… Solving Conflicts.


When you are working on a team, there is a chance that another person, changed the same section of code. When this happens, and you try to merge the two pieces of code, git doesn't know which piece should be kept, and will trigger a merge conflict.

To solve the Merge Conflict follow the following steps:

  1. To see the files, where you have conflicts, click on the conflicts link.

merge conflict 1

  1. Click on one of the files, and then on Merge to see the pieces of code that are conflicting with each other.

merge conflict 2

  1. Select the change that should remain.

merge conflict 3

  1. Check the bottom window to see how the code will change following your proposed merge. You can have multiple conflicts on the same file. On the top, you can see the number of conflicts remaining and navigate between them. Once you solve all conflicts in one file click Accept Merge on the top left.

merge conflict 4

  1. When all conflicts are solved, it will be possible to commit the merge and push the change to the remote Repository.

merge conflict 5

If you have a conflict between your code and any other person, before you overwrite their work, TALK to them and try to understand the best option TOGETHER

๐Ÿ’ฑ Move uncommitted changes to a different branch

If for some reason, you add some changes to the wrong branch, and they are not committed yet, you can move them to another branch.

To do this follow the steps:

  1. Right-click in one file and select Stash All. This should add all file to the Stashes area.

Stash 1

  1. Change to the correct branch. You will see that you still have files on the Stashes area. You won't see the files but will have an item on that list.

Stash 2

  1. Right-click on the item on the Stashes area and then select the Pop > Pop All as Unstaged option. After this, you should see all file on the Changes area.

Stash 3


SHARE ARTICLE

The End