How to set up GIT Repository in Azure DevOps

Prerequisites

You must have an organization and project in Azure DevOps. When you create a project, Azure DevOps automatically creates an empty repository in Repos.

1. Install Git command-line tools

Install one of the following Git command-line tools:

  • To install Git for Windows check this link

https://dynamics365techy.com/2023/05/git-github-window-os/

 

2. Clone the repo to your computer

To work with a Git repo, you clone it to your computer, which creates a complete local copy of the repo for you to work. Your code might be in one of several places.

  1. Complete the following step that's applicable to your scenario:
    1. If You don't have any code, first Create a new GIT repo in your project, and then complete the next step.
    2. If the code is in another GIT repo, such as a GitHub repo or a different Azure Repo instance, import it into a new or existing empty GIT repo, and then complete the next step.
    3. If the code is on your local computer and not yet in version control, either create a new GIT repo in your project or add your code to an existing repository.
  2. From your web browser, open the team project for your organization and select Repos > Files.

 

A screenshot of a computerDescription automatically generated with medium confidence

  1. Select Clone in the upper-right corner of the Code window and copy the URL.

image

image

4. Open the Git command window (Git Bash on Git for Windows). Go to the folder where you want the code from the repo stored on your computer, and run  git clone , followed by the path copied from Clone URL in the previous step. 

See the following example:

git clone <repository-url>

Git downloads a copy of the code, including all commits, and branches from the repo, into a new folder for you to work with.

5. Switch your directory to the repository that you cloned

cd njain-demo

3. Work in a branch

Git  branches  isolate your changes from other work being done in the project. We recommend using the  Git workflow , which uses a new branch for every feature or fix that you work on. For our examples, we use the branch DevOps3650.

  1. Create a branch with the  branch  command.

git branch DevOps3650

 

This command creates a reference in Git for the new branch. It also creates a pointer back to the parent commit so Git can keep a history of changes as you add commits to the branch.

Tip

If you're working with a previously cloned repository, ensure that you've checked out the right branch ( git checkout main ) and that it's up to date ( git pull origin main ) before you create your new branch.

  1. Use  checkout  to switch to that branch.

git checkout DevOps3650

Git changes the files on your computer to match the latest commit on the checked-out branch.

 

Tip

When you create a branch from the command line, the branch is based on the currently checked-out branch. When you clone the repository, the default branch (typically  main ) gets checked out. Because you cloned, your local copy of  main  has the latest changes.

git checkout main

git pull origin main

git branch  DevOps3650

git checkout  DevOps3650

You can replace the first three commands in the previous example with the following command, which creates a new branch named  DevOps3650  based on the latest main branch.

git pull origin main:  DevOps3650

Switch back to the Git Bash window that you used in the previous section. Run the following commands to create and check out a new branch based on the main branch.

git pull origin main:  DevOps3650

git checkout  DevOps3650

4. Work with the code

In the following steps, we make a change to the files on your computer, commit the changes locally, and then push the commit to the repo stored on the server.

 

  1. Browse to the folder on your computer where you cloned the repo, open any file in your editor of choice, and make some changes. Then, Save and close the file.
  2. In the Git command window, go to the  njain-demo  directory by entering the following command: 

cd njain-demo

  1. Commit your changes by entering the following commands in the Git command window: 

              git add .

           git commit -m "Commit 1"

The git add . command stages any new or changed files, and git commit -m creates a commit with the specified commit message.

Tip

Check what branch you're working on before you commit, so that you don't commit changes to the wrong branch. Git always adds new commits to the current local branch.

  1. Push your changes to the Git repo on the server. Enter the following command into the Git command window: 

      git push origin  DevOps3650

Your code is now shared to the remote repository, in a branch named  DevOps3650 . To merge the code from your working branch into the  main  branch, use a pull request .

5. Merge your changes with a pull request

Pull requests combine the review and merge of your code into a single collaborative process. After you’re done fixing a bug or new feature in a branch, create a new pull request. Add the members of the team to the pull request so they can review and vote on your changes. Use pull requests to review works in progress and get early feedback on changes. There’s no commitment to merge the changes because you can abandon the pull request at any time.

The following example shows the basic steps of creating and completing a pull request.

  1. Open the team project for your organization in your web browser and select Repos > Files . If you kept your browser open after getting the clone URL, you can just switch back to it.

image

  1. Select Create a pull request in the upper-right corner of the Files window. If you don't see a message like You updated  DevOps3650  just now , refresh your browser.

image

New pull requests are configured to merge your branch into the default branch, which in this example is development. The title and description are pre-populated with your commit message.

image

You can add reviewers and link work items to your pull request.

You can review the files included in the pull request at the bottom of the New Pull Request window.

A picture containing text, line, font, numberDescription automatically generated

  1. Select Create.

View the details of your pull request from the Overview tab. You can also view the changed files, updates, and commits in your pull request from the other tabs.

  1. Select Complete to begin the process of completing the pull request.

A screenshot of a computerDescription automatically generated

A screenshot of a computerDescription automatically generated with low confidence

 

  1. Select Complete merge to complete the pull request and merge your code into the development branch.

A screenshot of a computerDescription automatically generated with low confidence

Note

 

This example shows the basic steps of creating and completing a pull request. For more information, see Create, view, and manage pull requests.

 

View history

 

  1. Switch back to the web portal and select History from the Code page to view your new commit.

image

  1. Switch to the Files tab, and select the changed file to view your changes.

 

Clean up

Switch back to your Git Bash command prompt and run the following command to delete your local copy of the branch.

git checkout develoment

git pull origin develoment

git branch -d DevOps3650

This action completes the following tasks:

  • The git checkout  develoment  command switches you to the  develoment  branch.
  • The git pull origin  develoment  command pulls down the latest version of the code in the  develoment  branch, including your changes and the fact that  DevOps3650   was merged.
  • The git branch -d  DevOps3650   command deletes your local copy of that branch.

Posted

in

by

Tags: