Git Common Commands
Here's a list of the most commonly used Git commands when working on projects:
Git Commands
Basic Git Workflow
git init- Initialize a new Git repositorygit clone <repository-url>- Clone a repository from remotegit status- Check status of working directorygit add <file>- Add specific file to staginggit add .- Add all changed files to staginggit commit -m "message"- Commit staged changes with a messagegit push- Push commits to remote repositorygit pull- Fetch and merge changes from remote repository
Branches
git branch- List all local branchesgit branch -a- List all branches (local and remote)git branch <branch-name>- Create a new branchgit checkout <branch-name>- Switch to a branchgit checkout -b <branch-name>- Create and switch to a new branchgit merge <branch-name>- Merge specified branch into current branchgit branch -d <branch-name>- Delete a local branchgit push origin --delete <branch-name>- Delete a remote branch
Undoing Changes
git reset <file>- Unstage a file while keeping changesgit reset --soft HEAD~1- Undo last commit but keep changes stagedgit reset --hard HEAD~1- Remove last commit and discard changesgit checkout -- <file>- Discard changes to a filegit revert <commit-hash>- Create a new commit that undoes specified commitgit clean -fd- Remove untracked files and directories
History & Logs
git log- View commit historygit log --oneline- View simplified commit historygit blame <file>- Show who changed what and when in a filegit diff- Show changes between working directory and last commit