There are three basic steps to create a new branch and track your changes in a remote Git repository:

  1. Create a new branch:
    git checkout -b feature_branch_name
  2. Edit, add and commit your files
  3. Push your branch to the remote repository:
    git push -u origin feature_branch_name

It’s as simple as that!

Not sure what's going on? Here are those steps in more detail

Step 1

The git checkout -b feature_branch_name command is really doing two things.

First, the checkout command creates a new branch called feature_branch_name with the -b option, then it switches you to that new branch.

Without this shortcut, here's how you would normally create a new branch and switch to it:

git branch feature_branch_name
git checkout feature_branch_name

Check out Git branch and Git checkout for a deeper dive into both commands.

Step 2

Here's where you would make all of your changes and use the add and commit commands extensively.

When you add, delete, or modify local files, those changes aren't tracked by Git until you use the add command. By adding those files, you're telling Git that you want to track the history of the changes you made.

For example, say you have a file called test.txt with the following text:

Hello, world!

Then, you modify the file to read:

Hello, world, and all you beautiful people in it!

If you run git status, you'll see the following:

On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	text.txt

nothing added to commit but untracked files present (use "git add" to track)

To actually track the new changes you've made to the file, you need to run git add test.txt. If you run git status again, you'll see the following:

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   text.txt

Now, the changes you made in the text.txt file you made are in what's called the "staging area".

The staging area is sort of a middle step – while your changes are saved to your local machine, they're not tracked by Git yet. It allows you to choose which files to include in your Git commit, which is where Git commits to actually tracking the changes you've made.

The Git commit command basically takes a snapshot of all the changes you've made to the files in the staging area. Git uses these commits or snapshots to create a detailed timeline of changes you've made to your project.

If, for example, you introduce a bug later in the project, your Git commits make it easy to go back to a specific point in the timeline.

To save the changes to the files in the staging area, just run git commit. This will pull up a local text editor for you to write a commit message – a short message explaining what you changes and why.

You could also use the commonly used -m option to write a commit message directly from the command line:

git commit -m "My commit message"

Take a look at these articles to learn more about both commands:

Step 3

Finally, use the Git push command to push your committed changes to a remote repository.

A remote repository is just a fancy way of saying an online version of your project, and is usually on a site like GitHub or GitLab.

Since you created a new branch in step 1, if you run git push, there's a good chance you'll see the following error:

fatal: The current branch test has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature_branch_name

All this means is that you need to set the "upstream branch", or tell Git what your new branch should be called in your remote repository.

To fix this, just run the command in the error message:

git push --set-upstream origin feature_branch_name

You could also use the -u option instead:

git push -u origin feature_branch_name

Then the next time you run the git push command, the feature_branch_name will already exist in your remote repository, so you won't see the error message again.

Check out this article for a deep dive into the Git push command.

In summary

Creating a new branch and tracking your changes in a remote Git repository is easy. Here are the three main steps again:

  1. Create a new branch:
    git checkout -b feature_branch_name
  2. Edit, add and commit your files.
  3. Push your branch to the remote repository:
    git push -u origin feature_branch_name