Skip to content

This Repository contains info about GitHub basics that help beginner's how to use GitHub and contributing to opensource.

Notifications You must be signed in to change notification settings

nakul-py/github-basics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 

Repository files navigation

Github Basics

🧲 00 - How to Clone a GitHub Repository

πŸš€ What Does β€œCloning” Mean?

Cloning means copying a remote Git repository to your local machine. This lets you work with the project files, branches, and history locally.

πŸ”§ Requirements

βœ… You need:

  • Git installed (git --version to check)
  • A GitHub account (for some repos)
  • SSH or HTTPS access to the repo.

🌐 Choose a Clone URL

You can clone via:

  • HTTPS (recommended for beginners)

  • SSH (for advanced users with SSH keys)

Example GitHub repo URL:

https://github.com/username/repo-name.git

πŸ›  Clone the Repo

git clone https://github.com/username/repo-name.git

This will:

  • Create a folder named repo-name

  • Copy all code, commit history, and branches into it

🧭 Navigate Into the Project

cd repo-name

πŸ“Œ Check the Remote URL

git remote -v

Output:

origin  https://github.com/username/repo-name.git (fetch)
origin  https://github.com/username/repo-name.git (push)

🌿 01 - Branches in Git

🧠 What is a Branch?

A branch in Git lets you work on a separate line of development without affecting the main project. You can:

  • Try new features

  • Fix bugs

  • Work independently

The default branch is usually called main (or master in older repos).

πŸ”§ Basic Branch Commands

🧾 View Branches

git branch

🌱 Create a New Branch

git branch feature-xyz

πŸ” Switch to a Branch

git checkout feature-xyz

Or in one step (Git 2.23+):

git checkout -b feature-xyz

# or

git switch -b feature-xyz

πŸ“‚ List Remote Branches

git branch -r

πŸ”„ Merge a Branch into main

git checkout main
git merge feature-xyz

🧼 Delete a Branch

git branch -d feature-xyz    # Only if merged
git branch -D feature-xyz    # Force delete

πŸš€ 02 - Git Add, Stash, and Push

πŸ“¦ What is git add?

git add tells Git you want to include changes in the next commit.

πŸ”§ Common Commands

  • Add a single file:
git add filename.txt
  • Add all changed files:
git add .

🧩 Forcing a File to Be Tracked

Sometimes, Git will ignore files listed in .gitignore. But what if you intentionally want to commit one of those ignored files?

For example:

git add --force .yarnclean 

# or

git add --force filename.txt

This command means:

  • git add β†’ Stage the file

  • -f or --force β†’ Force Git to add the file even if it’s ignored

  • .yarnclean β†’ The file used by Yarn to clean unnecessary packages

πŸ’‘ Why This is Useful

In some projects, .yarnclean or any other file is added to .gitignore, but you might still want to track it for consistent cleanup across machines.


πŸ§™ What is git stash?

git stash temporarily saves uncommitted changes, letting you switch branches or pull updates without losing work.

πŸ”§ Common Commands

  • Stash current changes:
git stash
  • View stashed changes:
git stash list
  • Re-apply last stashed changes:
git stash pop
  • Drop a stash (delete it):
git stash drop

Tip: Use git stash -u or git stash --include-untracked to include untracked files too.


πŸš€ What is git push?

git push sends your local commits to the remote repository.

πŸ”§ Common Commands

  • Push current branch:
git push origin branch-name
  • Push and set upstream (first time):
git push --set-upstream origin branch-name

# or

git push -u origin branch-name

πŸ“œ 03 - Viewing Commit History with git log

🧠 What is git log?

git log shows the history of commits in your current branch β€” what was committed, by whom, and when.

πŸ”§ Basic Usage

git log

Output:

commit 6e4f3f8f (HEAD -> feature-xyz, origin/feature-xyz)
Author: Firstname Lastname <user@example.com>
Date:   Sun Jan 1 00:00:00 2022 +0000

    feat: Add new file

🧩 Useful Options

Command What it does
git log --oneline Show each commit in one line (short hash + message)
git log --graph Show a visual commit tree
git log -p Show the diff introduced by each commit
git log --stat Show files changed + insertions/deletions
git log --author="nakul" Show commits by a specific author
git log --since="2 days ago" Show recent commits
git log -- <filename> Show commits that touched a specific file

πŸ—‘οΈ 04 - Remove a Commit, Push, and Retrieve It (Force Push & Recovery)

🧾 Use git log to Find a Commit

git log

❌ How to Remove the Last Commit

πŸ”Ή 1. Remove last commit but keep changes (editable)

git reset --soft HEAD~1

This keeps your changes in staging (git status will show them as "to be committed").

πŸ”Ή 2. Remove last commit and unstage changes

git reset --mixed HEAD~1

Changes go back to the working directory, not staged.

πŸ”Ή 3. Remove last commit and discard changes (permanent)

git reset --hard HEAD~1

⚠️ This deletes the commit and all local changes.


πŸš€ Push the Updated History

After removing a commit, your local branch history is now different HEAD from the remote. So you'll need to force push:

git push origin --force

πŸ”„ Retrieve a Removed Commit (If You Didn't Hard Reset)

You can retrieve commits even after removal, if they haven’t been garbage collected.

πŸ•΅οΈ Find the commit hash

git reflog

This shows a log of all HEAD movements β€” even ones you've undone.

Example:

abc1234 HEAD@{1}: commit: added something

πŸ§™β€β™‚οΈ Recover it

git checkout abc1234
# or
git reset --hard abc1234

🧠 Tip: Safer Way to Undo Commits

Instead of git reset, you can also use git revert:

git revert HEAD

This creates a new commit that undoes the last one β€” safer for public branches.

πŸš€ 05 - Pulling Updates from a Remote Repository

🧠 What is git pull?

git pull updates your local branch with the latest changes from the remote repository.

πŸ”§ Common Command

git pull

πŸ§™ What is git fetch?

git fetch downloads updates from the remote repository without merging them into your local branch.

πŸ”§ Common Command

git fetch

πŸš€ What is git merge?

git merge merges changes from one branch into another.

πŸ”§ Common Command

git merge feature-xyz

πŸš€ What is git rebase?

git rebase updates the history of a branch by applying the commits from another branch on top of it.

πŸ”§ Common Command

git rebase feature-xyz

πŸš€ What is git reset?

git reset undoes local changes to tracked files.

πŸ”§ Common Command

git reset --hard HEAD

πŸ“Œ 06 - Working with Remotes

🧠 What is git remote?

git remote lets you list and add remotes.

πŸ”§ Common Commands

  • List remotes:
git remote
  • Add a remote:
git remote add origin https://github.com/username/repo-name.git
  • Remove a remote:
git remote remove origin

About

This Repository contains info about GitHub basics that help beginner's how to use GitHub and contributing to opensource.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published