News & Posts‎ > ‎

How to Git

posted Aug 13, 2021, 10:56 AM by Ashar Khan

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: 

  1. Create a project directory using Command Prompt 
    md <project_name> 
    cd <project_name> 

  1. Initialize local Git Repo: 
    git init 

  1. 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. 

  1. The template will scaffold the project files. Once ready, use the following command to add project files to local repo. 
    git add . 

  1. Check the status: 
    git status 

  1. Commit the files to local repo: 
    git commit –m “added project files for the first time” 

  1. 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 

  1. 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 

  1. Select the backlog item in the current sprint and Create a branch. 

  1. Select as many related items and name the branch. This will create a remote branch. 

  1. Go to local prompt and do git pull. This will bring the newly created remote branch 

  1. Make sure to set the current head to newly created local branch by: 

  1. git branch (to list all branches) 

  1. git checkout <new-branch-name> 

  1. Work on the code and make changes to files which needs updating.  

  1. Once done updating and compiling successfully with tests success, go to prompt and run: 

  1. git status 

  1. git add . 

  1. git commit –m <message> 

  1. git push 

  1. This will update the sync the remote branch 

  1. Go to Azure DevOps and create a pull request 

  1. Once the pull request is reviewed and approve, complete the merge with deleting the branch. 

  1. This will update everything back to the master branch. 

How to pull the changes back to local repo 

  1. Pull the changes from remote to local by running: 

  1. git branch master 

  1. git pull 

  1. The master branch will have latest updates 

How to remove heads for all remote branches on local repo 

  1. After the git pull and having latest changes to the local branches, you can prune the remote branches in local repo by: 

  1. git remote prune origin 

  1. 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 

  1. git fetch origin 

How to remove local branches from local repo when they are not needed 

  1. Get the local versions of branches in local repo: 

  1. git branch –vv (this will give you all local branches) 

  1. Delete the local branch 

  1. git branch –d <branch-name> 

 

Comments