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.
Step 1: Create a new branch
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.
Create a new branch from your current branch
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:
gt checkout main # Make sure you're on the main branchgt 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.
Create a new branch and keep your staged changes
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:
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.
Step 2: Add changes to the new branch
Once you are on your new branch, you can start adding new changes or staging existing changes.
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.
Step 3: Commit the changes
After adding your changes, the next step is to commit them. This records your changes in Git's history.
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:
echo "additional changes" >> file.jsgt modify --all
The gt modify --all
command will stage and commit all modifications.
Step 4: Push the new branch to a remote repository
After committing your changes locally, you might want to share your branch with others or back it up on a remote repository.
Using Git:
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:
gt submit --stack
This command pushes all changes in the stack to the remote repository and creates/updates pull requests as necessary.
Summary
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.