Skip to content

Latest commit

 

History

History
316 lines (217 loc) · 5.91 KB

NOTES.md

File metadata and controls

316 lines (217 loc) · 5.91 KB

Git and GitHub Guide

INDEX

  1. Introduction
  2. Configuring Git for the First Time
  3. General Git Features
  4. Git Help
  5. Git Branching
  6. Working with GitHub
  7. Git Undo

Introduction

What is Git

Git is a version control system that helps you keep track of code changes and collaborate on code with other developers.

Why Git

  • Git is widely used by over 70% of developers.
  • Developers can work together from anywhere in the world.
  • Git allows you to see the full history of the project.
  • Git helps developers revert to earlier versions of a project.

Features of Git

  • When a file is changed, added, or deleted, it is considered modified.
  • Modified files can be staged and then committed to store a permanent snapshot.
  • Git allows you to see the full history of every commit.
  • You can revert back to any previous commit.
  • Git keeps track of changes in each commit rather than storing a separate copy of every file.

What is GitHub

  • GitHub is a platform that provides tools to use Git effectively.
  • It is the largest host of source code in the world, owned by Microsoft since 2018.
  • GitHub allows developers to collaborate on projects remotely.

Configuring Git for the First Time

To set up your Git for the first time:

$ git config --global user.name "<Enter your username here>"
$ git config --global user.email "<Enter your email here>"

General Git Features

Initializing Git

Git will start tracking changes in the current folder:

$ git init

Staging Files

Staging individual files:

$ git add <filename with extension>

Staging all files in a folder:

$ git add --all
$ git add -A
$ git add .

Making a Commit

Adding commits helps keep track of progress and changes:

$ git commit -m "<Enter your message here>"

Committing without staging first:

$ git commit -a -m "<Enter your message here>"

Status of Files and Log

View the status of files:

$ git status
$ git status --short  # Compact view

View commit history:

$ git log
$ git log --oneline

Git Help

View help for specific commands:

$ git <command> -help

See all available options:

$ git help --all

To exit list view, press SHIFT + G to jump to the end and q to exit.


Git Branching

Branches allow working on different parts of a project without impacting the main branch. Once work is complete, branches can be merged back.

Making a New Git Branch

$ git branch <name of branch>

Checking All Available Branches

$ git branch

Switching to Other Branches

$ git checkout <branch name>

Making a New Branch and Directly Switching to It

$ git checkout -b <branch name>

Deleting a Branch

$ git branch -d <branch name>

Merging Two Branches

It’s preferred to switch to the master branch before merging:

$ git merge <branch name>

Working with GitHub

Push Local Repo to GitHub

Add a remote repository:

$ git remote add origin <paste copied URL here>

Push the master branch to GitHub and set it as default:

$ git push --set-upstream origin master

Push changes to GitHub after the initial setup:

$ git push origin

Pull Local Repo from GitHub

Pull changes from a remote repository:

$ git pull origin

Pull Branch from GitHub

List all branches:

$ git branch -a  # All branches
$ git branch -r  # Only remote branches

Checkout and pull a branch:

$ git checkout <branch name>
$ git pull

Push Branch to GitHub

Create a new branch and push it to GitHub:

$ git checkout -b <branch name>
$ git commit -a -m "<Message>"
$ git push origin <branch name>

Git Clone from GitHub

Clone a repository:

$ git clone <copied URL>

Clone to a specific folder:

$ git clone <copied URL> <folder name>

Git Undo

Git Revert

Revert to a previous commit:

$ git log --oneline  # Find commit hash
$ git revert HEAD --no-edit  # Revert the latest commit

Revert to an earlier commit:

$ git revert HEAD~<x>  # 'x' represents the number of commits back

Git Reset

Reset the repository to a previous commit:

$ git reset <commithash>

Undo reset (reset back to a specific commit):

$ git reset <commithash>

Git Amend

Modify the most recent commit:

$ git commit --amend -m "<Commit Message>"

Amend files:

  1. Stage the changes.
  2. Amend with:
$ git commit --amend

Thanks for reading!


This `NOTES.md` file provides a structured and easy-to-read guide for using Git and GitHub, organized in a logical manner with clear explanations of each topic.