Git and Branches a Guide for New Programmers

Git and Branches: A Guide for New Programmers

If you're new to programming and want to learn how to work with Git and branches in your projects, you've come to the right place! Git is a powerful version control system that helps you track changes and collaborate with others effectively. Branches allow you to work on different features or experiments without interfering with the main codebase. This guide will provide you with a step-by-step approach to get started with Git and understand the basics of branching.

Getting Started with Git

Install Git

Download and install Git from the official website (git-scm.com). Follow the installation instructions for your operating system.

Set up Git

Open a terminal or command prompt and configure your username and email using the following commands:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Initialize a Git repository

Navigate to your project directory in the terminal and run the following command to initialize a new Git repository:

git init

Add files to the repository

Use the command

git add [filename]

to add files to the staging area. You can specify individual files or use

git add .

to add all files in the current directory.

Commit changes

Commit your changes to create a new version of your project with the command

git commit -m "Commit message"

Make sure to provide a descriptive message that explains the changes you made.

View the commit history


git log

to view the commit history, including the commit hash, author, date, and commit message.

Commit Signing with GPG

Git allows you to sign your commits using GPG (GNU Privacy Guard) for added security and authenticity. Here's a high-level overview of the process:

Install GPG

Download and install GPG from the official website (gnupg.org). Follow the installation instructions for your operating system.

Generate a GPG key

Open a terminal or command prompt and run the following command to generate a GPG key:

gpg --gen-key

Follow the prompts to set up your GPG key. Make sure to provide a strong passphrase for added security.

Configure Git with your GPG key

Run the following command to configure Git to use your GPG key:

git config --global user.signingkey [key-id]

Replace [key-id] with the ID of your GPG key. You can find the ID by running

gpg --list-secret-keys --keyid-format LONG

and locating the key associated with your name and email.

Sign your commits

When making a commit, add the -S or --gpg-sign flag to sign the commit with your GPG key:

git commit -S -m "Commit message"

Git will prompt you to enter your GPG passphrase to sign the commit.

Remember to share your GPG public key with others to allow them to verify the authenticity of your signed commits.

Working with Branches

Create a new branch

To create a new branch, use the command

git branch [branch-name]

This creates a new branch based on the current commit.

Switch to a branch

Use the command

git checkout [branch-name]

to switch to an existing branch. This allows you to work on a specific branch and make changes independently of other branches.

Merge branches

Once you've made changes on a branch and want to integrate them into another branch, follow these steps:

a. Push your branch to a remote repository (e.g., GitHub, GitLab, Bitbucket) using the command

git push origin [branch-name]

b. Open the remote repository in your browser and locate your branch.

c. Create a pull request (PR) or merge request (MR) depending on the platform. Provide a descriptive title and description of the changes.

d. Reviewers can then review the changes, leave comments, and approve the pull request.

e. Once the pull request is approved, the changes can be merged into the target branch.

Resolve merge conflicts

If Git encounters conflicts during a merge, it will notify you. Conflicts occur when different branches modify the same part of a file. Open the conflicting files, resolve the conflicts manually, save the changes, and then commit the merged changes.

Delete a branch

Once you no longer need a branch, you can delete it using

git branch -d [branch-name]

Make sure you're on a different branch before deleting the branch you want to remove.

Tips & tricks

Here are some commonly searched commands and techniques that can be helpful when working with Git and branches:

Reverting uncommitted changes locally

If you have made changes to your code but haven't committed them yet and want to discard those changes, you can use the following command:

git checkout -- .

This will revert all the uncommitted changes in your working directory.

Resetting a branch to a specific commit

If you want to reset a branch to a specific commit, discarding any commits and changes after that commit, you can use the following command:

git reset --hard [commit-hash]

Replace [commit-hash] with the hash of the commit you want to reset to.

Amending the last commit

If you have made a mistake in your last commit message or want to add additional changes to it, you can use the following command:

git commit --amend

This will open your default text editor where you can modify the commit message or add changes.

Checking the status of your repository

To see the current status of your repository, including modified files and untracked files, use the command:

git status

This will provide you with an overview of the state of your repository.

Remember, practice makes perfect! The more you work with Git and branches, the more comfortable you will become with these commands and techniques. Don't be afraid to explore and experiment with different scenarios in a safe environment.


Congratulations! You now have a basic understanding of Git and branches. Remember to commit frequently, create branches for new features or experiments, and merge changes when they are ready. In most projects, merging branches involves creating a pull request or merge request on popular platforms like GitHub, GitLab, or Bitbucket. Pull requests facilitate code review and collaboration with team members. Git provides a powerful way to manage your code, collaborate with others, and protect the integrity of your commits.