Amr Ayman

Git Commands Cheat Sheet
🕑 4 mins
Here is the most used Git commands that every software engineer needs everyday
| Command | Description |
|---|---|
| Main Operations | |
git init |
Initialize current project folder for version control (creates .git files) |
git status |
Show status of current branch: the changes that need to be staged (tracked) or committed |
git add . |
Add all changed files to stage/index |
git restore . |
Remove all staged files from stage |
git add FILE |
Add file to stage/index |
git rm FILE --cached |
Remove files from stage/index |
git rm FILE |
Delete file |
git commit -m "MESSAGE" |
Commit changes |
git commit -m . |
Commit changes without message |
git commit --amend |
Edit/overwrite last commit |
git revert COMMIT_ID |
Undo by creating a new commit with reverted changes from previous commit |
git reset COMMIT_ID |
[Danger] Remove commits until selected one & make changes untracked Flags: –hard (force remove changes), –soft (put changes in stage) |
git checkout FILE |
[Danger] Restore file from last commit and remove uncommitted changes |
git mv old_FILE new_FILE |
Rename file |
git log |
Show history of commits |
git log -p |
Show history of commits & changes |
git log -p --author=userName |
Show history of commits & changes by specific user |
git log -p --since=2.days |
Show history of commits & changes in time duration (minutes, hours, weeks, etc.) |
git blame FILE |
Show when each line was last modified in file & who did it |
| Branch Operations | |
git branch |
List all local branches |
git branch -a |
Show all including remote branches |
git branch BRANCH_NAME |
Create new branch |
git branch NEW_BRANCH EXISTING_BRANCH |
Create a new branch from another (existing) branch |
git checkout -b NEW_BRANCH EXISTING_BRANCH |
Create and switch to a new branch based on another branch |
git checkout -b BRANCH_NAME REMOTE_NAME/REMOTE_BRANCH |
Create new branch from remote |
git cherry-pick COMMIT_ID |
Apply a specific commit from another branch to the current branch |
git merge --squash BRANCH_NAME |
Squash all commits from a branch into a single commit before merging |
git rebase -i HEAD~N |
Interactive rebase to squash last N commits (change ‘pick’ to ‘squash’/’s’) |
git branch -d BRANCH_NAME |
Delete branch |
git checkout BRANCH_NAME |
Move to branch |
git merge BRANCH_NAME |
Merge a branch to current branch (in new commit) |
git rebase BRANCH_NAME |
Merge a branch to current branch (editing commits chain of current branch) |
| Remote Operations | |
git clone REPO_URL |
Get copy of remote repo |
git remote add REPO_NAME REPO_URL |
Add remote repo |
git fetch REMOTE_NAME |
Fetch all remote branches and their history without merging |
git fetch REMOTE_NAME REMOTE_BRANCH_NAME |
Fetch specific remote branch |
git pull REMOTE_NAME BRANCH_NAME |
Fetch and merge (sync) remote changes into current local branch |
git pull REMOTE_REPO_NAME REMOTE_BRANCH_NAME |
Get remote repo latest files |
git push REMOTE_REPO_NAME REMOTE_BRANCH_NAME |
Update remote repo |
git remote |
Show all remote repos |
git push REMOTE_REPO_NAME --delete REMOTE_BRANCH_NAME |
Delete remote branch |



