GIT & GITHUB : Start Before Your Code Get Bugs🐛

GIT & GITHUB : Start Before Your Code Get Bugs🐛

Vcs(Version Control System)

Version control is a system that records changes to a file or a set of files over time so that you can recall specific versions later. It allows you to revert the entire project to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more.

Git

Git was originally authored by Linus Torvalds in 2005 for the development of the Linux kernel. Git is a Distributed Version Control System (DVCS). In this clients don't just check out the latest snapshot of the files-they fully mirror the repository. thus if any server dies, and these systems are collaborating via it, any of the client repositories can be copied back up to the server to restore it.

Installing Git

For Linux(for binary installer)

If you are on Fedora use the command - $ yum install git

If you are on a Debian-based distribution(like Ubuntu) use the below command -

$ apt-get install git

For Mac

Install the Xcode command line tool or homebrew (refer to git official page)

For Windows

  1. Go to your browser and paste this link - git-scm.com/download/win

  2. After opening that webpage you will see the direct link to download for Windows.

Setup Your Git

Install git for setting up your username and email address

Example -

$ git config --global user.name "John Doe"

$ git config --global user.email Johndoe.example.com

$ git config --global core.editor youreditor

Git has three states

  1. Committed - It means that the data is safely in your local database.

  2. Modified - It means that you have changed the file but have not committed it to your databases yet.

  3. Staged - It means that you have marked a modified file in its current version to go into your next commit snapshot

Create to First local Repository

  • There are two ways you can create your file:-

i) On your PC go to the file explorer and then top of the left side of the screen, you will see a plus(+) icon just click on it and create one folder.

ii) After that open that folder and again click on the (+) icon, select text document and name it as per your doc.(such as index.html, readme. md, style. css, etc.)

OR

i) Open your Internal terminal(such as PowerShell, git bash) in your PC and just type the below command to create a folder

mkdir Foldername  // for both windows & bash

ii) After creating the folder type the cd mkdir Foldername command & click the enter button to change your directory to your folder and again type the below command to create your file.

type nul > filename.extension  // for windows
touch filename.extension             // for bash

iii) After that click the Enter button, hence you'll have your repository ready

  • Initialize a repository that you created using the below command either in a terminal(bash) or in an integrated terminal in your(IDE) such as Vscode.
$ git init
  • Now you have your local git repository.

Git Basics Command

Some important git commands are explained below -

  1. git add . - this command helps to track a new file in your project

  2. git commit - this command helps to commit after staging a file

  3. git status -this command helps to check which files are in which state

  4. git diff - this command helps to see the changes in the files.

  5. git log - this command helps to see the history of the commits.

  6. git branch <branch name> - this command helps to create a new branch.

  7. git branch - this command helps to show a simple listing of your current branches.

  8. git checkout <branch name> - this command helps to switch to an existing branch.

  9. git merge <branch name> - this command helps to merge the branch to the main branch in your repository.

  10. git remote add origin <remote repository URL> - this command helps to add a remote repository to a local repository.

  11. git pull [remote-name] - this command helps to get the data from your remote repository.

  12. git push -u origin main - this command helps to upstream the remote branch for the local branch.

  13. git push - It helps to push code to a remote branch or on the server

Git has many useful commands that will improve your productivity, below I have attached the link to download the free Git cheat sheet which I created while learning Git.

👉 GIT CHEATSHEET

Github

GitHub is the single largest host for git repositories and is the central point of all collaboration for millions of developers and projects. a large number of all git repositories are hosted on GitHub, and many open-source projects use it for git hosting, issue tracking, code review, and many other things.

Setup Your GitHub Account

Go to github.com and click on the sign-up button then, set up your free account by providing a username, your email and a password, and click on the sign up for GitHub button, after verifying your email which GitHub sends you to your email.

Working With Github

After setting up your GitHub account, let's see how to make a copy of any public(open-source project) repository on Git Hub.

  1. Go to Git Hub and find any repository that is familiar with your skills & hit click on that repository.

  2. See the fork option on the above right-hand side of the repo, click on that option.

  3. After clicking the fork button(Github makes a copy of the project that is entirely yours),you'll redirected to another window and on that window click on the Create Fork button

  4. Right now, you have your remote repository not a local repository on your Computer or PC. To create that you have to clone it.

  5. For cloning that repo, check below METHOD 3

The given methods will help you connect remote repos to local or vice-versa

  • To add a local repository to remote (METHOD 1)
  1. Create a new repo on GitHub and copy the clone https code

  2. $ git branch -M main (on local repo)

  3. $ git remote add origin https://github.com/XYZ

  4. $ git remote

  5. $ git remote -v

  6. $ git push -u origin

  • To add a local repository to remote (METHOD 2)
  1. Navigate to the project directory using the terminal (or command prompt).

  2. Initialize a new Git repository using the command git init.

  3. Add all the files to the staging area using git add . (note the period after "add").

  4. Commit the changes using git commit -m "commit message".

  5. Create a new repository on GitHub.

  6. Copy the URL of the remote repository.

  7. Add the remote repository using the command git remote add origin <remote repository URL>.

  8. Push the changes to GitHub using git push -u origin main.

  • How to clone a repository from GitHub(METHOD 3)

  1. Go to the GitHub website (github.com) and navigate to the repository you want to clone.

  2. On the repository's main page, click on the "Code" button. It will open a dropdown with options to clone the repository using HTTPS, SSH, or GitHub CLI. Choose the method you prefer.

    • If you choose HTTPS, click on the clipboard icon to copy the URL to your clipboard.

    • If you choose SSH, click the "Use SSH" button, and a similar clipboard icon will appear to copy the SSH URL.

  3. Open a terminal or command prompt on your local machine.

  4. Navigate to the directory where you want to clone the repository. You can use the cd command to change directories.

  5. Once you are in the desired directory, use the git clone command followed by the URL you copied earlier. git clone https://github.com/username/repository.git

  6. Press Enter to execute the command. Git will connect to the GitHub repository and download the files to your local machine. The repository will be cloned into a new directory with the same name as the repository.

  7. Once the cloning process is complete, you will have a local copy of the repository on your machine.

Contribute to a public repository on GitHub

Follow the below steps to contribute to open-source projects

  1. Clone the forked repo of the project locally.

  2. Create a descriptive topic branch.

  3. Make changes to the code.

  4. Check that the change is good.

  5. Commit the change to the topic branch

  6. Push that new topic branch to commit back up to the forked project

  7. Open a pull request on GitHub.

  8. After that, the project owner either merges your code or closes your pull request. after discussing it with you.

Conclusion:-

Hence, Git is a version control system that records changes to a file or set of files over time. It allows users to revert the entire project to a previous state, compare changes over time, and more. Git is a Distributed Version Control System (DVCS) that fully mirrors the repository. on the other hand, GitHub is the largest host for git repositories and a central point of collaboration for millions of developers and open-source projects.

Follow Me On Socials :

Linkedin

Twitter

Github

Like👍|Share📲|Comment💭

Did you find this article valuable?

Support MOHD NEHAL KHAN by becoming a sponsor. Any amount is appreciated!