This article describes how to use a working branch and then merge the changes to your default branch. A working branch is used to make changes and commits without any affect on your default branch. When you’re happy with the changes in the working branch, merge the commits to your default branch.

Visual Studio Code (VS Code) is used as the Markdown editor and the terminal for Git commands. This article uses the testrepo repository that was created in Git basics: Get started with Git and GitHub. If you need to set up your environment or create the repository, refer to that article.

Note
This article is written as a beginners guide for Git and GitHub. The repository is set up for one person to learn the command syntax. Some steps verify that a command worked as intended so that you can see the results and understand the concepts.

Branch overview

Repositories have several types of branches: default, remote, and working. The default branch is a repository’s single-source of truth. A working branch might also be referred to as a feature branch.

Branch Description
Default The repository’s branch named main. You can commit changes directly to main but a best practice is to merge commits from a working branch.

Use git branch to display the local clone’s list of branches. The branch tagged with an asterisk (*) is the active branch.
Remote Each branch has a remote branch in GitHub. For example, in your local clone, origin/main is the remote branch for main.

Use git branch -r to display your local clone’s remote branches.
Working Create a working branch from the main branch. The commit history is identical and the most recent commit is the base of the two branches. As you commit changes in the working branch, it diverges and its commit history is ahead of the main branch. Use git log to display the commit history.

Changes in the working branch don’t affect the default branch. To publish changes in your working branch, merge the working branch commits into the main branch.

Sync the repository

Make sure that your GitHub repository and your local clone are in sync. Changes can be committed on GitHub, so it’s a best practice to make sure you’re working with the most current files. This step becomes more important when you work with shared repositories where multiple people update a repository.

  1. Launch VS Code.
  2. Open your local clone’s directory: File > Open Folder.
  3. Go to C:\github\clonedemo\testrepo and choose Select Folder.
  4. Open a VS Code terminal: Terminal > New Terminal.
  5. Checkout the main branch.

     git checkout main
    

    The output shows that you’re on the main branch.

     Already on 'main'
     Your branch is up to date with 'origin/main'.
    
  6. Sync the main branch of your GitHub repository and local clone.

     git fetch origin
     git merge origin/main
    

    You use git fetch to pull any changes from GitHub to your local clone. The git merge command merges changes from the GitHub remote branch origin/main into your main branch. If the GitHub repository and local clone are in sync, Already up to date is displayed.

Create a working branch

Create a working branch in your local clone and push it to GitHub. You’ll use this branch to update files and commit changes.

  1. Create a working branch from the main branch.

     git checkout -b my-new-branch
    

    The git checkout command combines two tasks. The command creates a new branch and switches to the new branch to make it active. It’s similar to doing git branch <branch name> and git checkout <branch name>.

    The output shows that git checkout -b created a new branch named my-new-branch and then switched to that branch.

     Switched to a new branch 'my-new-branch'
    
  2. Verify the new branch is active.

     git branch
    

    The output lists the branches and the asterisk (*) confirms my-new-branch is active. Any changes to the repository’s files are now tracked in my-new-branch.

       main
     * my-new-branch
    
  3. Push your working branch to GitHub.

     git push origin my-new-branch
    

    The output shows that your working branch was pushed to your GitHub account’s repository.

     Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
     remote:
     remote: Create a pull request for 'my-new-branch' on GitHub by visiting:
     remote:      https://github.com/<GitHub account>/testrepo/pull/new/my-new-branch
     remote:
     To https://github.com/<GitHub account>/testrepo.git
      * [new branch]      my-new-branch -> my-new-branch
    

Edit a file

Use the working branch to add, change, or delete files. In this example, you’ll update the README.md file and push the commit to GitHub.

  1. In VS Code, open README.md.
  2. Add a sentence to the file and save the change.
  3. From the VS Code terminal, check the branch’s status.

     git status
    

    The output shows that in my-new-branch the README.md file was changed with the status Changes not staged for commit.

     On branch my-new-branch
     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:   README.md
    
     no changes added to commit (use "git add" and/or "git commit -a")
    
  4. Stage the updated file for commit and show the status.

     git add .
     git status
    

    The git add command stages the modified file so it can be committed. The period (.) indicates that any modified file will be staged. An alternative is to specify the file with git add README.md.

    The git status output shows Changes to be committed.

     On branch my-new-branch
     Changes to be committed:
       (use "git restore --staged <file>..." to unstage)
             modified:   README.md
    
  5. Commit the change to the file and include a commit message.

     git commit -m "updates README.md"
    

    The git commit -m command commits the change with the commit message within double-quotes. This message is displayed on GitHub and in the git log history.

    The output shows the successful commit and the commit’s unique hash: 9d377a0.

     [my-new-branch 9d377a0] updates README.md
      1 file changed, 1 insertion(+), 1 deletion(-)
    
  6. Verify the commit was successful.

     git status
    

    The output confirms there are no other changes that need to be added or committed.

     On branch my-new-branch
     nothing to commit, working tree clean
    
  7. Push the changes to my-new-branch on GitHub.

     git push origin my-new-branch
    

    The output shows the status as the commit is uploaded to GitHub.

     Enumerating objects: 5, done.
     Counting objects: 100% (5/5), done.
     Delta compression using up to 8 threads
     Compressing objects: 100% (2/2), done.
     Writing objects: 100% (3/3), 346 bytes | 346.00 KiB/s, done.
     Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
     To https://github.com/<GitHub account>/testrepo.git
        b897559..9d377a0  my-new-branch -> my-new-branch
    

Compare files

Compare files between your branches on GitHub. This comparison shows the differences between your main branch and my-new-branch.

  1. Sign in to GitHub.
  2. Go to your GitHub repository named testrepo and select the Code tab.
  3. Select the link that shows 2 branches. The number will vary if you’ve created other branches.
  4. Open main and my-new-branch in separate browser tabs.
  5. View the README.md file in each branch.

The README.md files are different because you changed the file in my-new-branch and that change doesn’t affect the main branch.

Merge changes

When the committed changes in your working branch are ready for publication, merge the commits to your main branch. When you push the commit to GitHub, your changes are live in the main branch.

  1. In a VS Code terminal, checkout the main branch.

     git checkout main
    
     Switched to branch 'main'
     Your branch is up to date with 'origin/main'.
    
  2. Merge the commits from my-new-branch into the main branch.

     git merge my-new-branch
    

    The output shows the changes.

     Updating b897559..9d377a0
     Fast-forward
      README.md | 2 +-
      1 file changed, 1 insertion(+), 1 deletion(-)
    
  3. Verify the status of the main branch.

     git status
    

    The output shows your local repository is one commit ahead of the remote.

     On branch main
     Your branch is ahead of 'origin/main' by 1 commit.
       (use "git push" to publish your local commits)
    
     nothing to commit, working tree clean
    
  4. Push the changes to the main branch on GitHub.

     git push origin main
    

    The output shows the status.

     Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
     To https://github.com/<GitHub account>/testrepo.git
        b897559..9d377a0  main -> main
    
  5. Compare the README.md files again and verify the files in main and my-new-branch contain the same content.

Clean up

When you’re done and no longer need the repository, delete your repository from GitHub and the local clone.

  1. From GitHub, there’s a drop-down menu at the top, right of the page. Select Your repositories
  2. Select testrepo.
  3. Select Settings and scroll down to Danger Zone.
  4. Select Delete this repository and follow the prompts.
  5. To delete your local clone, remove C:\github\clonedemo\testrepo.

Conclusion

In this article you created a working branch, updated a file, and merged the commit into your default branch. When you combine these skills with the skills learned in Git basics: Get started with Git and GitHub, you can maintain your repository. To learn about how to work in a shared respository, see Git basics: Create a fork and submit a pull request.