In this post, we’ll walk through the process of configuring a Git client using VS Code and managing branches in Azure DevOps. This will cover everything from setting up credentials to committing changes and syncing branches with a remote repository.
Step 1: Configure Git Client
To begin, we’ll configure VS Code as our Git client, but feel free to use any other Git client you prefer.
Open VS Code.
Go to the terminal and open a new terminal.
Switch the terminal to Git Bash.
Run the following command to save your credentials:
git config --global credential.helper wincred
Set up your username and email for Git:
git config --global user.name "Your Name" git config --global user.email "your-email@example.com"
Step 2: Clone a Repository from Azure DevOps
Log into Azure DevOps and navigate to your project (e.g., Parts Unlimited).
Go to Repos on the left-hand menu and click Clone.
You can either generate Git credentials or copy the repository URL.
Select Clone in VS Code from the dropdown, and it will prompt you to open VS Code.
Choose or create a new folder in VS Code to clone the repository into.
If prompted, log into your Microsoft account to authenticate.
Once authenticated, your credentials will be saved, so this step is a one-time process. After cloning, the entire project will be visible in VS Code.
Step 3: Making Changes and Committing
Let’s make a small change in the code:
Open a file, for example, from the
Migration
folder.Add a comment (e.g.,
// First change
), and save the file usingCtrl + S
.You will see the change in the Source Control tab, but it's still not committed to the remote repository.
To commit the change:
Add a commit message, like "My first change."
Click Commit to save it locally.
Click Sync Changes to push your changes to the remote repository.
You can now view the commit history in Azure DevOps under Commits, where you will see the latest commit.
Step 4: Staging and Committing Specific Changes
Let’s explore how to stage specific changes:
Make changes to two files and save them.
In Source Control, click the + symbol next to one of the files to stage that change.
Add a commit message (e.g., "Change 2") and commit the staged file only.
Now, sync the changes to the remote repository.
The staged changes will be pushed, but the unstaged file will remain as a local change.
Step 5: Understanding Branches
Branches allow you to work on different versions of your codebase without affecting the main code. When you clone a repository, a local branch (usually master
) is created. The changes made on this branch are synced to the remote master branch.
To create a new branch (e.g., dev
) in VS Code:
Click on master at the bottom left of the screen.
Select Create new branch from master.
Name the branch
dev
, and hit Enter.
Now, any changes made will be on the dev
branch.
Step 6: Syncing and Deleting Branches
To sync the branches:
Click Sync Changes, and the new
dev
branch will also be synced to the remote repository.You can view the new branch under Branches in Azure DevOps.
To delete the remote branch:
Go to the three dots next to the branch in Azure DevOps and click Delete Branch.
In VS Code, run
Ctrl + Shift + P
, type Git: Delete Branch, and select the localdev
branch to delete.
Lastly, to remove the unused remote branch:
- Run the command
Ctrl + Shift + P
and type Git Fetch Prune. This will fetch and remove any deleted remote branches locally.
Check the output logs to track what changes were made.
Stay tuned for Part 2, where we’ll cover how to manage branches and pull requests directly from Azure Repos