A quick reference for the most common and useful Git commands.
π Setup & Initialization
Command |
Description |
git init |
Initialize a new Git repository in the current directory. |
git clone [repo-url] |
Clone an existing repository. |
git remote -v |
List remote repositories connected to your project. |
git remote add origin [url] |
Link a local repo to a remote repo. |
git branch -M [new-name] |
Rename the current branch (commonly main ). |
π Working with Files
Command |
Description |
git add [file/dir] |
Stage a file or directory for commit. |
git add . |
Stage all changes in the current directory. |
git rm [file] |
Remove a file and stage the removal. |
git mv [old] [new] |
Rename or move a file. |
πΎ Saving Changes
Command |
Description |
git commit -m "msg" |
Commit staged changes with a message. |
git commit -am "msg" |
Stage and commit tracked files in one step. |
git reset --soft [hash] |
Undo commits but keep changes staged. |
git reset [hash] |
Undo commits and unstage changes (keep in working dir). |
git reset --hard [hash] |
Reset everything to a specific commit (β destructive). |
git revert [hash] |
Create a new commit that undoes a specific commit. |
git revert --continue |
Continue revert if conflicts occur. |
git revert --abort |
Cancel an in-progress revert. |
πΏ Branching & Merging
Command |
Description |
git branch |
List all branches, * shows current branch. |
git branch [name] |
Create a new branch. |
git branch [new] [existing] |
Create new branch from another. |
git checkout [branch] |
Switch to an existing branch. |
git checkout -b [name] |
Create and switch to a new branch. |
git merge [branch] |
Merge a branch into the current branch. |
git push --set-upstream origin [branch] |
Push new branch and set remote tracking. |
π Sync with Remote
Command |
Description |
git pull |
Fetch and merge from remote. |
git fetch |
Download changes but donβt merge yet. |
git push |
Push commits to remote. |
git push -u origin main |
Push and set default upstream (first push). |
π Check Status & History
Command |
Description |
git status |
Show staged/unstaged/untracked files. |
git log |
Show commit history. |
git log --oneline --graph --decorate --all |
Pretty, compact commit history. |
git diff [file] |
Show unstaged changes for a file. |
git diff --staged |
Show staged changes (ready to commit). |
π Stash & Temporary Work
Command |
Description |
git stash |
Save uncommitted changes temporarily. |
git stash list |
Show list of stashed changes. |
git stash apply [id] |
Apply stashed changes (keeps in stash). |
git stash pop |
Apply and remove from stash. |
βοΈ Resolving Merge Conflicts
git checkout main
git pull origin main
git checkout your-branch
git merge main
# Resolve conflicts manually in files
git add .
git commit -m "fix merge conflict"
git push
Top comments (0)