How to Git
Post date: Aug 13, 2021 5:56:28 PM
Initializing your project Repo and Commit/Push to Remote Origin
When you create a project in Azure DevOps, there are multiple options to start using Git on your project. These options are mentioned below:
Push an existing repository
This is the best option to use when setting up a project from scratch. This option requires you to create a local new Git repository and create a project then commit and push it to the remote origin.
Follow the steps below to do all of the above:
Create a project directory using Command Prompt
md <project_name>
cd <project_name>
Initialize local Git Repo:
git init
Open Visual Studio or Code and create a project using a suitable template in the directory. If you are using donet CLI or SPFx (generator-sharepoint) then create a project using dotnet or SPFx commands in the directory.
The template will scaffold the project files. Once ready, use the following command to add project files to local repo.
git add .
Check the status:
git status
Commit the files to local repo:
git commit –m “added project files for the first time”
Add remote origin in Azure DevOps repo URL where you have created the project:
git remote add origin https://HarrisCounty-US@dev.azure.com/HarrisCounty-US/<project_name>/_git/SPFxRushDemo
Push the code from local repo to master branch in remote repository.
git push --set-upstream origin master
Creating branches on the Remote/Origin repo using Azure DevOps
How to create a branch for work items in remote repo
Select the backlog item in the current sprint and Create a branch.
Select as many related items and name the branch. This will create a remote branch.
Go to local prompt and do git pull. This will bring the newly created remote branch
Make sure to set the current head to newly created local branch by:
git branch (to list all branches)
git checkout <new-branch-name>
Work on the code and make changes to files which needs updating.
Once done updating and compiling successfully with tests success, go to prompt and run:
git status
git add .
git commit –m <message>
git push
This will update the sync the remote branch
Go to Azure DevOps and create a pull request
Once the pull request is reviewed and approve, complete the merge with deleting the branch.
This will update everything back to the master branch.
How to pull the changes back to local repo
Pull the changes from remote to local by running:
git branch master
git pull
The master branch will have latest updates
How to remove heads for all remote branches on local repo
After the git pull and having latest changes to the local branches, you can prune the remote branches in local repo by:
git remote prune origin
The above command will destroy local copies of remote/origin branches.
NOTE: You can also using the command below if you have not deleted the remote branch in Azure DevOps:
git branch –rd <branch-name>
git pull
This will remove remote and local branches
git fetch origin
How to remove local branches from local repo when they are not needed
Get the local versions of branches in local repo:
git branch –vv (this will give you all local branches)
Delete the local branch
git branch –d <branch-name>