How to work with Multiple Branches in Git?

Login to Azure DevOps (ADO) and navigate to Project (In this case Sample) >> Repo >> Branches

Multiple Branches

Now to clone one of the branches to the local machine (mostly Main branch)


      1. Select the branch and click on “Clone” on the top right

Multiple Branches

      2. Select “Clone in VS Code” and then click on Open

Multiple Branches

Multiple Branches

      3. Select the folder to clone the branch and click “Select as Repository           Destination” and click Open in New Window.

Multiple Branches

Multiple Branches

Note: Git Bash Terminal is being used for git commands.

      4. For switching to another branch, “checkout” command to be used.

          $ git checkout <branchName>

Multiple Branches

      5. Always get updated files before working on any changes. To get                updated files from ADO, “pull” command to be used.

       $ git pull

Scenario:
Assuming that a user is working on Branch A (Updates) but is not in a stage to commit, but have to work on another branch i.e., Branch B (restore) on priority.
User must Stash the changes first and then can move to Branch B.
“git stash temporarily shelves (or stashes) changes you’ve made to your working copy so you can work on something else, and then come back and re-apply them later on.”
       1. First Save and then Stash the changes
           $ git stash
       2. Switch to Branch B by using checkout command

Multiple Branches

     3.  After the changes, following activities to be performed to push          
        changes to Branch B
           a.  Status: displays the state of the working directory and the       
                 staging  area.
                 $ git status
           b.  Add: adds a change in the working directory to the staging area.
                $ git add.
           c. Commit: sends the latest changes of the source code to the     
                repository.
               $ git commit -m “some hotfix”    //Add a Message to the commit.
           d. Push: upload the changes to the repository.
               $ git push

Multiple Branches

       4.  Now the changes will be reflected in ADO repo.
       5.  Move to Branch A and get stashed work to continue working on     
            the previous tasks.
       6.  Stash apply: overwrites files in the current working tree but leaves
            the stash history alone.
            $ git stash apply

Multiple Branches

 

Leave a Reply