Skip to content

Latest commit

 

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

Git & GitHub Setup Guide

1️⃣ Create a repository on GitHub

  • Go to GitHub → New repository
  • Enter the repository name same as your folder (e.g., my_repository)
  • Do NOT check README / .gitignore / License
  • Copy the repository URL

Example:

https://github.com/<github_username>/<repository_name>.git

2️⃣ Prepare your local folder

cd /path/to/your/folder

git init
git add .
git commit -m "Initial commit"

If the folder is empty:

touch .gitkeep
git add .
git commit -m "Initial commit"

3️⃣ Connect to GitHub remote

git remote add origin https://github.com/<github_username>/<repository_name>.git

Verify:

git remote -v

Show detailed remote information:

git remote show origin

4️⃣ Rename local branch to main

git branch -M main

5️⃣ Set upstream branch

git push -u origin main

After first push:

git push

6️⃣ Authentication (Recommended)

Windows:

git config --global credential.helper manager

GitHub CLI:

gh auth login

Verify login:

gh auth status

7️⃣ Clone an existing repository

git clone https://github.com/<user>/<repo>.git

cd <repo>

Clone a specific branch:

git clone -b <branch_name> https://github.com/<user>/<repo>.git

8️⃣ Handle rejected push

If you see:

rejected - fetch first
non-fast-forward

Run:

git pull origin main --rebase

Then:

git push origin main

9️⃣ Handle merge conflicts during rebase

Keep your version:

git checkout --ours <file>
git add <file>
git rebase --continue

Keep remote version:

git checkout --theirs <file>
git add <file>
git rebase --continue

Abort rebase:

git rebase --abort

Editor shortcuts:

Vim:

:wq

Nano:

Ctrl+X

🔟 Force push (last resort)

Safer:

git push origin main --force-with-lease

Dangerous:

git push origin main --force

Only use when necessary.

1️⃣1️⃣ Everyday workflow

git add .
git commit -m "your message"
git push

1️⃣2️⃣ Upstream branch mismatch

If you see:

fatal: The upstream branch of your current branch does not match

Run:

git branch --set-upstream-to=origin/main main

1️⃣3️⃣ Fetch without merge

Fetch latest changes:

git fetch origin

Fetch all remotes:

git fetch --all

Difference:

git fetch

Downloads changes only.

git pull

Downloads + merges/rebases.

1️⃣4️⃣ Update branch with latest main

Using rebase:

git checkout <branch_name>

git fetch origin

git rebase origin/main

Using merge:

git checkout <branch_name>

git fetch origin

git merge origin/main

1️⃣5️⃣ Pull Request workflow

git checkout -b feature-name

git add .

git commit -m "feat: description"

git push -u origin feature-name

Then:

  1. Open GitHub
  2. Click Compare & Pull Request
  3. Create Pull Request
  4. Wait for review

1️⃣6️⃣ Amend commit

Change last commit message:

git commit --amend -m "new message"

Add forgotten files:

git add .

git commit --amend --no-edit

1️⃣7️⃣ Handle merge conflicts (non-rebase)

Check conflicts:

git status

Keep local version:

git checkout --ours <file>

Keep remote version:

git checkout --theirs <file>

Resolve:

git add <file>

git commit -m "resolve merge conflict"

git push

1️⃣8️⃣ Undo last commit (keep changes)

git reset --soft HEAD~1

1️⃣9️⃣ Undo last commit (discard changes)

git reset --hard HEAD~1

2️⃣0️⃣ Restore & unstage

Discard file changes:

git restore <file>

Discard all unstaged changes:

git restore .

Unstage file:

git restore --staged <file>

Legacy:

git reset HEAD <file>

2️⃣1️⃣ Check commit history

git log --oneline
git log --oneline --graph --all
git log --pretty=format:"%h %an %ad %s" --date=short

2️⃣2️⃣ Stash changes

git stash
git stash list
git stash pop
git stash apply stash@{0}
git stash drop stash@{0}
git stash clear

2️⃣3️⃣ Branching

Create:

git branch <branch_name>

Switch:

git checkout <branch_name>

Modern:

git switch <branch_name>

Create and switch:

git checkout -b <branch_name>

Modern:

git switch -c <branch_name>

List:

git branch -a

Delete local:

git branch -d <branch_name>

Force delete:

git branch -D <branch_name>

Delete remote:

git push origin --delete <branch_name>

Rename:

git branch -m <new_name>

2️⃣4️⃣ Merging branches

git checkout main
git merge <branch_name>
git merge --no-ff <branch_name> -m "merge: <branch_name> into main"

Abort:

git merge --abort

2️⃣5️⃣ Tagging

git tag v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
git tag
git push origin v1.0.0
git push origin --tags
git tag -d v1.0.0
git push origin --delete tag v1.0.0

2️⃣6️⃣ Diff & inspect

git diff
git diff --staged
git diff main..<branch_name>
git show <commit_hash>
git blame <file>

2️⃣7️⃣ Cherry-pick

git cherry-pick <commit_hash>
git cherry-pick <commit_hash> --no-commit

Abort:

git cherry-pick --abort

2️⃣8️⃣ Revert

git revert <commit_hash>
git revert <commit_hash> --no-commit

2️⃣9️⃣ Clean untracked files

Preview:

git clean -n

Delete files:

git clean -f

Delete files + folders:

git clean -fd

Delete everything:

git clean -fdx

3️⃣0️⃣ Submodules

git submodule add https://github.com/<user>/<repo>.git <path>
git clone --recurse-submodules https://github.com/<user>/<repo>.git
git submodule update --remote

Remove:

git submodule deinit <path>

git rm <path>

3️⃣1️⃣ Aliases

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all"

Usage:

git st
git co main
git br
git lg

3️⃣2️⃣ Global config

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

VSCode:

git config --global core.editor "code --wait"

Vim:

git config --global core.editor "vim"

Nano:

git config --global core.editor "nano"

Default branch:

git config --global init.defaultBranch main

View config:

git config --list

3️⃣3️⃣ .gitignore

Create:

touch .gitignore

Common entries:

node_modules/
.env
.env.local
.DS_Store
dist/
build/
*.log
.next/
.vercel/

Stop tracking a file:

git rm --cached <file>

git add .

git commit -m "remove file from tracking"

3️⃣4️⃣ Fix detached HEAD

git checkout -b <new_branch_name>

Then:

git checkout main

git merge <new_branch_name>

3️⃣5️⃣ Recover deleted branch

git reflog
git checkout -b <branch_name> <commit_hash>

3️⃣6️⃣ Interactive rebase

git rebase -i HEAD~<N>

Commands:

  • pick
  • reword
  • squash
  • drop

Never rewrite shared branch history.

3️⃣7️⃣ Squash commits

git reset --soft HEAD~3

git commit -m "combined commit"

git push origin main --force-with-lease

⚠️ Notes

  • Git ignores empty folders → use .gitkeep
  • git fetch does not merge
  • git pull = fetch + merge/rebase
  • git revert is safer than git reset
  • git reflog can recover most mistakes
  • Prefer --force-with-lease over --force
  • Use .gitignore early
  • Never force push to shared production branches
  • Always pull or fetch before major work
  • --ours keeps local changes
  • --theirs keeps incoming changes
  • Detached HEAD is not dangerous if you create a branch before leaving it

About

Git & GitHub setup guide from local to remote repository

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors