Back to Git & Dev Tools
2026-03-207 min read

current branch (Git & Dev Tools)

Learn current branch (Git & Dev Tools) step by step with clear examples and exercises.

Title: Mastering Git and Developer Tools: Current Branch


Why This Matters

In today's fast-paced software development world, version control systems like Git have become indispensable tools for developers. The ability to manage multiple versions of a project, collaborate with other developers, and track changes is crucial in ensuring the success of any software project. In this lesson, we will focus on understanding the current branch in Git and various developer tools associated with it.


Prerequisites

To fully grasp the concepts discussed in this lesson, you should have a basic understanding of:

  1. Git fundamentals, such as commits, branches, merges, rebases, and cherry-picking.
  2. Basic command-line navigation and manipulation.
  3. Familiarity with popular developer tools like Visual Studio Code, Atom, or Sublime Text.
  4. A working understanding of GitHub or other version control repository hosting services.
  5. Knowledge of common Git commands like git status, git log, git diff, and git stash.
  6. Familiarity with Git workflows such as Feature Branch Workflow, GitFlow, and Forking Workflow.

Core Concept

Understanding Branches in Git

A branch in Git represents an independent line of development for a project. The master (or main) branch is the primary branch where the latest stable version of the project resides. However, developers often create new branches to work on specific features or bug fixes without affecting the main branch.

Creating and Deleting Branches

To create a new branch, use the following command:

$ git switch -c <new-branch-name>

You can delete a branch using the git branch -d command followed by the branch name:

$ git branch -d <branch-name>

Switching Branches

To switch between branches, you can use the git checkout command followed by the name of the desired branch:

$ git checkout <branch-name>

If you want to create a new branch and switch to it at the same time, use the git switch command instead:

$ git switch -c <new-branch-name>

Merging Branches

Merging branches allows you to combine changes from one branch into another. To merge a feature branch into the master branch, first switch to the master branch:

$ git checkout master

Then, use the merge command followed by the name of the feature branch:

$ git merge <feature-branch>

If there are any merge conflicts, you'll need to resolve them manually or with a merge tool like Visual Studio Code's built-in Merge Conflict Editor.

Rebasing Branches

Rebasing a branch integrates all the changes from one branch onto another, creating a linear development history. To rebase a feature branch onto the master branch, first switch to the feature branch:

$ git checkout <feature-branch>

Then, use the rebase command followed by the name of the base branch (usually master):

$ git rebase master

Cherry-Picking Commits

Cherry-picking allows you to apply specific commits from one branch to another. To cherry-pick a commit, use the following command:

$ git cherry-pick <commit-hash>

Current Branch

The current branch is the branch that you are currently working on. You can view the name of the current branch using the git branch command:

$ git branch
* master
feature1
feature2

In this example, master is the current branch (denoted by the asterisk).


Worked Example

Let's illustrate the concepts discussed so far with a simple example:

  1. Create a new repository and navigate to it:
$ mkdir my-repo && cd my-repo
$ git init
  1. Create and switch to a new branch:
$ git switch -c feature1
Switched to a new branch 'feature1'
  1. Make some changes, add them, and commit:
$ echo "Adding feature 1" >> README.md
$ git add .
$ git commit -m "Add feature 1"
  1. Switch back to the master branch and make changes there:
$ git checkout master
Switched to branch 'master'
$ echo "Updating README for master" >> README.md
$ git add .
$ git commit -m "Update master README"
  1. Merge the feature1 branch into master:
$ git checkout master
$ git merge feature1
  1. Resolve any potential merge conflicts using a merge tool or manually editing files.
  1. Push both branches to a remote repository, such as GitHub:
$ git push origin master
$ git push origin feature1
  1. Create a new branch feature2 based on the latest version of the master branch and make some changes:
$ git checkout -b feature2
$ echo "Adding feature 2" >> README.md
$ git add .
$ git commit -m "Add feature 2"
  1. Cherry-pick the first commit from feature1 onto feature2:
$ git checkout feature2
$ git cherry-pick <commit-hash-of-first-feature1-commit>
  1. Resolve any potential merge conflicts, if any.

Common Mistakes

1. Forgetting to commit changes before switching branches

Always remember to commit your changes before switching branches, as uncommitted changes will be lost when you switch.

2. Merge conflicts

Merge conflicts can occur when two branches have conflicting changes in the same file. Resolve these conflicts manually or use a merge tool like Visual Studio Code's built-in Merge Conflict Editor to help with the resolution process.

Common Mistakes: Merge Conflicts (Subheadings)

Types of Merge Conflicts

  • Textual conflicts: when two branches have different versions of the same text in a file.
  • Structural conflicts: when two branches have different file structures, such as adding or removing files.

Resolving Merge Conflicts (Subheading)

  1. Identify the conflicting files by running git status.
  2. Open the conflicting files using your preferred text editor or a merge tool like Visual Studio Code's built-in Merge Conflict Editor.
  3. Review and resolve the conflicts, either by keeping one version of the code or creating a new solution that combines both versions.
  4. Save the file and stage it for commit:
$ git add <conflicting-file>
  1. Commit the resolved merge conflict:
$ git commit -m "Resolved merge conflict in <conflicting-file>"

Practice Questions

  1. How do you create a new branch and switch to it?
  2. What is the name of the current branch, and how can you find it?
  3. Describe the steps to merge a feature branch into the master branch using Git.
  4. You have made changes in a file on the feature1 branch but haven't committed them yet. What should you do before switching to another branch?
  5. How would you resolve a merge conflict using Visual Studio Code's built-in Merge Conflict Editor?
  6. Describe the difference between merging and rebasing branches in Git.
  7. You have created a new branch feature2 based on an outdated version of the master branch. How can you update your local feature2 branch to match the latest changes in the remote master branch?
  8. What command would you use to delete a remote branch on GitHub?
  9. Describe how cherry-picking works and provide an example.
  10. You have made a commit that introduces a bug. How can you revert that specific commit using Git?
  11. Explain the difference between git stash and git stash apply.
  12. What is the purpose of using Git hooks, and how do they help in maintaining code quality?
  13. Describe the benefits of using a feature branch workflow over a linear workflow like GitFlow or Forking Workflow.

FAQ

Q: Can I delete branches that are no longer needed?

A: Yes, you can safely delete branches once they have been merged into the master branch or if they are no longer relevant to the project.

Q: What happens when I switch back to a branch with uncommitted changes?

A: If you switch to another branch with uncommitted changes, Git will warn you that your changes will be discarded. You can choose to either stash your changes or commit them before switching branches.

Q: How do I rename a branch in Git?

A: To rename a branch, first create a new branch with the desired name and then merge the old branch into it. After that, you can delete the old branch. For example:

$ git switch -c new-feature1 feature1
$ git merge feature1
$ git branch -d feature1

Q: How do I revert a commit in Git?

A: To revert a specific commit, use the git revert command followed by the commit hash:

$ git revert <commit-hash>

This will create a new commit that undoes the changes made in the specified commit.

Q: What is Git stash? How does it help manage uncommitted changes?

A: Git stash allows you to temporarily save your uncommitted changes so that you can switch branches without losing them. You can then apply the saved changes back to your working directory when needed using git stash apply.

Q: What are Git hooks, and how do they help in maintaining code quality?

A: Git hooks are scripts that run automatically in response to specific events, such as commit or push. They can be used to enforce coding standards, perform automated tests, or trigger notifications when certain conditions are met. By using Git hooks, you can ensure that your code adheres to best practices and maintain a high level of quality.

Q: What is the difference between a feature branch workflow and a linear workflow like GitFlow or Forking Workflow?

A: A feature branch workflow involves creating a separate branch for each new feature or bug fix, developing it independently, merging it into the master branch once completed, and then deleting the feature branch. This approach promotes isolation, collaboration, and code review. In contrast, linear workflows like GitFlow and Forking Workflow involve more formalized branching and merging strategies that may include long-lived development branches and release branches. These workflows are better suited for larger projects with multiple contributors and complex release cycles.

current branch (Git & Dev Tools) | Git & Dev Tools | XQA Learn