Sagar Medtiya
Sagar Medtiya

Sagar Medtiya

Learn to use Git and GitHub Efficiently!!!🚀🚀

Learn to use Git and GitHub Efficiently!!!🚀🚀

A Complete Beginner's tutorial to get started using Git and GitHub✨

Sagar Medtiya's photo
Sagar Medtiya
·Sep 1, 2022·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • 🍁What is a Git?
  • 🍁What is a GitHub?
  • 🍁How to install Git?
  • 🍁Git Commands
  • 🍁Move backward to the previous state
  • 🍁Git Lifecycle
  • 🍁Start a new repository and publish it to GitHub
  • 🍁Summary

🍁What is a Git?

Git is an open-source distributed version control system(DVCS). Especially, it is a system used to record changes to files over time, so you can see what all changes you have made later on.

A version control system simply tracks the history of changes whenever people or team collaborate to do project together. As the project evolves team can test, run and contribute to the code with confidence that any version can be recovered anytime if something happens. Developers can review the following:

  • Which changes were made?
  • Who made the changes?
  • When were the changes made?

🍁What is a GitHub?

GitHub is a website and cloud-based service that helps developers store and manage their code, as well as track and control changes to their code. GitHub's interface is user-friendly enough so that the novice coders can take advantage of Git. Without GitHub, using Git generally requires a bit more technical savvy and use of the command line. With addition to that, anyone can SignIn and host a public repository freely which makes GitHub especially popular with open-source projects.

🍁How to install Git?

You can download your Git version according to your OS. Here is the Link

🍁Git Commands

  • git init

    It initializes a new git repository and starts tracking the existing files. It adds a hidden sub folder .git within the existing folder that contains internal data structure required for version control.

Syntax: git init <directory>

  • git clone

    It creates the local copy of the existing remote repository. The clone includes all the project's files, history and branches.

Syntax: git clone <repo>

  • git add

    It stages a change. This command performs staging, the first part of that two-step process. Any changes that are staged will become a part of the next snapshot and a part of the project’s history. Staging and committing separately gives developers complete control over the history of their project without changing how they code and work.

Syntax: git add <directory>

  • git commit -m

    It saves the snapshot to the project history and completes the change-tracking process. In short, a commit functions like taking a photo. Anything that’s been staged with git add will become a part of the snapshot with git commit. -m attribute is used to write the message.

Syntax: git commit -m <message>

  • git status

    It shows the status of changes as untracked, modified, or staged.

Syntax: git status

  • git branch

    It shows all the branches being worked on locally.

Syntax: git branch

  • git merge

    This command is typically used to combine changes made on two distinct branches. For example, a developer would merge when they want to combine changes from a feature branch into the master branch for deployment.

Syntax: git merge <branch>

  • git pull

It updates the local line of development with updates from its remote counterpart. Developers use this command if a teammate has made commits to a branch on a remote, and they would like to reflect those changes in their local environment.

Syntax: git pull --rebase <remote>

  • git push

It updates the remote repository with any commits made locally to a branch.

Syntax: git push <remote-repo> --all

🍁Move backward to the previous state

If we found a bug, we must trace from the beginning of the code, looking into line per line of codes. Git simplifies our work with the move backward feature. Instead of looking from the beginning of works, we can track based on the previous commit before a bug was found. There are three commands to move back on git. It is used for our needs.

git checkout: it is like a time machine, we can restore the condition of the project file to the designated time. However, this is temporary. These are not stored in the git database

git reset: this command makes us unable to go back to the future. We will lose our commits. After the git logs were reset, we need to write a new commit

git revert: will take the existing file condition in the past, then merge them with the last commit

🍁Git Lifecycle

The git lifecycle is divided into four states namely:

  • Modified: any changes didn’t been marked yet. We can do anything here, manipulate files, create or delete a new folder, and other things.

  • Staged: a condition when our changes have been marked but didn’t been recorded yet.

  • Committed: folders or files successfully are been recorded into our .git folder.

🍁Start a new repository and publish it to GitHub

Let's create a new repository and publish it to GitHub. Do not initialise the repository with a README, .gitignore or License. This empty repository will await your code.

# create a new directory, and initialize it with git-specific functions
git init my-repo

# change into the `my-repo` directory
cd my-repo

# create the first file in the project
touch README.md

# git isn't aware of the file, stage it
git add README.md

# take a snapshot of the staging area
git commit -m "add README to initial commit"

# provide the path for the repository you created on github
git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git

# push changes to github
git push --set-upstream origin master
git push -u origin master (Alternative)

🍁Summary

This is the end of our tutorial, coders! Learn more about the git cheat sheet!

Thanks for reading this blog. Hope you have liked it!💗

Did you find this article valuable?

Support Sagar Medtiya by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this