Graphite Reviewer is now Diamond

How to commit to a new branch in Git

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

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


This guide will take you through creating a new branch, committing changes, and pushing them to a remote repository, essential for developing new features, fixing bugs, or experimenting in an isolated environment. We'll also show you how to do this with the Graphite CLI, which simplifies Git operations and helps you improve pull request management.

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.

Using the Graphite CLI:

Terminal
gt checkout main # Make sure you're on the main branch
gt create --all --message "Start a new feature"

The Graphite CLI simplifies branch creation and immediately stages any changes, if present, in a new commit on the new branch.

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 45,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.

Using the Graphite CLI: The changes are staged when you create or modify the stack, as shown in Step 1 and upcoming Step 3.

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.

Using the Graphite CLI: If you need to make additional changes and commit them:

Terminal
echo "additional changes" >> file.js
gt modify --all

The gt modify --all command will stage and commit all modifications.

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

Using Git:

Terminal
git push -u origin new-feature

This command pushes the new-feature branch to the origin remote, setting up the branch to track the remote branch, which simplifies future pushes.

Using Graphite CLI:

Terminal
gt submit --stack

This command pushes all changes in the stack to the remote repository and creates/updates pull requests as necessary.

Join 45,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

This workflow is essential for managing separate development threads efficiently. For further reading on committing in Git, see the official Git documentation. For more details on using the Graphite CLI, check out our quick start guide.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Built for the world's fastest engineering teams, now available for everyone