Comprehensive Git Commands Guide
A handy reference for commonly used Git commands with concise explanations.
Configuration Commands
1. Set Username
git config --global [Link]"Your Name"
Sets your name globally to identify your commits.
2. Set Email
git config --global [Link] "[Link]@[Link]"
Sets your email globally for tagging commits.
3. Check Configuration
git config --list
Displays the current Git configuration (user, email, etc.).
Repository Commands
4. Initialize a Repository
git init
Creates a new Git repository in the current directory.
5. Clone a Repository
git clone <repository-url>
Downloads a repository from a remote server and creates a local copy.
Staging Commands
6. Add Specific File
git add <file>
Stages a specific file for the next commit.
7. Add All Changes
git add .
Stages all new, modified, and deleted files in the current directory.
Status and Logs
8. Check Repository Status
git status
Displays the state of the working directory and staging area.
9. View Commit History
git log
Shows a list of previous commits.
10. View Simplified Commit Log
git log --oneline
Displays commit history in a compact format (one commit per line).
Branching and Switching
11. List Branches
git branch
Lists all branches in the repository and highlights the current branch.
12. Create a New Branch
git branch <branch-name>
Creates a new branch with the given name.
13. Switch to a Branch
git checkout <branch-name>
Switches to the specified branch.
14. Create and Switch to a New Branch
git checkout -b <branch-name>
Creates a new branch and immediately switches to it.
Commit Commands
15. Commit Changes
git commit -m "Your commit message"
Creates a commit with a message describing the changes.
16. Amend the Last Commit
git commit --amend -m "Updated commit message"
Edits the last commit (message or staged files).
Remote Repositories
17. Add a Remote Repository
git remote add origin <repository-url>
Links your local repository to a remote server.
18. View Remote Repositories
git remote -v
Lists the remote repositories linked to your project.
19. Push Changes to Remote
git push origin <branch-name>
Sends commits from your local branch to the corresponding remote branch.
20. Pull Changes from Remote
git pull origin <branch-name>
Fetches updates from the remote branch and merges them into your current branch.
Miscellaneous
21. Discard Unstaged Changes
git checkout -- <file>
Reverts unstaged changes to the last committed state.
22. Remove Staged File
git reset <file>
Unstages a file without deleting its changes.
23. Reset to a Previous Commit
git reset --hard <commit-hash>
Resets the repository to a specific commit, discarding all changes after it.
24. Show Changes
git diff
Displays differences between the working directory and the staging area.