Git Tutorial – A Comprehensive Beginner’s Guide

Git Tutorial – A Comprehensive Beginner’s Guide

Git is one of the most widely used DevOps tools for handling collaborative projects of various scales. This version control system enables you to stay on top of your project history. It also allows team members to work on different parts of the same project efficiently and effectively.

However, if you’re new to Git, handling various Git commands can be challenging.

This tutorial will cover Git basics, common commands, and workflow on local machines and remote repositories. We will also discuss the benefits and drawbacks of Git.

Download Complete Git Cheat Sheet

What Is Git and How Does It Work?

Global information tracker (Git) is an open-source version control system (VCS) for tracking changes in any set of files. It was initially designed and developed for Linux kernel development, but now is widely used for coordinating work on collaborative projects such as software development.

Git enables users to have non-linear development. A Git project’s database is stored in a remote repository hosted on a platform like GitHub or GitLab, allowing multiple people to work on different parts of the same project simultaneously. This results in improved data integrity and development speed.

There are two types of version control systems – centralized and distributed.

Centralized version control system users have to connect to the main repository to make any modifications since it stores all the project’s files. If there’s a problem with the main repository, users won’t be able to make any changes to the branches.

Meanwhile, Git is a distributed version control system, so it doesn’t rely on its main repository as all clients fully mirror its content. The users can clone the main repository to their local machine and modify the files offline, allowing a more flexible workflow.

Here are some of the most-used Git terms you will have to know to understand the basics of Git workflow, in alphabetic order:

  • Branch – Git has the main branch as its default branch. Creating a new Git branch means creating a new independent line of development from the main branch, such as developing a new feature. Find out how to rename a Git branch.
  • Check out – select which line of development you want to apply your changes. Checking out a branch will make Git record new commits on that branch.
  • Clone – create a local copy of an existing repository.
  • Commit – save the changes you’ve made. The commit command also refers to the snapshot of your repo at the time of the commit.
  • Git repository – also known as repo, is a .git folder inside a project, where all the commits are located. It tracks and saves all changes made to the Git project files. Note that the files in the repository are the ones that Git recognizes.
  • HEAD – a pointer that refers to the last commit command in the currently checked out branch. Think of it as the current branch.
  • Local repository – a copy of a repository stored in your computer.
  • Pull – copy the changes from a remote repository to a local machine.
  • Push – copy the changes from a local machine to a remote repository.
  • Staging area – also called index, this is where users organize files for the next commit. It informs Git what will change between the current commit and the next one.
  • URL – the location of the Git repository.
  • Working directory – also called a working area or a working tree, it refers to testing out your modifications before committing them to the staging area. It’s located in your local directory, and is the untracked area of Git.
The three stages of the basic workflow in Git, consisting of working directory, staging area, and finally Git repository

There are three stages in the basic workflow of Git:

  1. Start by creating a feature branch from the main branch in the working directory and developing your features there. Move back to this stage whenever you make any changes to your files, such as adding, deleting, or renaming them.
  2. Once you’re done, add the files to the staging area by merging the feature branch with the staging branch. Doing so helps review and organize the files you want to commit, making the project version easy to understand.
  3. Once the development has reached a milestone, commit the local changes to the main Git repository permanently by merging the staging branch with the main branch. It stores all the snapshots of the project versions.

Note that Git doesn’t track every modified file as it only includes the file in your commit if it is present in the staging area.

If you need to commit two different modified files, add the first file to the staging area, then perform the commit command. Once you’ve done with the first commit, repeat these steps to do the second commit.

Why Use Git?

Let’s go over the benefits and drawbacks of using Git for managing your projects.

Pros of Using Git

  • Open-source – it’s free to use. You can download its source code and modify it to suit your needs. Furthermore, many resources are available online to learn the best Git practices.
  • Fast and lightweight – the core part of Git and most of its operations are written and performed locally, making it faster than the centralized version control system.
  • Moderate hardware power – Git doesn’t require powerful hardware, as team members only need to interact with the server when they push or pull changes to the main repo. It’s also beneficial for growing teams, since Git doesn’t prompt further hardware requirements no matter how many people use it.
  • Secure environment – it uses SHA1, a cryptographic hash function, to identify objects within its database. It checks every file and commit during the checkout operation, so it’s not possible to alter data in the database without Git storing the changes.
  • Safer backup – Git creates multiple copies of your data as it mirrors the repository to all clients, allowing more backups. Furthermore, Git can take snapshots, which are the representations of a file system, enabling you to roll back to the state when the snapshot was taken. This can be a helpful recovery solution in case of failure.
  • Easier branching – creating, deleting, and merging branches takes only a few seconds, making it more time-efficient and less complicated than a centralized version control system.

Cons of Using Git

  • Steep learning curve – operating Git includes deploying various command lines. It might take a while to learn Git terms and how it works.
  • Poor binary file handling – Git slows down when handling binary files like images.
  • Possibility to work in the wrong area – this can happen due to several reasons, such as forgetting to perform the checkout command beforehand or leaving your repo in a branch instead of the main branch.
  • No built-in access control – users typically use SSH keys to restrict access to the Git server. However, widely-known platforms like GitHub and GitLab have their own permission mechanism.

How to Install Git on Linux, Mac, and Windows

To start using Git, begin by installing it on your local machine or checking whether you already have it installed on your computer.

Installing Git on Linux

If you’re using Linux, you need to perform two tasks – installing and configuring Git on your computer.

You also have to pay attention to a few prerequisites, such as logging in to Linux as root or as a user with sudo privileges.

Installing Git on Mac

There are several ways to install Git on Mac.

First, use the following command to check if your machine already has Git pre-installed:

git --version

If you don’t have Git installed on your Mac, the terminal will show this message:

The message the terminal will show if you don't have Git installed on your Mac

Click Install to download and install XCode. It’s an environment for developing software for Apple’s operating systems and it can handle Git as well. Simply follow the prompts, and you’ll have the working version of Git.

The second way to install Git on macOS is using the standalone installer for macOS. However, some users have reported that this installer has compatibility issues with macOS X Snow Leopard and Lion (OSX 10.6 and 10.7). If your Mac runs on those two versions, we recommend using XCode or follow the third installation method – Homebrew.

Homebrew homepage with install option

Homebrew is a software management system that lets you install command line utilities like Git. Enter this command to the terminal to install it:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once you have Homebrew installed, proceed with these steps:

  1. Launch the terminal and enter -brew install git.
  2. The installation should begin. Once done, check whether the installation is successful by running the git –version command.
  3. Use the git config command to configure your Git username and email, replacing myusername and example@mysite.com with your own. Any new commits you create will be associated with these details.
git config --global user.name "myusername"
git config --global user.email "example@mysite.com"

Installing Git on Windows

To install Git on Windows, you can use its standalone installer. Here’s how to do so:

  1. Download the latest version of Git for Windows installer.
  2. Start the installer. Once the Git Setup wizard screen opens, follow the prompts to complete the installation. We recommend going with the default options, as they’re sufficient to facilitate users’ needs in general.
Git 235.12 setup page with list of components to install
  1. Open the Command Prompt. Enter the git config command to configure your Git username and email by replacing myusername and example@mysite.com with your own. Now, any new commits you create will be associated with them.
git config --global user.name "myusername" $ git config --global user.email "example@mysite.com"

Pro Tip

Install Git credential manager to help you remember your Git credentials.

How to Use Git Commands

There are various ways to run commands in Git, from using command line tools like the terminal in macOS or the command prompt in Windows to graphical user interfaces (GUI).

This section will cover various Git commands you need to know to make the most of Git using the command line tool.

Using Git on a Local Repository

As discussed previously, the first stage of working in Git is developing your project in a working directory. This working tree is located in your local Git repository – the main repo’s copy that exists in your computer.

This section will discuss the most important commands for using Git locally. You can create a new Git repository and modify your repo with the following commands, even without an internet connection.

git init

The first step in creating a new repository is running the git init command. Doing so will create an empty Git repository. It sets up a new directory in a .git folder in your working tree, which contains the necessary Git metadata.

You can also use the git init command to convert an existing project into a Git repository. Note that you only need to run this command once at the beginning of a new project.

Navigate to your project folder, and enter the following command into your command line tool:

git init

You can also specify the directory you want to initialize a new repository. Use this syntax for doing so:

git init <folder>

For example, for a new repository in the demo-project folder, the syntax should look like this:

git init demo-project

This command will create a .git folder in the demo-project folder instead of your current working tree.

git add

The git add command adds new or modified files from the working directory to the staging area, also called index.

Git doesn’t have a simple save feature. Instead, it uses a process called committing. The first step in committing a change to a Git repository is running the git add command to select the files you want to commit.

This command will move those selected files to the staging area. From there, you can use another Git command – git commit – to record the local changes made to those files into a Git repo.

There are several ways to use git add, including:

  • git add . – add all unstaged files in a repository.
  • git add *.txt – add all .txt files in the repository.
  • git add <file name> – add a specific file.
  • git add <path> – add a specific directory of files.

Here’s an example of applying git add command:

git add index.html

This command line will add an index.html file to the staging area.

git status

The git status command shows the current state of the working directory and the staging area.

This command will check which local changes have been staged or committed and which ones haven’t. If there are no new local changes, it will show a message that indicates there’s nothing to commit and that the working tree is clean.

Git status message that indicates there's nothing to commit and the working tree is clean.

It’s also helpful for new Git users, as the message provides hints on what to do under specific circumstances.

Suppose there are some untracked files that haven’t been staged or committed in your local repository. In response, this command will show the list of the untracked files, and you can follow that up by moving them to the staging area.

Screenshot of the command that will show the list of the untracked files.

Furthermore, this command can also show if you have a merge conflict and which files are causing the problem.

git rm –cached

The git rm –cached command removes a file from the index, but it will remain intact in the working directory. This is useful if you plan to add it back to your Git repo in the future.

There are two ways to clear your cache using the git rm –cached command, depending on whether you’re going to remove specific files only or you want to clear an entire directory.

If you want to remove a specific file from the index, specify the file name after the command. In this example, we will remove a file named file.conf:

git rm --cached file.conf

If you want to remove an entire directory from the index, use the following command:

$ git rm -r --cached <directory name>

.gitignore

There might be files that you don’t want to include when committing a set of modifications, such as private configurations and hidden system files. Git doesn’t have an ignore command, but you can use a .gitignore file for this task.

A .gitignore file is a text file that specifies which files and folders Git should ignore in your working tree. It typically exists in a project’s root directory.

There are various types of files that should be ignored, and they’re usually machine-generated files or built artifacts. The most common examples include:

  • Hidden system files, like .DS_Store and Thumbs.db
  • Files generated during runtime, like .log and .temp
  • Dependency caches, like the contents of /node_modules and /packages
  • Personal IDE configuration files, like .idea/workspace.xml
  • Build output directories, like /bin, /out, and /target

To create a .gitignore file, create a plain text file using any text editor like Notepad in Windows and name it .gitignore. Add the target files as needed.

Here’s an example of the .gitignore file:

filename.txt
directory/
*.html
*~

The list above shows various patterns to list a file in .gitignore:

  • Literal file names – ignore a specific file name, such as filename.txt.
  • Directories – ignore an entire directory by adding a slash symbol (/) at the end of the directory name, for example, directory/.
  • Wildcard – ignore any file that ends with a specific extension by adding an asterisk (*) before the extension name, for example, *.html. Additionally, *~ will prompt Git to ignore any file ending with ~, such as index.html~.

You can also specify certain files to be excluded from the .gitignore list by using the ! prefix.

For example, you don’t want Git to ignore example.html, even though all other files with .html extension are ignored. In this case, add this to your .gitignore list:

*.html
!example.html

Remember that it is impossible to exclude a file inside an ignored directory.

For example, you cannot exclude directory/example.log from an ignored path directory/. If you added !directory/example.log in the .gitignore file, Git would still ignore directory/example.log because the entire directory is ignored.

git commit

The git commit command creates a snapshot of your repository’s content at a specific time.

It records the changes made to the files in your repository. On top of that, commits serve as the comprehensive project record, showing you how it has evolved.

Each commit has a unique commit ID for easier reference. A commit ID is also helpful for referring to a specific commit when you need to undo a change.

This command is the second step in saving the changes made to a repository. The process starts with the git add command for staging any new changes you want to include in a commit. Then, git commit creates a commit with those changes to a repository. The git add command won’t affect anything until you execute git commit.

Once the code in your local branch is ready, use git push to commit to the remote repository. This enables various team members to commit their code in their local repo and push it to the main repo later when the code is fully ready.

There are various options of the git commit usage, with the most common options as follows:

  • git commit – the primary command to commit the staged changes. It will launch a text editor that prompts you to submit a commit message that briefly describes the change you’ve made.
  • git commit -m “message” – creates a commit with an inline message. Use any message, but make sure it is concise and understandable for other team members.
  • git commit -a – creates a commit that includes all changes in a working directory. This command will only include modifications to tracked files, that have been added using the git add command.
  • git commit -am “message” – combines the function of the -a and -m options, which will create a commit of all staged changes and includes an inline comment with it.
  • git commit –amend – allows modification to the last commit, such as editing the last commit message without changing the snapshot.

For example, you have just changed the styling of a homepage and moved the css/styles.css file to the staging area. Now you can continue to perform git commit and put a commit message there to help anyone understand the change.

git commit -m "Change the styling on homepage"

Pro Tip

When writing a commit message, many developers use the present tense to make the comments seem like actions. This helps make any rewriting operations more intuitive.

git log

The git log command shows the commit history of a repository. By default, its output includes commit IDs, messages, and other metadata. It helps navigate your growing project history.

The difference between git log and git status is that git status applies to the current directory, while git log applies to the repository history.

Suppose you want to see a list of all or new commits that have been pushed to your team’s repository. Use the following command to do so:

git log

Git will return a list similar to the example below:

The output showing the latest log information in the repository.

This output shows the latest log information in the repository. Here’s the breakdown of this example:

  • commit ID – a random string of numbers and letters.
  • Author – in this case, it’s “Test”. It also shows the author’s email.
  • Date – specifies the exact date and time.
  • Commit message – in this example, it’s “hello file added” and “new file”.

It’s also possible to filter the output by adding various flags. There are multiple options to filter your logs. Check the examples below to see the applications of git log filters:

git log -n 5

The -n flag helps you see a number of your most recent logs. For example, here the git log command will return a list of the five most recent commits made to a repository:

git log --author="Jane Doe"

The author flag filters the commits by the person who wrote the changes. The example above will return a list of commits that were changed by Jane Doe.

In larger projects, sometimes the person who creates a commit is not the one who pushes it to the main repository. That’s why Git also provides an option to filter the log by a committer name, using the committer flag:

git log --committer="John Doe"

In this example, the command will show commits committed by John Doe.

git log --committer="John Doe"

The –before and –after flags filter results of git log by date. Both flags accept a wide range of date formats, but the most commonly used ones are full dates and relative values. The git log command example above will retrieve a list of commits after October 28, 2021.

git log --before="yesterday"

An example of a relative value is yesterday. The git log command above will retrieve a list of commits from the previous day.

git log --after="2021-10-28" --before="2022-02-23"

Combining both the –before and –after flags to filter commits from a specific timeframe is also possible. The example above will retrieve a list of commits published from October 28, 2021 to February 23, 2022.

git log -- index.html

The git log command above is useful when you only want to see a list of commits affecting a specific file. In this example, the command will prompt Git to show the changes made to the index.html file in your code.

git log -S"# Hello"

The -S flag enables you to search for all commit changes that involve a particular line of code. In the example above, the command line searches for commits that have added or removed the term # Hello to your code.

git log a12bcd3..a45b67c

To filter your log based on a particular range of commits, use the git log <since>..<until> command. The example above will return a list of commits made between pushing the commit “a12bcd3” and the commit “a45b67c”.

git log --grep="feat:"

The –grep flag allows you to filter your log based on the commit message. This command above will retrieve a list of all commits which comments start with feat:.

Working With Git Branches

One of the essential features in a Git version control system is branching. Git branches let you move away from the main codebase and make changes to it without affecting the original main code or other branches. In other words, it allows developers to work on independent development lines in parallel.

For example, in a website project, you can work on a new feature, while another developer is fixing a bug. Both of you will create a new branch and start working on those respective branches.

If you need to make an immediate change to the website, you can switch to the main branch, make the change, and push it live. After that, you can switch back to work on the new feature branch and merge it to the main branch once you’re done.

If another developer finishes his task at a different time, this will not affect someone else. All those changes will be safely kept in each branch and will only affect the main codebase when pushed to the main branch.

Check the illustration below to better understand how Git branches work:

How git branches work.

Branches also help reduce the possibility of unstable code being merged into the main branch since developers will only push their code into the main branch once they’re ready.

You can also ask developers to create a pull request first, which is a proposal for pushing a change to the main branch. Making a pull request lets other developers review that code first, ensuring it’s ready to be merged with the main branch.

Pro Tip

Name your default branch with a neutral name like main. For years, the default branch in a Git repository has been commonly named master. However, this naming convention might be soon left behind as the software industry is moving towards inclusivity.

git branch

The git branch command is used to create, rename, and delete branches. It can also retrieve a list of local and remote branches.

This command is commonly used together with git checkout and git merge to switch branches and merge different code versions.

Let’s start with creating a branch. In this example, we will create a new branch called v2.0. Use this syntax to do so:

git branch v2.0

Once you run this command, your development environment will remain attached to the HEAD of the repository. You’ll need to use the git checkout command if you’d like to see this new branch.

Note that the repository history remains the same when you create a new branch, as Git only creates a pointer to that new branch.

Next, if you want to rename your branch, you can use the -m flag to the branch that you’re currently viewing. For example, you want to rename the v2.0 branch to v.2.0.1. You have to open the v2.0 branch, and apply this code:

git branch -m v.2.0.1

To delete a branch in a repository, use the -d flag. Let’s say you want to delete the v.2.0.1 branch. Here’s the command to do just that:

git branch -d v.2.0.1

If you run the git branch -d command and there are changes on this branch that have not been merged into another branch, Git won’t delete that branch. This command help ensure you won’t lose any changes by accident.

If you intentionally decide to delete a branch permanently, even if it has changes that have not been merged anywhere, use the -D flag. Here’s an example to force-delete a branch named v.2.0.1:

git branch -d v.2.0.1

Command with -D flag won’t show any warnings when executed, so keep in mind to only use it when you’re sure to delete the branch.

Lastly, if you want to retrieve a list of branches in a Git repository, run this syntax:

git branch

Here’s the example of this command’s output:

* main
* v.2.0.1
* v.1

Alternatively, you can also use the git branch -list command, which will return the same list from your repo.

In addition, you can also create tags to give reference to specific points in your repository history.

A Git tag is usually used to mark a release version. For example, whenever developers are ready to launch new project versions, they create tags for easier identification.

Tags are like branches that don’t change – once they’re created, they have no further history of commits.

Use the following syntax to create a new Git tag:

git tag <tagname>

The common tag name pattern in version control is to use version numbers, such as v1.0.

git checkout

To start modifying a branch, you need to switch to it. Here’s where you’ll use the git checkout command to switch between different branches.

Once you check out a branch, your working directory will be changed. For example, you’re on branch v.1, and you want to move to branch v.2. To do so, apply this command:

git checkout v.2

Now your current directory is branch v.2.

If you’re not sure which branch you’re currently in, use the following command to find out:

git branch --show-current
The syntax which shows the current branch you're on

Alternatively, you can also use this syntax to see the current branch you’re on:

git rev-parse --abbrev-ref HEAD

The git checkout command can also create a new branch. Usually, you’ll need to run the git branch command first to create a new branch, then run the git checkout command for checking it out. However, the command shown below provides a more convenient method:

git checkout -b beta-v.3

This command simultaneously creates a new branch named beta-v.3 and checks it out, allowing you to switch to it immediately.

Note that the branch created with the git checkout -b command will be based on the current HEAD by default.

git merge

The git merge command combines an independent line of development with another, integrating them into a single branch.

For example, when a developer merges the changes made in a staging branch with the main branch, this command will put back together a forked repository history into a unified project history.

Note that when you’re merging a target branch to the main branch, only the main branch will be updated to reflect the merge, while the target branch will remain unchanged.

To execute this command, ensure you’re in the main branch. If you’re not there yet, move to your main branch by running the git checkout command. If the branch that you want to merge is demo-project, the merge command should look like this:

git merge demo-project

If the two branches you’re merging both changed the same section of the same file, Git won’t be able to determine which version to use, therefore triggering a merge conflict.

To solve this, run the git status command to figure out which files need to be troubleshot.

Managing Remote Git Repository

Now that you have learned how to navigate your local repository, it’s time to learn about managing a remote Git repository. Remote repositories allow you to collaborate with others on a Git project using code hosting platforms like GitHub, BitBucket, or GitLab.

A remote repository is typically hosted on a remote server. Therefore, unlike a local repository, you need internet connection to access a remote repository.

Once you push the local changes from your local repository to a remote one, your changes become visible to other team members who have access to it. They can pull your changes from the remote repository to their local repository, and integrate the changes into their workspaces. You can also do the same when others push their local changes to the remote repository.

See the following roadmap for Git workflow in relation to a remote repository:

The roadmap workflow when managing a remote Git repository.

The last section of this tutorial will explain the commands used when managing remote repositories.

git remote

The git remote command allows you to create, view, and delete connections to a remote repository.

Here are a few examples of the usage of the git remote command.

If you want to view your git remote configurations, run this syntax:

git remote

This command will return a list of remote connections you have to other repositories.

To add a new remote repository connection, use this command below:

git remote add <name> <url>

For example, if you want to add a remote repository named new-remote, its URL is https:/github.com/user/new-remote.git. The syntax will be like this:

git remote add new-remote https:/github.com/user/new-remote.git

Once you’ve added it, you’ll be able to use new-remote as a shortcut for https:/github.com/user/new-remote.git in other Git commands.

If you want to rename a remote connection, use this command:

git remote rename <old-name> <new-name>

Meanwhile, when you find that a remote repository is already obsolete, you can remove your connection to it by using this command:

git remote rm <name>

git push

The git push command transfers the commits in a local repository to a remote repository.

Before you start pushing commits, check whether the command line is pointing to the intended repository by using this command:

git remote -v 

If it’s not pointing to the correct address, change the address with this command:

git remote set-url <remote repo name> <remote repo address>

Once it points to the correct address, run this command:

git push <remote name> <branch name>

For example, if the remote repository name is origin and the branch name is main, the syntax should look like this:

git push origin main

This command will send local commits to the remote repository’s main branch.

git pull

The git pull command retrieves and downloads the content of a remote repository to your local machine. Here’s the syntax to do so:

git pull <remote repository name>

For example, to retrieve the content of the main branch, use this command:

git pull main

If you want to pull code from just one specific branch, use this syntax:

git checkout <branch name>
git pull <remote name>

Suppose you want to work with code on a branch named demo-v1, and the remote repository name is main. The command will be as follows:

git checkout demo-v1
git pull main

The git checkout command will move you to the branch to the code you want to pull. Then, the git pull command will retrieve the code.

Sometimes people get confused about the differences between the git pull and the git fetch commands. In short, both are used to retrieve the content of a remote repository, but the former only downloads the data, while the latter downloads and integrates the data to the working files.

From here, you can see that the git pull operation is actually the combination of git fetch and git merge commands.

git clone

The git clone command creates a local copy of a remote repository. Developers use this command to obtain a copy of an existing repository to develop it in their local machine without affecting the main codebase.

It’s also one of the two ways in Git to set up a repository – by using the git init command to create a new Git repository or using the git clone command to clone an existing one.

Same as the git init command, the git clone command is typically only executed once. With cloning, you’ll have all the metadata, Git branches, and the complete repository history.

Use this command to clone a remote repository:

git clone <remote URL>

Suppose you want to clone a GitHub repository and start editing it right away. Run these commands to do so:

git clone https:/github.com/user/new-remote.git
cd new-remote

In this example, Git will create a new directory named new-remote. Then, the cd command will move you to that new directory, where you can start viewing and editing the files.

Besides using Git’s native protocol, you can also perform cloning via HTTPS or SSH protocol.

The steps slightly differ across Git platforms, but the command is similar.

Here are the steps to clone a repo via GitHub:

  1. Open the main page of the repository that you want to clone.
  2. Click the Code button above the list of files.
  3. Copy the code under the Clone with HTTPS section to clone the repository using HTTPS. To clone the repository using the SSH key, click Use SSH and copy the code, including the certificate issued by your organization’s SSH certificate authority.
  4. Open Git Bash.
  5. Move to the directory where you want the cloned copy to be.
  6. Use this command to clone over HTTPS. The URL is the one that you’ve copied previously:
  7. Open the main page of the repository that you want to clone.
  8. Click the Code button above the list of files.
  9. Copy the code under the Clone with HTTPS section to clone the repository using HTTPS. To clone the repository using the SSH key, click Use SSH and copy the code, including the certificate issued by your organization’s SSH certificate authority.
  10. Open Git Bash.
  11. Move to the directory where you want the cloned copy to be.
  12. Use this command to clone over HTTPS. The URL is the one that you’ve copied previously:
$ git clone https://github.com/yourusername/yourrepository

Or this command to clone over SSH:

$ git clone ssh://github.com/yourusername/yourrepository
  1. Hit Enter. That is it, you’ve successfully created a local clone!

If you use Bitbucket, here are the steps to do so:

  1. Open the repository you want to clone.
  2. Click + in the global sidebar and select Clone this repository.
  3. Copy the clone command, either the HTTPS or the SSH format. Keep in mind that if you’re using SSH, ensure you have your public key in Bitbucket, and it’s loaded on the local system where you want to store your clone.
  4. Using the terminal, move to the local directory where you want to clone the repository.
  5. Paste the command you’ve copied in the third step.

If you’re using the HTTPS, the command will be:

$ git clone https://username@bitbucket.org/yourusername/yourrepository

For SSH, use this command:

$ git clone ssh://git@bitbucket.org:yourusername/yourrepository

A successful clone command will appear in a new sub-directory on your local drive, inside your target directory. It will have the same name as the repository that you cloned.

Conclusion

Git helps you manage your projects and easily monitor project history. It enables you to create a new project, copy an existing repository, create a new feature, and work on it together with your team. On top of that, it provides a lightweight, secure working environment for your development project.

However, it can be daunting for beginners to learn Git, as its operation involves a lot of commands. It also takes some time to understand Git’s multiple development areas – from the local repository to the remote one – but once you get a better grasp of it, you’ll be able to navigate your development project way more effectively. We hope this tutorial provides a helpful way of learning Git basics. If you want to learn more about using Git to improve your productivity, try automating your workflow using git hooks. Good luck.

Was this tutorial helpful?

Author
The author

Nadia Maya Ardiani

Maya is a Digital Content Writer at Hostinger. As a website development and digital marketing enthusiast, she's keen to share her knowledge so people can thrive online. When she's not writing, you can find her watching sci-fi movies while eating ramen.