Software Engineering

Working in a Team with Git Branches

When I first started learning Git, I thought creating a branch copied all the files into another folder. That's not exactly what happens...

KUZUE
July 12, 2026
5 min read
10 views

Working in a Team with Git Branches

Git becomes even more powerful when working in a team.

Instead of everyone making changes directly on the main branch, each developer creates a separate branch to work on a feature or fix. This allows multiple people to work on the same project without interfering with one another.


How Git Branches Work

When I first started learning Git, I thought creating a branch copied all the files into another folder.

That's not exactly what happens.

When you create a branch, Git creates a new pointer that points to the same commit as the branch you created it from. It then moves HEAD to the new branch.

As you continue making commits, only that branch moves forward. The original branch remains unchanged until you merge your work back into it.

mermaid
gitGraph
    commit id: "Initial Commit"
    commit id: "Project Setup"

    branch feature/login
    checkout feature/login

    commit id: "Create Login Page"
    commit id: "Google OAuth"

    checkout main

[!NOTE] A Git branch does not create another copy of your project. It simply creates another pointer to the same project history, allowing you to work independently.


Understanding HEAD

HEAD represents the branch (or commit) you're currently working on.

Whenever you switch branches, Git moves HEAD to the branch you've switched to.

For example:

text
main
 │
 ▼
A──B──C
       ▲
      HEAD

After creating a feature branch:

text
main
 │
 ▼
A──B──C
       ▲
 feature/login
       ▲
      HEAD

Every new commit you make now belongs to feature/login.


Creating a Branch

To create a new branch and switch to it immediately, run either of the following commands:

bash
git switch -c branch-name

or

bash
git checkout -b branch-name

Example:

bash
git switch -c feature/login

Both commands perform the same operation.


Switching Between Branches

If the branch already exists, switch to it using:

bash
git switch branch-name

or

bash
git checkout branch-name

Example:

bash
git switch main

When you switch branches, Git restores your working directory to the snapshot stored in that branch. That's why files sometimes appear or disappear after switching—they only exist in the commits belonging to that branch.


Viewing Existing Branches

To list all local branches:

bash
git branch

Example output:

text
* feature/login
  main
  dev

The * indicates the branch you're currently working on.


Viewing Commit History

To view a condensed history of commits:

bash
git log --oneline

Example output:

text
5c71b8f Add Google OAuth
d91ac43 Create login page
f2bd9a1 Initial project setup

This command is useful when you need to quickly inspect your project's history.


Downloading Remote Changes

Git provides two commands for keeping your local repository up to date.

git fetch

Downloads the latest changes from the remote repository without merging them into your current branch.

bash
git fetch

Use this command when you want to review remote changes before applying them.


git pull

Downloads the latest changes and merges them into your current branch.

bash
git pull

In simple terms, git pull is equivalent to running:

text
git fetch
git merge

Merging a Branch

Once you've completed your work, switch to the destination branch and merge the feature branch.

bash
git merge branch-name

Example:

bash
git switch main
git merge feature/login

Git combines the commits from feature/login into main.

mermaid
gitGraph
    commit
    branch feature/login
    checkout feature/login
    commit id: "Login UI"
    commit id: "Google OAuth"
    checkout main
    merge feature/login

Cloning an Existing Repository

If you're joining a team project, you don't need to initialize Git again with git init.

Instead, clone the repository.

bash
git clone repository-url

Cloning creates a complete copy of the repository on your computer, including its commit history, branches, and configuration.

[!TIP] Use git init only when creating a brand-new project. Use git clone when working with an existing repository.

View Existing Remote References

To see all the remote repositories your local project is connected to, run:

bash
git remote -v

Example output: origin https://github.com/username/project.git (fetch) origin https://github.com/username/project.git (push)

Understanding origin

origin is just an alias (or nickname) for the full repository URL. Git stores the actual repository URL internally, but instead of making you type the full URL every time, it lets you refer to it using an alias like origin. origin → https://github.com/username/project.git

Push to Multiple GitHub Repositories

If you want to maintain access to two different repositories, add another remote instead of replacing origin. git remote add backup https://github.com/username/another-repo.git Now your project has two remotes:

text
origin  → https://github.com/team/project.git
backup  → https://github.com/username/another-repo.git

You can push to either one: git push origin main or git push backup main

Remove a Remote Reference

To remove a remote reference:

git remote remove origin

This does not delete your GitHub repository or your local project. It simply removes the connection between your local Git repository and the remote repository.

Add a Remote Reference

To add a new remote:

bash
git remote add origin https://github.com/username/project.git

Change a Remote URL

If you don't want the hassle of removing and re-adding a remote, you can simply update its URL: git remote set-url origin https://github.com/username/new-project.git This keeps the remote name (origin) but points it to a different repository.

Thank you for reading through

Comments (0)

No comments yet. Be the first to share your thoughts.