Cloning means copying a remote Git repository to your local machine. This lets you work with the project files, branches, and history locally.
β You need:
- Git installed (git --version to check)
- A GitHub account (for some repos)
- SSH or HTTPS access to the repo.
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
git clone https://github.com/username/repo-name.gitThis will:
-
Create a folder named
repo-name -
Copy all code, commit history, and branches into it
cd repo-namegit remote -vOutput:
origin https://github.com/username/repo-name.git (fetch)
origin https://github.com/username/repo-name.git (push)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).
git branchgit branch feature-xyzgit checkout feature-xyzOr in one step (Git 2.23+):
git checkout -b feature-xyz
# or
git switch -b feature-xyzgit branch -rgit checkout main
git merge feature-xyzgit branch -d feature-xyz # Only if merged
git branch -D feature-xyz # Force deletegit add tells Git you want to include changes in the next commit.
- Add a single file:
git add filename.txt- Add all changed files:
git add .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.txtThis command means:
-
git addβ Stage the file -
-for--forceβ Force Git to add the file even if itβs ignored -
.yarncleanβ The file used by Yarn to clean unnecessary packages
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.
git stash temporarily saves uncommitted changes, letting you switch branches or pull updates without losing work.
- 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 dropTip: Use git stash -u or git stash --include-untracked to include untracked files too.
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-namegit log shows the history of commits in your current branch β what was committed, by whom, and when.
git logOutput:
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| 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 |
git loggit reset --soft HEAD~1This keeps your changes in staging (git status will show them as "to be committed").
git reset --mixed HEAD~1Changes go back to the working directory, not staged.
git reset --hard HEAD~1After removing a commit, your local branch history is now different HEAD from the remote. So you'll need to force push:
git push origin --forceYou can retrieve commits even after removal, if they havenβt been garbage collected.
git reflogThis shows a log of all HEAD movements β even ones you've undone.
Example:
abc1234 HEAD@{1}: commit: added somethinggit checkout abc1234
# or
git reset --hard abc1234Instead of git reset, you can also use git revert:
git revert HEADThis creates a new commit that undoes the last one β safer for public branches.
git pull updates your local branch with the latest changes from the remote repository.
git pullgit fetch downloads updates from the remote repository without merging them into your local branch.
git fetchgit merge merges changes from one branch into another.
git merge feature-xyzgit rebase updates the history of a branch by applying the commits from another branch on top of it.
git rebase feature-xyzgit reset undoes local changes to tracked files.
git reset --hard HEADgit remote lets you list and add remotes.
- 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