- 1. Create a repository on GitHub
- 2. Prepare your local folder
- 3. Connect to GitHub remote
- 4. Rename local branch to main
- 5. Set upstream branch
- 6. Authentication (Recommended)
- 7. Clone an existing repository
- 11. Everyday workflow
- 13. Fetch without merge
- 14. Update branch with latest main
- 24. Check commit history
- 25. Stash changes
- 29. Diff & inspect
- 15. Pull Request workflow
- 16. Fork & Upstream workflow
- 26. Branching
- 27. Merging branches
- 28. Tagging
- 30. Cherry-pick
- 33. Submodules
- 41. Conventional Commits (Team Standard)
- 8. Handle rejected push
- 9. Handle merge conflicts during rebase
- 10. Force push (last resort)
- 12. Upstream branch mismatch
- 17. Amend commit
- 18. Handle merge conflicts (non-rebase)
- 19. Undo last commit (keep changes)
- 20. Undo last commit (discard changes)
- 21. Rescue commits from protected main (GH006 error)
- 22. Hard reset local branch to match remote
- 23. Restore & unstage
- 31. Revert
- 32. Clean untracked files
- 37. Fix detached HEAD
- 38. Recover deleted branch
- 39. Interactive rebase
- 40. Squash commits
- 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>.gitcd /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"git remote add origin https://github.com/<github_username>/<repository_name>.gitVerify:
git remote -vShow detailed remote information:
git remote show origingit branch -M maingit push -u origin mainAfter first push:
git pushWindows:
git config --global credential.helper managerGitHub CLI:
gh auth loginVerify login:
gh auth statusgit clone https://github.com/<user>/<repo>.git
cd <repo>Clone a specific branch:
git clone -b <branch_name> https://github.com/<user>/<repo>.gitIf you see:
rejected - fetch first
non-fast-forward
Run:
git pull origin main --rebaseThen:
git push origin mainKeep your version:
git checkout --ours <file>
git add <file>
git rebase --continueKeep remote version:
git checkout --theirs <file>
git add <file>
git rebase --continueAbort rebase:
git rebase --abortEditor shortcuts:
Vim:
:wq
Nano:
Ctrl+X
Safer:
git push origin main --force-with-leaseDangerous:
git push origin main --forceOnly use when necessary.
git add .
git commit -m "your message"
git pushIf you see:
fatal: The upstream branch of your current branch does not match
Run:
git branch --set-upstream-to=origin/main mainFetch latest changes:
git fetch originFetch all remotes:
git fetch --allDifference:
git fetchDownloads changes only.
git pullDownloads + merges/rebases.
Clean deleted remote branches from local list:
git fetch --pruneUsing rebase:
git checkout <branch_name>
git fetch origin
git rebase origin/mainUsing merge:
git checkout <branch_name>
git fetch origin
git merge origin/maingit checkout -b feature-name
git add .
git commit -m "feat: description"
git push -u origin feature-nameThen:
- Open GitHub
- Click Compare & Pull Request
- Create Pull Request
- Wait for review
After PR is merged on GitHub (Clean up):
git switch main
git pull origin main
git branch -d feature-name
git fetch --pruneUsing GitHub CLI (gh) instead of browser:
gh pr create --base main --title "feat: description" --body "PR details"
gh pr list
gh pr checkout <pr_number>
gh pr review --approve
gh pr merge --merge --delete-branchAdd original repository as upstream:
git remote add upstream https://github.com/<original_owner>/<repo>.gitSync fork with original repository:
git fetch upstream
git switch main
git merge upstream/main
git push origin mainChange last commit message:
git commit --amend -m "new message"Add forgotten files:
git add .
git commit --amend --no-editCheck conflicts:
git statusKeep local version:
git checkout --ours <file>Keep remote version:
git checkout --theirs <file>Resolve:
git add <file>
git commit -m "resolve merge conflict"
git pushgit reset --soft HEAD~1git reset --hard HEAD~1Move accidental commits made directly on main to a new branch and reset local main:
git switch -c fix-branch-name
git push -u origin fix-branch-name
git switch main
git reset --hard origin/mainDiscard all local changes/commits and match GitHub 100%:
git fetch origin
git reset --hard origin/mainDiscard file changes:
git restore <file>Discard all unstaged changes:
git restore .Unstage file:
git restore --staged <file>Legacy:
git reset HEAD <file>git log --onelinegit log --oneline --graph --allgit log --pretty=format:"%h %an %ad %s" --date=shortgit stashgit stash listgit stash popgit stash apply stash@{0}git stash drop stash@{0}git stash clearCreate:
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 -aDelete 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>git checkout maingit merge <branch_name>git merge --no-ff <branch_name> -m "merge: <branch_name> into main"Abort:
git merge --abortgit tag v1.0.0git tag -a v1.0.0 -m "Release version 1.0.0"git taggit push origin v1.0.0git push origin --tagsgit tag -d v1.0.0git push origin --delete tag v1.0.0git diffgit diff --stagedgit diff main..<branch_name>git show <commit_hash>git blame <file>git cherry-pick <commit_hash>git cherry-pick <commit_hash> --no-commitAbort:
git cherry-pick --abortgit revert <commit_hash>git revert <commit_hash> --no-commitPreview:
git clean -nDelete files:
git clean -fDelete files + folders:
git clean -fdDelete everything:
git clean -fdxgit submodule add https://github.com/<user>/<repo>.git <path>git clone --recurse-submodules https://github.com/<user>/<repo>.gitgit submodule update --remoteRemove:
git submodule deinit <path>
git rm <path>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 lggit config --global user.name "Your Name"git config --global user.email "[email protected]"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 mainView config:
git config --listCreate:
touch .gitignoreCommon 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"git checkout -b <new_branch_name>Then:
git checkout main
git merge <new_branch_name>git refloggit checkout -b <branch_name> <commit_hash>git rebase -i HEAD~<N>Commands:
- pick
- reword
- squash
- drop
Never rewrite shared branch history.
git reset --soft HEAD~3
git commit -m "combined commit"
git push origin main --force-with-leasefeat:add a new feature (e.g.,feat: add login page)fix:fix a bug (e.g.,fix: resolve null pointer error)docs:documentation updates (e.g.,docs: update setup guide)style:formatting/CSS changes without logic changesrefactor:code restructuring without changing behaviorchore:maintenance tasks, dependencies, or config updates
- Git ignores empty folders → use
.gitkeep git fetchdoes not mergegit pull= fetch + merge/rebasegit revertis safer thangit resetgit reflogcan recover most mistakes- Prefer
--force-with-leaseover--force - Use
.gitignoreearly - Never force push to shared production branches
- Always pull or fetch before major work
--ourskeeps local changes--theirskeeps incoming changes- Detached HEAD is not dangerous if you create a branch before leaving it