GIT Commands
By: zigmoid
Posted on: 05/13/2014
Here’s a list of essential Git commands you’ll need as a developer:
Basic Git Commands
- Initialize a Git RepositoryshCopyEdit
git init
- Clone a RepositoryshCopyEdit
git clone <repo_url>
- Check StatusshCopyEdit
git status
- Add Files to Staging AreashCopyEdit
git add <file> # Add a specific file git add . # Add all changed files
- Commit ChangesshCopyEdit
git commit -m "Your commit message"
- View Commit HistoryshCopyEdit
git log
Branching & Merging
- Create a New BranchshCopyEdit
git branch <branch_name>
- Switch to a BranchshCopyEdit
git checkout <branch_name>
- Create & Switch to a New BranchshCopyEdit
git checkout -b <branch_name>
- Merge a BranchshCopyEdit
git merge <branch_name>
- Delete a BranchshCopyEdit
git branch -d <branch_name> # Delete local branch git push origin --delete <branch_name> # Delete remote branch
Working with Remote Repositories
- Check Remote URLsshCopyEdit
git remote -v
- Add a Remote RepositoryshCopyEdit
git remote add origin <repo_url>
- Push Changes to Remote RepositoryshCopyEdit
git push origin <branch_name>
- Pull Changes from Remote RepositoryshCopyEdit
git pull origin <branch_name>
- Fetch Remote Changes (Without Merging)shCopyEdit
git fetch origin
Undo & Reset
- Undo Last Commit (Keep Changes in Staging)shCopyEdit
git reset --soft HEAD~1
- Undo Last Commit (Discard Changes)shCopyEdit
git reset --hard HEAD~1
- Revert a Commit (Create a New Commit to Undo)shCopyEdit
git revert <commit_hash>
- Discard Changes in a FileshCopyEdit
git checkout -- <file>
- Remove a File from Staging AreashCopyEdit
git reset <file>
Stashing Changes
- Stash Current ChangesshCopyEdit
git stash
- Apply Stashed ChangesshCopyEdit
git stash pop
- List Stashed ChangesshCopyEdit
git stash list
- Drop a StashshCopyEdit
git stash drop
Advanced Commands
- Rebase a BranchshCopyEdit
git rebase <branch_name>
- Squash Commits (Interactive Rebase)shCopyEdit
git rebase -i HEAD~<number_of_commits>
- Cherry-Pick a CommitshCopyEdit
git cherry-pick <commit_hash>
- Check Differences Between CommitsshCopyEdit
git diff <commit1> <commit2>
- Tag a CommitshCopyEdit
git tag <tag_name>
- Push Tags to RemoteshCopyEdit
git push origin --tags
Cleaning Up
- Remove Untracked FilesshCopyEdit
git clean -f
- Delete All Local Branches Except
main
/master
shCopyEditgit branch | grep -v "main" | xargs git branch -D
This should cover most of your Git needs! 🚀 Let me know if you need more details on any command.