Git 101: A Beginner’s Guide to Understanding and Using Git

download 1 Journey as a Software Developer

Introduction

Git is a distributed version control system that helps you track changes to your code over time. It’s used by software developers, web developers, and other professionals who work with code. Git makes it easy to collaborate with others on a project and keep track of changes to your codebase.

With Git, you can:

  • Keep track of changes to your code over time.
  • Create branches to work on different parts of your codebase independently.
  • Merge changes from different branches.
  • Collaborate with others on a project.

Installing Git

To use Git, you need to install it on your computer. Here are the steps to install Git on Windows, Mac, and Linux.

Windows

  1. Go to the official Git website at https://git-scm.com/.
  2. Click on the “Download” button to download the latest version of Git for Windows.
  3. Run the installer and follow the prompts to complete the installation.

Mac

Using Homebrew

  1. Open Terminal on your Mac.
  2. Install Homebrew by running the following command: /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
  3. Install Git by running the following command: brew install git

Using MacPorts

  1. Open Terminal on your Mac.
  2. Install MacPorts by downloading the package installer from https://www.macports.org/install.php.
  3. Install Git by running the following command: sudo port install git

Linux

Ubuntu or Debian

  1. Open Terminal on your Linux machine.
  2. Run the following command to update your package list: sudo apt-get update
  3. Install Git by running the following command: sudo apt-get install git

Fedora or CentOS

  1. Open Terminal on your Linux machine.
  2. Run the following command to update your package list: sudo dnf update
  3. Install Git by running the following command: sudo dnf install git

Basic Git Concepts

Git uses repositories to store your codebase. A repository is a directory where you store your code, along with all the history of changes to that code. A commit is a snapshot of your code at a specific point in time. When you make changes to your code, you create a new commit.

You can create branches to work on different parts of your codebase independently. For example, you might create a branch to work on a new feature or fix a bug. When you’re ready to merge your changes back into the main branch, you can use the git merge command to combine the changes from different branches.

Git Commands

Git has many commands, but you only need to know a few to get started. Here are some common Git commands and their uses:

git add

Add changes to the staging area. The staging area is where you prepare changes to be committed. Use this command to tell Git which changes you want to commit.

git add <filename>

git commit

Create a new commit with the changes in the staging area. When you create a commit, you’re creating a snapshot of your code at a specific point in time. Use this command to save your changes to your local repository.

git commit -m "commit message"

git push

Upload your local commits to a remote repository. When you push your changes, you’re sharing them with others who have access to the remote repository.

git push origin <branch>

git pull

Download changes from a remote repository to your local repository. When you pull changes, you’re updating your local repository with changes made by others.

git pull origin <branch>

git branch

Create, list, or delete branches. Use this command to manage your branches.

git branch <branch_name>  # create a new branch
git branch  # list all branches
git branch -d <branch_name>  # delete a branch

git merge

Combine changes from different branches. Use this command to merge changes from one branch into another.

git merge <branch_name>

Working with Git

Now that you understand the basic concepts and commands of Git, here are some tips for working with Git:

1. Create a new branch for each feature or bug fix

Create a new branch for each feature or bug fix to keep your changes organized and to make it easier to merge changes later.

git branch <branch_name>
git checkout <branch_name>

2. Commit often

Committing often helps you track changes and roll back if something goes wrong.

git add <filename>
git commit -m "commit message"

3. Pull changes frequently

Pulling changes frequently helps you keep your local repository up-to-date with changes made by others.

git pull origin <branch_name>

4. Push changes regularly

Pushing changes regularly helps you share your changes with others and keep everyone on the same page.

git push origin <branch_name>

5. Use descriptive commit messages

Use descriptive commit messages to help others understand the changes you made.

git commit -m "Add login functionality"

Conclusion

Git is a powerful tool for managing your codebase and collaborating with others. With Git, you can keep track of changes to your code over time, create branches to work on different parts of your codebase independently, merge changes from different branches, and collaborate with others on a project. By following best practices and using descriptive commit messages, you can make working with Git even more productive and efficient.