How to commit to a new branch in Git

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


This guide will walk you through the process of creating a new branch, committing changes to it, and pushing it to a remote repository. This workflow is essential for developing new features, fixing bugs, or even trying out ideas in a safe, isolated environment. We'll cover several scenarios, including creating a branch with local or staged changes, and pushing branches to remote repositories.

Before making any changes, it's often a good practice to create a new branch. This keeps your changes organized and separate from the main line of development, usually referred to as the main branch.

Terminal
git checkout -b new-feature

This command does two things: it creates a new branch called new-feature and switches to it immediately. git checkout is the command used for switching branches, and -b tells Git to create a new branch if it doesn't exist.

If you have changes already staged (using git add), and you want to move these to a new branch, you don't need to do anything extra. Just use the same command:

Terminal
git checkout -b new-feature

This will switch to the new branch new-feature with your staged changes still intact and ready to be committed on the new branch.

Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

Once you are on your new branch, you can start adding new changes or staging existing changes.

Terminal
git add <file-or-directory>

Replace <file-or-directory> with the name of the file or directory you want to add. You can use . to add all changes in the current directory.

The git add command is used in Git to stage changes made in the local working directory for the next commit, essentially marking specific changes to be included in the next snapshot of the project. The local working directory is where you actively modify files, creating a live and editable version of your project content.

In contrast, the staging area, also known as the index, is a prep zone for changes before they are officially committed to the repository's history.

By using git add, you tell Git to include updates to specific files or directories in the staging area, thus preparing them to be permanently stored in the repository upon performing the next commit. This step is crucial because it allows developers to curate and review changes before finalizing them in a commit, enabling a controlled and intentional revision history.

After adding your changes, the next step is to commit them. This records your changes in Git's history.

Terminal
git commit -m "Add a descriptive message here"

The -m flag allows you to add a commit message directly in the command line.

After committing your changes locally, you might want to share your branch with others or back it up on a remote repository.

Before pushing, check if the remote repository is set up:

Terminal
git remote -v

This command lists all the remotes connected to your local repository. If you don't see any, you might need to add one using:

Terminal
git remote add origin <repository-url>

Replace <repository-url> with the URL of your remote repository.

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2
Terminal
git push -u origin new-feature

This command pushes the new-feature branch to the origin remote. The -u flag sets up the branch to track the remote branch, which makes future pushes (and pulls) easier by just using git push or git pull without specifying the branch.

This workflow is crucial for managing separate development threads within the same project efficiently. Here's a quick recap of the steps:

  • Create a new branch with git checkout -b new-feature
  • Add changes to your new branch with git add
  • Commit your changes with git commit -m "Your message"
  • Push your branch to a remote with git push -u origin new-feature

For further reading on committing in Git, see the official Git documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2