Keeping a track of your changes and its history is very important for a developer. Azure Repos is a set of version control tools that you can use to manage your code. Whether your software project is large or small, using version control as soon as possible is a good idea. Version control systems are software that help you track changes you make in your code over time.
In this post we will push our code to the created repository in Azure DevOps using the Command line with Git. Check out my previous post where we saw Pushing your Code to Azure DevOps Repository from Visual Studio. We have already covered in that post, how to create a project in Azure DevOps. We will not cover here that portion.
- Git : Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
- Azure DevOps Account
- Azure DevOps Project
Push your Code to Azure Repo
You can make use of Git Bash or Command line for this purpose. Open the terminal from the project folder. To do that, on the navigation path type cmd and hit enter. It will open the terminal from the project directory.
To initialize empty local git repository, type the below command.
Next we need to add files to the staging area for Git. Before the files are committed to a repository, the files needs to be added to the Staging area. You can either add individual files or directories or add all unstaged files using below command.
git add .
For adding individual file, type the file name in place of the dot.
git add README.md
Now we need to commit our code changes made to the files to a local repository. Each commit will have an unique ID for the reference. It is important to add a commit message as well, that will tell us what changes we have made.
git commit -m "first commit"
Below is the output similar to this you will get when you commit any changes.
To see the branches in your repository, use the below command.
-M option, <oldbranch> will be renamed to <newbranch>. If <oldbranch> had a corresponding reflog, it is renamed to match <newbranch>, and a reflog entry is created to remember the branch renaming. If <newbranch> exists, -M must be used to force the rename to happen.
git branch -M main
To connect a local repository with a remote repository. In this case, our remote repository is Azure Repo. Please give the URL of your repository. You may be asked to provide your Azure DevOps credentials if you are doing it for the first time.
git remote add origin <Azure DevOps Repo URL>.git
You can find the URL from Azure DevOps. When you open the Repo in the portal, you will see that your repo is empty, there you will find the URL.
Finally we push our code to the Azure Repo.
git push -u origin --all
Below is similar response you will get when code is pushed.
Sometimes there are changes in your Azure Repo (may be changed by your team member) which are not present locally, in such cases, you might receive an error message as below.
So first we need to get the changes from remote repository to our local repository. This command will get all the changes to the local repo.
It is a good practice to pull the changes first and then make your changes on top of it. There are many other git commands available. I have covered the basic ones which is sufficient for you to get started. To learn more about the git commands, refer the official documentation here.