Git & Version Control

Git

To check git version command:

git --version

Git Configuration

git config --global user.name "Manan Academy"
git config --global user.email "contact@mananacademy.com"

To check existing user & user email:

git config --global user.name
git config --global user.email

To change existing configuration:

git config --global edit

Initialize or Clone Repository

To start a new project:

mkdir my-project
cd my-project
git init  # Will create a repository in a folder

To check changes/modification inside a repository:

git add <filename>
git status

Clone an existing Github repository:

git clone https://github.com/torvalds/linux.git

Clone a specific branch:

git clone -b master https://github.com/user/project.git

To see previous commit:

git log

Stage Files (Prepare for Commit)

Track a single file:

git add index.html

Track all changes:

git add .

Undo staging:

git reset index.html

Remove a tracked file:

git rm old_file.txt

Commit Commands

git commit -m "Add login page"

Add + commit tracked files:

git commit -am "Fix navbar"

To fix Fix last commit message

git commit --amend -m "Improve navbar styles"

Branching Examples

Make a new feature branch:

git branch feature/auth

Switch to it:

git switch feature/auth

Or old method:

git checkout feature/auth

Create + switch:

git checkout -b feature/auth

Delete branch:

git branch -d feature/auth

Force delete:

git branch -D feature/auth

Merge & Rebase

Merge “develop” branch into current branch:

git merge develop

Abort merge if conflict too messy:

git merge --abort

Push & Pull

Push to GitHub:

git push origin main

Push a new branch for the first time:

git push -u origin feature/auth

Pull updates:

git pull

Pull using rebase (clean history):

git pull --rebase

Fetch remote changes without merging:

git fetch
git diff origin/main

Remote Repository Examples

Check remotes:

git remote -

Add GitHub URL:

git remote add origin https://github.com/user/repo.git

Change remote URL:

git remote set-url origin git@github.com:user/repo.git

View History & Differences

Full history:

git log

Clean, one-line history:

git log --oneline --graph

View details of a commit:

git show 7e23a1f

Compare working branch with main

git diff main

Compare two branches:

git diff feature/auth develop

Stash Examples

Save temporary changes:

git stash

Save with message:

git stash save "Halfway through navbar redesign"

Apply latest stash:

git stash apply

Apply and delete stash:

git stash pop

Show all stashes:

git stash list

Leave a Reply