Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
105 views
in Information Technology by (176k points)
Learn everything about Git Branching: Create, manage, and merge branches in Git with our step-by-step guide. Optimize your workflow using the best practices for Git Branches, GitHub, and version control.

Please log in or register to answer this question.

2 Answers

+1 vote
by (176k points)

Git Branch

Introduction

A branch in Git is a lightweight movable pointer to one of the commits. Branching allows you to create separate lines of development within a repository, enabling parallel development, experimentation, and feature isolation. By default, Git creates a branch called main (or master in older repositories).

Creating a New Branch

1. Check Existing Branches

To see all branches in the repository, you can use:

git branch 

This command will list all branches and highlight the current branch with an asterisk (*).

2. Create a New Branch

To create a new branch, use the git branch command followed by the branch name:

git branch feature-branch 

This command creates a new branch named feature-branch.

3. Switch to the New Branch

To start working on the new branch, you need to switch to it using the git checkout or git switch command:

git checkout feature-branch
# or
git switch feature-branch 

Now, you are on the feature-branch and can start making changes independently of the main branch.

Working with Branches

1. Making Changes

When you make changes in your working directory and commit them, they will be added to the current branch:

# Make changes to files
git add .
git commit -m "Add new feature" 

2. Merging Branches

Once you are satisfied with the changes in feature-branch, you can merge them back into the main branch.

First, switch back to the main branch:

git checkout main
# or
git switch main 

Then, merge the changes from feature-branch:

git merge feature-branch 

If there are no conflicts, the changes will be integrated into the main branch.

Handling Conflicts

Conflicts occur when changes in different branches overlap. Git will mark the conflicts in the affected files.

1. Identifying Conflicts

After attempting a merge, Git will notify you if there are conflicts:

Auto-merging filename
CONFLICT (content): Merge conflict in filename 

2. Resolving Conflicts

Open the conflicting file and manually resolve the conflicts. Git marks conflicts in the file like this:

<<<<<<< HEAD
Current changes in main branch
=======
Changes in feature-branch
>>>>>>> feature-branch 

Edit the file to keep the desired changes and remove the conflict markers (<<<<<<<, =======, >>>>>>>).

3. Marking Conflicts as Resolved

After resolving conflicts, you need to add the resolved files and commit the changes:

git add filename
git commit -m "Resolve merge conflict in filename" 

Deleting a Branch

After merging and ensuring the branch is no longer needed, you can delete it.

1. Delete a Local Branch

To delete a local branch, use the -d flag:

git branch -d feature-branch 

If the branch is not fully merged, Git will warn you. Use -D to force deletion:

git branch -D feature-branch 

2. Delete a Remote Branch

To delete a remote branch, use the push command with the --delete flag:

git push origin --delete feature-branch

Viewing Branch History

You can view the commit history of a branch using:

git log --oneline 

To see a visual representation of all branches and their commits:

git log --oneline --graph --all 

Example Workflow

Here’s a complete example of a typical branching workflow:

  1. Create and Switch to a New Branch:

    git branch new-feature
    git checkout new-feature
    # or combine both steps
    git checkout -b new-feature 
  2. Make Changes and Commit:

    # Make changes to files
    git add .
    git commit -m "Implement new feature" 
  3. Merge Changes Back to Main:

    git checkout main
    git merge new-feature
    
  4. Delete the Branch:

    git branch -d new-feature 

Branches in Git are a powerful feature that facilitate parallel development and effective management of changes. Understanding how to create, switch, merge, and delete branches is essential for efficient workflow management in Git.

+1 vote
by (176k points)

FAQs on Git Branch

Q: What is a branch in Git?

A: A branch in Git is a movable pointer to a commit. The default branch name in Git is master (or main in newer versions), and any other branch is simply a name for a different set of commits.

Q: How do you create a new branch?

A: You can create a new branch using the git branch command followed by the branch name.

git branch new-branch 

Q: How do you switch to a different branch?

A: To switch to a different branch, use the git checkout command followed by the branch name.

git checkout new-branch 

Alternatively, you can create and switch to a new branch in one command using git checkout -b.

git checkout -b new-branch 

Q: How do you list all branches in a repository?

A: To list all branches in your repository, use the git branch command.

git branch

Q: How do you delete a branch?

A: To delete a branch, use the git branch -d command followed by the branch name.

git branch -d new-branch 

Q: How do you merge one branch into another?

A: To merge a branch into the current branch, use the git merge command followed by the branch name you want to merge.

git merge new-branch 

Q: How do you resolve conflicts when merging branches?

A: If there are conflicts during the merge, Git will mark the files as conflicted and stop the merge process. You can resolve the conflicts manually by editing the files, then add the resolved files using git add and complete the merge with git commit.

Q: How do you rename a branch?

A: To rename the current branch, use the git branch -m command followed by the new branch name.

git branch -m new-branch-name

Q: How do you push a new branch to a remote repository?

A: To push a new branch to a remote repository, use the git push command followed by the remote name and the branch name.

git push origin new-branch 

Q: How do you track a remote branch?

A: To track a remote branch, use the git checkout command with the --track option.

git checkout --track origin/remote-branch 

Example Code

# Initialize a new Git repository
git init my-repo
cd my-repo

# Create a new file and commit it to the master branch
echo "Hello, world!" > file.txt
git add file.txt
git commit -m "Initial commit"

# Create a new branch and switch to it
git checkout -b feature-branch

# Make changes in the new branch
echo "Feature work" > feature.txt
git add feature.txt
git commit -m "Add feature work"

# Switch back to the master branch
git checkout master

# Merge the feature branch into the master branch
git merge feature-branch

# Delete the feature branch
git branch -d feature-branch

# Push the master branch to the remote repository
git remote add origin https://github.com/yourusername/your-repo.git
git push -u origin master 

This example covers the basics of creating, switching, merging, and deleting branches in Git, as well as pushing changes to a remote repository.

Important Interview Questions and Answers on Git Branch

Q: What is a branch in Git?

A branch in Git is a lightweight movable pointer to a commit. Branching allows you to work on different parts of a project separately. By creating a branch, you can isolate your work from the main codebase until it’s ready to be merged.

Q: How do you create a new branch in Git?

You can create a new branch in Git using the git branch command followed by the branch name.

git branch new-branch 

Q: How do you switch to an existing branch in Git?

You can switch to an existing branch using the git checkout command followed by the branch name.

git checkout existing-branch 

Q: How do you create and switch to a new branch in one command?

You can create and switch to a new branch in one command using git checkout -b followed by the branch name.

git checkout -b new-branch 

Q: How do you list all branches in a Git repository?

You can list all branches in a Git repository using the git branch command.

git branch 

Q: How do you delete a branch in Git?

You can delete a branch in Git using the git branch -d command followed by the branch name.

git branch -d branch-to-delete 

If the branch has not been merged, you can force delete it using -D.

git branch -D branch-to-delete 

Q: What is the difference between git merge and git rebase?

  • git merge integrates changes from one branch into another. It creates a new commit that combines the changes from both branches.
  • git rebase moves or combines a sequence of commits to a new base commit. It rewrites the commit history to produce a linear sequence of commits.

Q: How do you merge a branch into the current branch in Git?

You can merge a branch into the current branch using the git merge command followed by the branch name you want to merge.

git merge branch-to-merge 

Q: How do you rebase a branch in Git?

You can rebase the current branch onto another branch using the git rebase command followed by the branch name.

git rebase branch-to-rebase-onto 

Q: How do you resolve conflicts during a merge or rebase?

When conflicts occur, Git will pause the merge or rebase process and allow you to manually resolve the conflicts. After resolving the conflicts, you can continue the process.

To continue after resolving conflicts during a merge:

git add resolved-file
git commit 

To continue after resolving conflicts during a rebase:

git add resolved-file
git rebase --continue 

Example Code

  1. Creating and switching to a new branch:
git checkout -b feature-branch 
  1. Listing all branches:
git branch
  1. Merging a branch:
git checkout main
git merge feature-branch 
  1. Rebasing a branch:
git checkout feature-branch
git rebase main 
  1. Resolving conflicts:
# During merge/rebase, resolve conflicts manually in the files
git add resolved-file
# Continue rebase
git rebase --continue
# Or, if merging
git commit

Related questions

+1 vote
1 answer
+1 vote
1 answer
+1 vote
1 answer
+1 vote
1 answer
+1 vote
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...