Skip to content

Getting Started with Git

Setting up

Follow the Microsoft guide to set up your authentication to Azure Repos. Once you've done that you can either create a repo (or ask your project administrator do so for you) or clone an existing repo (more likely case).

When you start with new code in a new repo, navigate to the folder where you’re going to develop your code and use this command:

git init [repository name]

Get Code

This section assumes you have an existing repository and need to pull down most recent changes so you can do your work.

Locate the repo in Azure DevOps and get the URL to clone the repo.

clone-button.png

clone-URL.png

Then open up a terminal and execute this command:

git clone [insert URL]

You could clone from a different branch with the following command:

git clone --branch [branch-name] [repo-URL]

Now that you have the code available you need to create a new branch to do your work.

git checkout -b [new branch name]

Note the -b above. That is to tell git to make a new branch (locally). If you were switching to an existing branch it’s the same command, just leave off the -b.

More information about recommended branching strategies, security and policies can be found here in our Wiki.

If you want to see all the branches you have locally use this command:

git branch

If you need to pull down changes that have been made to a branch, there are some options.

git fetch

Fetch is a relatively harmless command. It downloads from the remote repo but it does NOT integrate it into your working set of files. When working in the same branch as others you should fetch a lot.

git pull

The pull command is similar to fetch, but in addition to downloading the changes, it also integrates the changes into your working set of files. Sometimes this command can result in merge conflicts.

Push Code

Once you have some code that you’d like to integrate with a shared branch (develop or main – add section on branching strategy), you will want to push your code and allow colleagues to review the code via a Pull Request (PR).

First get all your changes on stage to be committed and pushed:

git add .

The . means that we’re adding all changed files in this folder. You could explicitly list out the specific files to add if you’d like by providing the name instead of the .

Next commit the files you’ve staged in the previous command:

git commit -m “[describe what you did]”

The -m flag indicates a message is coming next. That message is known as your commit message. This is really a note to remind yourself and inform your team what you did in this particular commit.

Now you’re ready to push things to the remote repo.

git push origin [your-branch-name]

Use the -u flag when it is the first time this particular branch is being pushed up. The u stands for upstream.

Keep in mind that you have to do all three of these commands (add, commit, push) to successfully push your code to the remote repo.

If you forget which commands you’ve done, you can check your status:

git status

Some additional commands that you should know once you're comfortable with the ones above are as follows:

git revert [commit-ID]

Just like it sounds the revert command lets you undo the changes you've made. The commit ID is the commit you want to go back to.

This is a more advanced command and should be used sparingly and carefully to avoid unwanted deletions. One advantage of this approach is that it does not undo commit history, so all commits are still available in the history.

git merge

We often do the merges in the Azure DevOps UI via Pull Requests but there are times when you might want to merge the most up to date version of main with your current work. In that case you would checkout the main branch locally and then merge that into your current local branch. Finally switch back to your local branch and continue working.

Next Steps

Give these commands a try in a sandbox repo, either created in your project or in the organization linked to your Visual Studio Enterprise (VSE) license if you have one. Another place to get more familiar with these commands and other git related concepts is in GitHub. Do not put your Medtronic related work here but you can create a personal account and follow this guide to experience the same training that Microsoft employees complete in their first month on the job.

Medtronic Enhanced GitFlow and Branching

medtronic-gitflow

References

  • https://dzone.com/articles/top-20-git-commands-with-examples
  • https://www.freecodecamp.org/news/10-important-git-commands-that-every-developer-should-know/