DEV Community

Cover image for Learn Git & GitHub in Under 6 Minutes
Adil Shahzad
Adil Shahzad

Posted on

Learn Git & GitHub in Under 6 Minutes

Alt Text

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

Alt Text

Git Basic Commands

Step 1

Git Init

# Initializes the current directory as a git repo
git init
Enter fullscreen mode Exit fullscreen mode

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

Alt Text

Step 2

# Add file in Working directory 
nano README.md
Enter fullscreen mode Exit fullscreen mode

Alt Text

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

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 .

Enter fullscreen mode Exit fullscreen mode

Alt Text

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
Enter fullscreen mode Exit fullscreen mode

Alt Text

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

Enter fullscreen mode Exit fullscreen mode

Let's make changes to README.md Files

Alt Text

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

Step 5

Git log

# Shows the commit logs
git log

Enter fullscreen mode Exit fullscreen mode

Alt Text

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Alt Text

Uploading your Local Repository to GitHub/ Bitbucket / Gitlab

Step 0

Create your Account on Github if you don't have any.

Step 1

Alt Text

Alt Text
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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Alt Text

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
Enter fullscreen mode Exit fullscreen mode

Alt Text

Let's make a change and then push again.

Alt Text

Alt Text

Error # 1

Src refspec master does not match any error when pushing to the repository.

git push -u origin main — force

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Alt Text

Finally!!
Alt Text

How to write a perfect GitHub README.md?

Please refer to the following Blog to write a perfect Github README.md

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

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
GitHub

Top comments (0)