GIT Basics: A beginners guide!
Tags: git, tutorial, github
Published: 2023-01-13 12:49:47

**Git** is a popular version control system that allows developers to track and manage changes to their code. It is widely used in software development and is considered to be one of the most popular version control systems in use today.

One of the main benefits of using Git is that it allows developers to **work on the same codebase simultaneously without interfering with each other's work**. This is achieved by using a branching system, which allows developers to create separate branches of the codebase to work on. Once a developer has completed their work, they can then merge their changes back into the main branch.

Another benefit of using Git is that it allows for easy collaboration among developers. By using a centralized repository, such as [GitHub](https://github.com/), developers can easily share their code with others and collaborate on projects together.

In this tutorial, we will go through the basics of Git and how to use it effectively.


### **1. Installing Git**
The first step in using Git is to install it on your computer. You can download the latest version of Git from the official website. Once you have downloaded the installer, run it and follow the instructions to install Git on your computer. More detailed instruction can be found here https://github.com/git-guides/install-git

### **2. Creating a new repository**
Once you have installed Git, you can create a new repository on your computer by running the command 
```bash
git init
```
in the command prompt. This command will create a new repository in the current directory.

### **3. Adding files to the repository**
To add files to your repository, you can use the command 
```bash
git add
``` 
followed by the file name. For example, to add a file called "index.html", you would run the command 
```bash
git add index.html
``` 
You can also use the command 
```bash
git add .
```  
to add all the files in the current directory to the repository.

### **4. Committing changes**
Once you have added your files, you can then commit your changes using the command 
```bash
git commit
```
and providing a commit message. The commit message should be a brief description of the changes you have made. For example, 
```bash
git commit -m 'Fixed issue that causing layout flickering'
```

### **5. Pushing changes to a remote repository**
To push your changes to a remote repository, such as GitHub, you can use the command 

```bash
git push
```
This will upload your changes to the remote repository, allowing others to access them. For example, 
```bash
git push origin master
```
will push your changes to the `master` branch of a remote repository named `origin`.

### **6. Downloading changes from a remote repository**
To download changes from a remote repository, you can use the command 
```bash
git pull
```
This will download any changes that have been made to the remote repository and merge them with your local repository. For example, 
```bash
git pull origin master
```
will pull any changes from the `master` branch of a remote repository named `origin` and merge them with your local repository.

### **7. Branching**
Git allows developers to work on separate branches of the codebase simultaneously. To create a new branch, use the command 
```bash
git branch branch_name
```
and to switch to that branch use the command 
```bash
git checkout branch_name
```
Once you have made changes to the branch and you are ready to merge it with the main branch, you can use the command 
```bash
git merge branch_name
```

These are the basic commands that you need to know to start using Git effectively. By using Git, you will be able to track and manage changes to your code, collaborate with others, and easily share your code.

As an example, let's say you are working on a project called `MyProject` and you have created a new branch called `feature1` to work on a new feature. To create the branch and switch to it, you would run the commands:
```bash
git branch feature1
git checkout feature1
```

You make some changes to the code, add the files and commit the changes with the commit message "added feature1"
```bash
git add .
git commit -m 'added feature1'
```

Once you are ready to merge the changes to the main branch, you switch to the main branch and merge feature1 branch
```bash
git checkout master
git merge feature1
```

### **8. Cloning a repository**
Another way to access a remote repository is by cloning it. Cloning a repository will create a copy of the remote repository on your local machine, allowing you to work on the code locally. You can use the command `git clone` followed by the repository URL to clone a repository. For example, 
```bash
git clone https://github.com/user/MyProject.git
```
will clone the `MyProject` repository from the GitHub user's account.

### **9. Checking the status of the repository**
You can use the command `git status` to check the status of your repository. This command will show you which files have been modified, added, or deleted and which branch you are currently working on.

### **10. Viewing the commit history**
You can use the command `git log` to view the commit history of your repository. This command will show you a list of all the commits that have been made, along with the commit message, author, and date.

### **11. Reverting changes**
If you need to revert changes that you have made, you can use the command `git revert`. This command will undo the changes made in a specific commit and create a new commit with the changes. You can also use the command `git reset` to remove commits from the repository.

---

Git is a powerful version control system that can help you to track and manage changes to your code. By using Git, you can work on the same codebase simultaneously, collaborate with others, and easily share your code. In this tutorial, we have covered the basics of Git and how to use it effectively. Remember to keep your commits small and focused, and always use meaningful commit messages.

---

### Key takeouts

1. Git is a tool that helps you keep track of the changes you make to your code. It allows you to save different versions of your code and easily switch between them.
2. Git also allows you to work on the same code with other people. You can make changes to your own copy of the code, and then merge those changes with the version that everyone else is working on.
3. To start using Git, you will need to download and install it on your computer, then you can create a new "repository" to store your code. To save changes to your code, you will use the basic Git commands like "git add" and "git commit".