Getting Started with Git

June 6, 2023 Avishka Devinda

Are you new to Git and feeling overwhelmed by all the different commands? Don't worry, you're not alone! Git is a powerful version control system that can be a bit daunting to navigate at first. In this beginner's guide, we'll walk you through some of the most common Git commands and how to use them.

Git Init

The first step in using Git is to initialize a new repository. To do this, navigate to the directory where you want to store your project and run the following command:

git init

This will create a new Git repository in the current directory. You should only need to do this once per project.

Git Add

Once you've created a new Git repository, you'll need to start tracking your files. To do this, use the git add command. For example, if you want to track all the files in your project directory, run the following command:

git add .

If you only want to track a specific file, replace the . with the file name.

Git Branch

Branches are an important part of Git that allow you to work on different versions of your code at the same time. To check which branch you are currently in, use the following command:

git branch

This will list all the branches in your repository and highlight the branch you are currently in. The current branch will have an asterisk next to it.

To create a new branch, use the following command:

git branch <branch name>

This will create a new branch with the specified name.

Git Checkout

You can then switch to the new branch using the git checkout command:

git checkout <branch name>

This will switch you to the specified branch and update your working directory to reflect the files in that branch.

You can also create and switch to a new branch in one command using the -b option:

git checkout -b <branch name>

This will create a new branch with the specified name and switch you to that branch in one step.

Git Push

Once you've made changes to your code, you'll want to push them to a remote repository (such as GitHub). To do this, use the following command:

git push <remote name> <branch name>

For example, if you want to push your changes to a remote repository named origin on the master branch, run the following command:

git push origin master

Git Pull

If someone else has made changes to the code in the remote repository, you'll need to pull those changes down to your local repository. To do this, use the following command:

git pull <remote name> <branch name>

For example, if you want to pull changes from the origin repository on the master branch, run the following command:

git pull origin master

Git Merge

If you've made changes on a different branch and want to merge those changes back into the main branch, use the following command:

git merge <branch name>

For example, if you want to merge changes from a branch named feature-branch into the main branch, run the following command:

git merge feature-branch

Git Commit Delete

If you've made a mistake in a commit message, you can delete the last commit using the following command:

git commit --amend

This will open your default text editor, allowing you to edit the commit message. Once you've made your changes, save and exit the text editor.

Git Branch Delete

If you no longer need a branch, you can delete it using the following command:

git branch -D <branch name>

For example, if you want to delete a branch named feature-branch, run the following command:

git branch -d feature-branch