DEV Community

Kervie Sazon
Kervie Sazon

Posted on

Git & GitHub Notes (Continuation) - Branching

Today I continued learning Git and GitHub, focusing on branching and collaboration commands.

Branch

A branch in Git is a separate line of development. It allows you to work on new features, fixes, or experiments without affecting the main project.

Most repositories have a default branch called:
main

Developers usually create new branches to:

  • Add new features
  • Fix bugs
  • Test ideas safely

Commands encountered:

git branch

The git branch command is used to view or manage branches.
View all branches

git branch
Enter fullscreen mode Exit fullscreen mode

Example Output:

* main 
  feature-testing
Enter fullscreen mode Exit fullscreen mode

The * indicates the current branch you are working on.

Create new branch

git branch feature-testing
Enter fullscreen mode Exit fullscreen mode

This creates the branch but does not switch to it yet.

git checkout

git checkout is used to switch between branches.

git checkout -b <branch_name>

This command creates a new branch and switches to it at the same time.

Command

git checkout -b feature-testing
Enter fullscreen mode Exit fullscreen mode

This is equivalent to running:

git branch feature-testing
git checkout feature-testing
Enter fullscreen mode Exit fullscreen mode

git diff

git diff shows the difference between file changes.

This helps developers review what has changed before committing or merging.

git commit -am

This command is a shortcut that:

  1. Stages tracked files
  2. Creates a commit

Command

git commit -am "Update test feature"
Enter fullscreen mode Exit fullscreen mode

Important notes:

  • Works only for already tracked files
  • New files still require git add

Example:

git add newfile.go
git commit -m "Add new file"
Enter fullscreen mode Exit fullscreen mode

git merge

git merge combines changes from one branch into another.

Example workflow

Switch to the branch you want to merge into:

git checkout main
Enter fullscreen mode Exit fullscreen mode

Merge another branch:

git merge feature-testing
Enter fullscreen mode Exit fullscreen mode

This brings the changes from feature-testing into main.

git pull

git pull updates your local repository with the latest changes from GitHub.

Command

git pull
Enter fullscreen mode Exit fullscreen mode

It basically does two things:

  1. Fetch updates from the remote repository
  2. Merge them into your local branch

git branch -d

This command deletes a branch after it has been merged.

Command

git branch -d feature-login
Enter fullscreen mode Exit fullscreen mode

This helps keep the repository clean by removing branches that are no longer needed.

Example Branch Workflow

A common workflow when adding a new feature:

git checkout -b feature-navbar
git add .
git commit -m "Add navbar feature"
git push
Enter fullscreen mode Exit fullscreen mode

After finishing the feature:

git checkout main
git merge feature-navbar
git branch -d feature-navbar
Enter fullscreen mode Exit fullscreen mode

In summary, I learned how branching works in Git and how it helps developers work on features without affecting the main branch. I practiced creating and switching branches using commands like git branch and git checkout -b. I also learned how to review changes with git diff, combine branches using git merge, and update my local repository with git pull. These commands helped me understand a basic workflow for developing features and managing code changes more efficiently using Git.

Top comments (0)