- 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.
Using 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
Change 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~1Discard 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 "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 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-lease- 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