Top Git Commands with Examples
1. git init
o Initializes a new Git repository.
o Example: git init
2. git clone [repository-url]
o Clones a repository from a URL.
o Example: git clone
https://github.com/user/repo.git
3. git status
o Shows the working directory status.
o Example: git status
4. git add [file]
o Stages a file for commit.
o Example: git add file.txt
5. git commit -m "[message]"
o Commits staged changes with a message.
o Example: git commit -m "Initial commit"
6. git log
o Shows the commit history.
o Example: git log
7. git diff
o Shows differences between working directory
and index.
o Example: git diff
8. git branch
o Lists all branches.
o Example: git branch
9. git branch [branch-name]
o Creates a new branch.
o Example: git branch feature-branch
10. git checkout [branch-name]
o Switches to a different branch.
o Example: git checkout feature-branch
11. git merge [branch-name]
o Merges a branch into the current branch.
o Example: git merge feature-branch
12. git pull
o Fetches and merges changes from the remote
repository.
o Example: git pull origin main
13. git push
o Pushes changes to the remote repository.
o Example: git push origin main
14. git remote -v
o Lists remote repositories.
o Example: git remote -v
15. git fetch
o Fetches changes from the remote repository.
o Example: git fetch origin
16. git reset [file]
o Unstages a file.
o Example: git reset file.txt
17. git rm [file]
o Removes a file from the working directory and
staging area.
o Example: git rm file.txt
18. git stash
o Stashes changes in a dirty working directory.
o Example: git stash
19. git stash apply
o Applies stashed changes.
o Example: git stash apply
20. git tag [tag-name]
o Creates a new tag.
o Example: git tag v1.0
21. git tag -d [tag-name]
o Deletes a tag.
o Example: git tag -d v1.0
22. git rebase [branch-name]
o Re-applies commits on top of another base
tip.
o Example: git rebase feature-branch
23. git cherry-pick [commit-hash]
o Applies the changes from a specific commit.
o Example: git cherry-pick a1b2c3d
24. git log --oneline
o Shows the commit history in a simplified
format.
o Example: git log --oneline
25. git log --graph
o Displays a graphical representation of the
commit history.
o Example: git log --graph
26. git log --pretty=format:"%h - %an, %ar : %s"
o Customizes the format of log output.
o Example: git log --pretty=format:"%h - %an,
%ar : %s"
27. git diff --staged
o Shows differences between staged changes
and the last commit.
o Example: git diff --staged
28. git diff [branch1] [branch2]
o Shows differences between two branches.
o Example: git diff main feature-branch
29. git config --global user.name "[name]"
o Sets the Git username.
o Example: git config --global user.name "John
Doe"
30. git config --global user.email "[email]"
o Sets the Git email address.
o Example: git config --global user.email
"john.doe@example.com"