Amr Ayman

Git Commands Cheat Sheet
🕑 3 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 checkout -b BRANCH_NAME REMOTE_NAME/REMOTE_BRANCH |
Create new branch from remote |
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 pull REMOTE_REPO_NAME REMOTE_BRANCH_NAME |
Get remote repo latest files |
git fetch REMOTE_REPO_NAME REMOTE_BRANCH_NAME |
Get remote repo changes history |
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 |
MORE Articles
Share
Rate This



