The basics of GitHub and Git Commands for every beginner.
Tags: github, git, opensource, beginners
Published: 2020-12-09 19:47:34

## But Before that, What exactly is Git and GitHub? 
Common Definition — **Git** is a _version control system_, that keeps track of all the minute changes in your project and allows you to move back to the previous version irrespective of the number of times you change it. 
**GitHub**, on the other hand, is the most popular service that hosts all your git repositories. And there are plenty of others like Github.

You can download git for your system depending on your OS from [here](https://git-scm.com/download). Also, consider making a GitHub account from [here](https://github.com).

Now that you're done with Git and GitHub, let's start with the basic commands you're going to need to keep track of your projects.

# 1. `git init`
`git init` is the beginning of any Git Repository. This command initializes an empty git repository in the current working directory. Please note that this repository will be created in the current directory you are working in. Make sure you are in your project folder before initializing a Git Repo.
```
$ git init
Initialized empty Git repository in E:/workingfolder/.git/
```
You can now work in this folder and do the necessary changes in your projects, and keep track of all the changes.

# 2. `git clone`
Cloning in git is exactly what it is named. It CLONES or creates a copy of a repository which might be hosted on GitHub or any other git hosting platform. 
```
$ git clone <url_of_the_original_github_repository.git>
Cloning into 'project name'...
remote: Enumerating objects: 36, done.
remote: Counting objects: 100% (36/36), done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 36 (delta 14), reused 33 (delta 11), pack-reused 0
Receiving objects: 100% (36/36), 13.41 KiB | 259.00 KiB/s, done.
Resolving deltas: 100% (14/14), done.
```
Suppose you want to use someone else's opensource work, or you want to contribute to their work, you can do that by cloning their repository into your local system and start working.

# 3. `git branch`
`git branch` allows you to make a branch or an _extension_ of the current files in the repository. What is the use, you may ask?
This is typically used in 2 scenerios. 
- While working with other's repository and contributing to their code, hence making a _pull request_, and.
- Making updates to an already up and running, completed and hosted projects, so that the new changes doesn't affect the main branch and hence break the whole code.</br>

To create a new branch, simply run in the current working directory: 
```
$ git branch -M <branch_name>
```
Similarly, to delete an already existing branch, you can run
```
$ git branch -d <branch_name>
Deleted branch <branch_name> (was <branch-id>).
```
The `-d` here specifies that you want to delete the mentioned branch, and hence removes all the files and changes of _that branch_.
# 4. `git checkout`
Okay, you've created a new branch and are now set to work in the new branch and make updates there. But, you're currently still in the `main` branch of your repository. How do you switch branches?
This is where `git checkout` comes into play. It simply swaps the current branch to the desired branch you want to work on. 

You can see all the available branches in a repository by running:
```
$ git branch
  branch1
  branch2
* main
```
the * before `* main` depicts that you're currently in the `main` branch. Let's say you want to move into `branch1`, you can do that by simply running:
```
$ git checkout branch1
Switched to branch 'branch1'
```
and then you can confirm the switch by running again
```
$ git branch 
* branch1
  branch2
  main
```
and so now the * is before `* branch1` which means that you're now in `branch1` branch.

# 5 `git status`
Okay, most changes are done and you are up for the day. The next day, you want to know where you left off, what are the changes that you have committed and pushed, and what you haven't. All the changes information will appear in **red** can be viewed by running the command: 
```
$ git status
```
Note that `git status` gives the status of the current branch, so make sure you are on the right branch. 

# 6. `git add`
Once you are done modifying or updating your code, you can save the files locally. But these changes and modifications won't be reflected in your Git Repository until you Add them in it. And this is where `git add` is used. 

To save the file changes into the Git Repository, or to Add new files to an empty repository, run the command: 
```
$ git add .
```
Notice the **.** after `git add .` is used to specify that you want to add all the files in the current working directory. If you want to add a specific file, you can do so my mentioning the name of the file:
```
$ git add main.py
```
If now you run `git status`, you will notice that all the changes now appear in **green** color, to show that these changes are added to the local repository and are ready to be committed and pushed.

# 7. `git commit`
Changes are added to the repository and now you want to let the repository know what changes have you done and what is their use. You can do this by **committing** your changes and adding a Commit Message.
To do so, simply run:
```
$ git commit -m "Your Commit Message Here"
```

# 8. `git remote`
You have created a Git repository on your local machine. And you have created a Git Repository on a remote git server, say, GitHub. Now You want to connect the local repository to the remote repository so that the changes you commit in this repository can also be committed to the remote repository. This is where `git remote` is used.
To connect the remote repo with the local repo, simply run:
```
$ git remote add origin https://github.com/<username>/<repository_name>.git
```

# 9. `git push`
The final step of the git operation is to push your code to the remote server where your Git Repository is hosted, be it GitHub, GitLab or any other for that matter. 
You can do this with a simple command of:
```
$ git push origin <you_branch_name>
```
or, if it your first push (to the main branch)
```
$ git push origin main
```
and this will upload your code to the remote repository and you are good to go.
<hr>
This is all about the basic git commands you need to know to get started with Git and Github and manage your code with this amazing version control tool. 

Thank you for reading. 
~ Yuvraj.
[LinkedIn](https://linkedin.com/in/yuvraj-dagur) | [Twitter](https://twitter.com/@yuvrajdagur12)
[GitHub](https://github.com/thatfreakcoder) | [Medium](https://yuvraj-dagur.medium.com/)