AZ-400: Designing and Implementing Microsoft DevOps Solutions
Work with Azure Repos and GitHub
Working with Git locally
Learn how to work effectively with Git on your local machine. This guide covers setting up Git, initializing repositories, making commits, branching strategies, merging, and cloning remote repositories—all via the command line. While we demonstrate these commands in a terminal, graphical interfaces are also available for those who prefer a visual approach.
Configuring Git
Before you make any commits, configure your Git username and email. This metadata is associated with each commit you make. You can set these values globally (for every repository on your machine) or locally (for a specific repository). The example below demonstrates the global configuration:
git config --global user.name "Jeremy Morgan"
git config --global user.email "[email protected]"
If you need repository-specific settings, run the commands without the --global
flag from within the targeted repository.
Tip
For a one-time project-specific setup, configuring locally ensures your project uses the correct credentials without affecting other repositories.
Initializing a New Repository
Follow these steps to create a new project, initialize it as a Git repository, and add your first file.
- Create a project directory and switch to it.
- Initialize the repository.
- Create a file named
hello.txt
and add some content.
# Create project directory and initialize repository
PS C:\Users\jeremy\Projects> mkdir my-project
PS C:\Users\jeremy\Projects> cd .\my-project\
PS C:\Users\jeremy\Projects\my-project> git init
Initialized empty Git repository in C:\Users\jeremy\Projects\my-project\.git\
# Create a new file
PS C:\Users\jeremy\Projects\my-project> echo "Hello Git!" > hello.txt
After adding the file, check the repository status to view untracked files:
PS C:\Users\jeremy\Projects\my-project> git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
Now, stage the file and commit your changes:
PS C:\Users\jeremy\Projects\my-project> git add .
PS C:\Users\jeremy\Projects\my-project> git commit -m "Initial commit: add hello.txt"
[master (root-commit) 395883b] Initial commit: add hello.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hello.txt
PS C:\Users\jeremy\Projects\my-project> git status
On branch master
nothing to commit, working tree clean
Finally, view the commit log to verify the commit:
PS C:\Users\jeremy\Projects\my-project> git log
commit 395883b84fd2cc3bd62a061190231962f8d40e8 (HEAD -> master)
Author: Jeremy Morgan <[email protected]>
Date: Tue Sep 17 16:22:43 2024 -0700
Initial commit: add hello.txt
The commit log reflects your configuration and shows that Git correctly recorded your commit history.
Creating and Merging a Branch
To work on a new feature without immediately affecting your main branch, create a new branch. In this example, we’ll create a branch named 1900-feature
, modify the file, and subsequently merge it back into the master branch.
Follow these steps:
- Create and switch to the new branch.
- Edit
hello.txt
to update its content. - Stage and commit the changes on the feature branch.
- Merge the feature branch into master.
# Create a new branch and switch to it
PS C:\Users\jeremy\Projects\my-project> git branch 1900-feature
PS C:\Users\jeremy\Projects\my-project> git checkout 1900-feature
Switched to branch '1900-feature'
# Verify current branch
PS C:\Users\jeremy\Projects\my-project> git branch
* 1900-feature
master
# Edit the file (change "Hello Git!" to "Hello Azure Repos!")
# Then check the repository status:
PS C:\Users\jeremy\Projects\my-project> git status
On branch 1900-feature
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.txt
# Stage and commit the modification
PS C:\Users\jeremy\Projects\my-project> git add hello.txt
PS C:\Users\jeremy\Projects\my-project> git commit -m "Update hello.txt: change to Azure Repos"
[1900-feature 7c4495d] Update hello.txt: change to Azure Repos
1 file changed, 0 insertions(+), 0 deletions(-)
# View commit log in the feature branch
PS C:\Users\jeremy\Projects\my-project> git log
commit 7c4495d5b979336b3b54b0b62a505921943e36d (HEAD -> 1900-feature)
Author: Jeremy Morgan <[email protected]>
Date: Tue Sep 17 16:24:59 2024 -0700
Update hello.txt: change to Azure Repos
commit 395883b84fd2cc3bd62a061190231962f8d40e8 (master)
Author: Jeremy Morgan <[email protected]>
Date: Tue Sep 17 16:22:43 2024 -0700
Initial commit: add hello.txt
Switch back to the master branch and merge the feature branch:
PS C:\Users\jeremy\Projects\my-project> git checkout master
Switched to branch 'master'
PS C:\Users\jeremy\Projects\my-project> git merge 1900-feature
Updating 395883b..7c4495d
Fast-forward
hello.txt | Bin 26 -> 52 bytes
1 file changed, 0 insertions(+), 0 deletions(-)
# Verify the merged changes
PS C:\Users\jeremy\Projects\my-project> type .\hello.txt
Hello Azure Repos!
These steps illustrate essential Git practices for branch management: keeping your main branch stable while developing new features.
Cloning a Repository
Cloning is a common Git operation that allows you to copy an existing repository from remote hosts like GitHub or Azure DevOps. Use the HTTPS URL of your repository when cloning.
For example, to clone a repository from GitHub:
PS C:\Users\jeremy\Projects> git clone https://github.com/jeremykodekloud/My-cool-project.git
After cloning, you can add a file, commit the changes, and push them back to the remote repository:
# Example of adding and committing a file in the cloned repository
PS C:\Users\jeremy\Projects\My-cool-project> echo "Hello from My Cool Project!" > hello.txt
PS C:\Users\jeremy\Projects\My-cool-project> git status
PS C:\Users\jeremy\Projects\My-cool-project> git add hello.txt
PS C:\Users\jeremy\Projects\My-cool-project> git commit -m "Add hello.txt with welcome message"
PS C:\Users\jeremy\Projects\My-cool-project> git push
Similarly, to clone from an Azure DevOps repository:
PS C:\Users\jeremy\Projects> mkdir test
PS C:\Users\jeremy\Projects> cd .\test\
PS C:\Users\jeremy\Projects\test> git clone https://[email protected]/KodeKloudDemo/KodeKloud%20Hotel/_git/SmartHotel360
Cloning into 'SmartHotel360'...
remote: Azure Repos
remote: Found 46 objects to send. (23 ms)
Unpacking objects: 100% (46/46), 1.87 MiB | 3.68 MiB/s, done.
Remember
After cloning a repository, you can modify files, commit those changes, and push them back to update the remote repository.
Graphical User Interfaces
While command-line Git operations are powerful, multiple graphical interfaces can simplify your workflow. Below are several popular tools:
Tool | Key Features | Image |
---|---|---|
Visual Studio Code | Integrated Git support with a rich code editor and Git panel | ![]() |
Visual Studio 2022 | Clone repositories, switch branches, and view the Git Changes panel | ![]() |
GitHub Desktop | User-friendly interface for cloning, committing, and pushing changes | ![]() |
These interfaces simplify Git operations if you prefer not to use the command line.
Conclusion
In this article, we explored key Git operations for working locally:
- Configuring Git with the proper username and email.
- Initializing a repository and tracking changes with
git add
andgit commit
. - Viewing commit history using
git log
. - Managing branches using
git branch
,git checkout
, andgit merge
. - Cloning repositories from remote platforms like GitHub and Azure DevOps.
Mastering these basic operations will enable you to work efficiently with Git, whether you choose the command line or a graphical interface. For more in-depth guides and advanced Git techniques, explore the Git Documentation and GitHub Guides. Happy coding!
Watch Video
Watch video content