Git Commands Cheatsheet: Your Ultimate Guide
Git is an essential tool for developers, enabling efficient version control and collaboration. Whether you’re a beginner or an experienced developer, having a handy cheatsheet can save you time and streamline your workflow. Here’s a comprehensive list of Git commands that you can refer to whenever you need.
Basic Commands
1. Initialize a Repository
git init
Creates a new Git repository.
2. Clone a Repository
git clone <repository-url>
Copies an existing Git repository.
3. Check Repository Status
git status
Displays the state of the working directory and staging area.
Working with Branches
4. Create a New Branch
git branch <branch-name>
Creates a new branch.
5. Switch to a Branch
git checkout <branch-name>
Switches to the specified branch.
6. Create and Switch to a New Branch
git checkout -b <branch-name>
Creates and switches to a new branch.
7. List All Branches
git branch
Lists all branches in the repository.
Making Changes
8. Add Changes to Staging Area
git add <file-name>
Adds a file to the staging area.
9. Commit Changes
git commit -m "commit message"
Records changes to the repository with a message.
10. Push Changes to Remote Repository
git push origin <branch-name>
Uploads local branch commits to the remote repository.
Merging and Rebasing
11. Merge Branches
git merge <branch-name>
Merges the specified branch into the current branch.
12. Rebase Branch
git rebase <branch-name>
Reapplies commits on top of another base tip.
Viewing History
13. View Commit History
git log
Displays the commit history.
14. View a Specific Commit
git show <commit-hash>
Shows changes made in a specific commit.
Undoing Changes
15. Unstage a File
git reset <file-name>
Removes a file from the staging area.
16. Revert a Commit
git revert <commit-hash>
Creates a new commit that undoes changes made by a previous commit.
17. Reset to a Previous Commit
git reset --hard <commit-hash>
Resets the repository to a specific commit, discarding all changes after it.
Working with Remotes
18. Add a Remote Repository
git remote add <name> <url>
Adds a new remote repository.
19. Fetch Changes from Remote
git fetch <remote-name>
Downloads objects and refs from another repository.
20. Pull Changes from Remote
git pull <remote-name> <branch-name>
Fetches and integrates changes from a remote repository.
Conclusion
This cheatsheet covers the most commonly used Git commands, but Git is a powerful tool with many more capabilities. Keep exploring and practicing to become more proficient.
If as a beginner, you want to understand, how can we upload our repository in Github with basic commands, watch this video.