Learn Git & GitHub in Under 6 Minutes
Tags: git, github, gitlab, gitops
Published: 2020-11-21 08:42:41


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x0nsaqzu2v0z3b1epmp8.png)

**In this Blog, You will Learn**
* What is Git?
* Git Basic Commands
* Uploading your code to GitHub/ Bitbucket / Gitlab
* Git Push and Pull
* How to write a perfect GitHub README.md?
* Instant Markdown Cheatsheet

## What is Git?

Git is a source distributed version control system which means that you can track your changes in computer files and coordinating work on those files among multiple people. So, that's why Git is called a distributed version control system. For example, At any point, you are working on a project and your project hit with a fatal error and you don't know what causes it so with the help of Git you can also revert to the stable state of the project.
Git also helps you to synchronize your project between the different people remotely. For example, you are working with your team members on a project and whenever you make changes in the file, git takes those changes and merge them into a repository actually Git stores information about the project's progress on a repository So you don't need to worry about mailing your files each time with your team and this is how collaborating remotely can be done easily using git.

## Downloading Git

Download Git From [Official Website](https://git-scm.com/downloads)

![Alt Text](https://miro.medium.com/max/700/1*9OoOKpdzjjZ_kG_Ix9JXCA.png)

## Git Basic Commands
### Step 1
**Git Init**
```
# Initializes the current directory as a git repo
git init
```

Right-click from your mouse and there is an option called Git Bash or you can also initialize Git from CMD in your respective Working directory.

![Alt Text](https://miro.medium.com/max/578/1*htU6KEXuAwOY440BU4DQdQ.png)

![Alt Text](https://miro.medium.com/max/700/1*eEtXF1i-XCot5e_Dg-i_mA.png)

### Step 2
```
# Add file in Working directory 
nano README.md
```
![Alt Text](https://miro.medium.com/max/578/1*m9iWTvpgbpSOdRb-kEAmfg.png)

and Writing in Vim Editor We need to save our README.md file so CTRL+O and Press ENTER After this CTRL+X for the exit.

![Alt Text](https://miro.medium.com/max/700/1*5B0-kYmpekIBb5uDPWMERg.png)

Now we need to add this README.md file into Git

**Git Add**
```
# Add a single file
git add README.md
# Add all the files in the current directory
git add .

# Also adds the files present in `.gitignore`
git add -f .

```
![Alt Text](https://miro.medium.com/max/570/1*-L_g_CQXoM9v88eH2qoljg.png)


### Step 3
**Git Commit**
```
# Commits/Records the changes to the local repo
git commit -m "Initial commit"

# Does not create a new commit
# Adds the changes to the most recent commit
git commit --amend
```
![Alt Text](https://miro.medium.com/max/573/1*Rj0QBYBcqNsqsXeyOt7OCw.png)

### Step 4
**Git Status**
```
# Shows the status of the working tree
git status

# Shows the output in short format
git status -s

# Shows the branch even in short format
git status -sb

```
Let's make changes to README.md Files

![Alt Text](https://miro.medium.com/max/566/1*zHx-oX939YxoMc-mNUzuQg.png)

Just add this using the git add README.md Command and after this use the git status command to check that your working tree is clean.

![Alt Text](https://miro.medium.com/max/576/1*hTiYy-_8TTrKAiYnndvb9g.png)

### Step 5
**Git log**
```
# Shows the commit logs
git log

```
![Alt Text](https://miro.medium.com/max/576/1*aBkYBH0BLBZXVKwM01ta-Q.png)


### Step 6
**git diff**
 ```
# Shows the changes between unstaged files and the commits
git diff commitID

# Shows the changes between staged(ready-to-be-committed) files and the commits
git diff --staged
```
I know you are a little confused here! I was too with Git log Command you have following Commit and Commit ID to
commit daf7f6125168e35737ade0191ec7c2c******(HEAD -> master)
( this is unique )

```
git diff daf7f612

```
![Alt Text](https://miro.medium.com/max/541/1*-BRw0hPhC6MAUXe32rgFiQ.png)

## Uploading your Local Repository to GitHub/ Bitbucket / Gitlab

### Step 0
Create your Account on [Github](https://github.com/) if you don't have any.

### Step 1

![Alt Text](https://miro.medium.com/max/476/1*J3B9gZErfDtUxRnRTzMoXQ.png)

![Alt Text](https://miro.medium.com/max/700/1*jE9Hndth43HWgmR1R-f7KQ.png)
Github changes branches master into main so we will discuss Git Branches and Github branches in the next blog.

### Github Branch Main ( Formerly Master)
```
git branch -M main

```

### Git Remote

```
# Shows all the remotes configured and their remote URL
git remote -v

# Adds a remote
# git remote add <remote-name> <remote-url>
git remote add upstream https://github.com/something/blogs.git

# Changes the URL of the remote
git remote set-url upstream https://github.com/some-thing/blogs.git
```

![Alt Text](https://miro.medium.com/max/571/1*mUJ8swnhnkNx0E_Ez9W3qg.png)

### Git Push

```
# Pushes the local changes to the remote to keep it up-to-date
git push -u origin main

# Force push the local changes to the remote
# Usually git will not allow you to push to the remote if the remote has some commits that are not present in local repo
# This will override that check and lets you force push to the remote
# This may cause the remote to lose some commits. So use it carefully.
git push -f origin main

# Push and set the remote as upstream 
# same as `git push --set-upstream origin feature-branch`
git push -u origin feature-branch

# Deletes the branch in the remote
# same as `git push --delete origin new-feature`
git push --delete origin new-feature
```


![Alt Text](https://miro.medium.com/max/700/1*joYGU8-0ORRh753ZMD2K3Q.png)

Let's make a change and then push again.

![Alt Text](https://miro.medium.com/max/578/1*GduRT-zSbkZ0IIz-Ux6m2g.png)

![Alt Text](https://miro.medium.com/max/700/1*DOFtbnGKuYkhFBp5aXGP_A.png)

## Error # 1
**Src refspec master does not match any error when pushing to the repository.**
```
git push -u origin main — force

```
This command is dangerous and should not be used many times because this command will remove your commits and logs.

### Git Pull

Let's make a change in Github test Repo and then pull to the local machine.
```
git pull
```


![Alt Text](https://miro.medium.com/max/585/1*ryYUG1kZZfF2pHT3sDLdfQ.png)

**Finally!!**
![Alt Text](https://miro.medium.com/max/583/1*SlSUBVuEPWwZRJi6ksMhEw.png)

## How to write a perfect GitHub README.md?
Please refer to the following Blog to write a perfect [Github README.md](https://adilshehzad786.medium.com/how-to-write-a-perfect-github-readme-md-bcf9dfa75dd2)

## Instant Markdown Cheatsheet

This is Markdown Cheatsheet which can help you to get started with Github Readme and also help you with Jekyll.

[Markdown Cheatsheet]('https://github.com/adilshehzad786/Markdown-Cheatsheet-and-attractive-Readme.md-files/wiki')


##Conclusion
So, Thank you everyone for reading. I just explained the basics of Git and GitHub. I strongly encourage you guys to read more related articles on Git and GitHub. I hope this article helped you all. In Next Blogs, I will more deep into Git and also Deep into Branches. So follow me on Medium.
Please Feel free to send me any changes if require on Linkedin .and also you can feel free to connect with me on Linkedin and also on Github. Thank you.
Happy Coding!
[Linkedin](https://www.linkedin.com/in/adilshehzad7/)
[GitHub](https://github.com/adilshehzad786)