DEV Community

Suman Bhattarai
Suman Bhattarai

Posted on

๐Ÿš€ Ultimate Git Cheatsheet: Your Go-To Guide for Version Control

Git is an essential tool for developers, enabling seamless version control and collaboration. Whether you're a beginner or a seasoned pro, this Git cheatsheet will help you navigate through essential commands with ease! ๐Ÿ’ก

๐Ÿ“Œ Table of Contents

  1. Getting Started with Git
  2. Basic Git Commands
  3. Branching and Merging
  4. Working with Remote Repositories
  5. Viewing History and Tracking Changes
  6. Advanced Git Commands
  7. Best Practices & Pro Tips

๐Ÿ Getting Started with Git

โœ… Install Git

Download and install Git from git-scm.com, then verify the installation:

git --version
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Initialize a Repository

git init
Enter fullscreen mode Exit fullscreen mode

Creates a new Git repository in the current directory.

๐Ÿ“ฅ Clone an Existing Repository

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

Copies a remote repository to your local machine.


๐Ÿ”ฅ Basic Git Commands

๐Ÿง Check Repository Status

git status
Enter fullscreen mode Exit fullscreen mode

Shows the current state of your working directory.

๐Ÿ“Œ Stage Changes

git add <file>
git add .
Enter fullscreen mode Exit fullscreen mode

Adds files to the staging area (. stages all changes).

โœ… Commit Changes

git commit -m "Descriptive commit message"
Enter fullscreen mode Exit fullscreen mode

Saves the staged changes to your local repository.

๐Ÿ› ๏ธ Modify Last Commit

git commit --amend -m "Updated commit message"
Enter fullscreen mode Exit fullscreen mode

Allows you to edit the last commit (only use before pushing!).


๐ŸŒฟ Branching and Merging

๐ŸŒฑ Create a New Branch

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

Creates a new branch.

๐Ÿ”„ Switch Branches

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Switches to the specified branch. Alternatively, use:

git switch <branch-name>
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Create and Switch in One Step

git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode

Creates and switches to a new branch.

๐Ÿ”— Merge Branches

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

Integrates changes from the specified branch into the current branch.

๐Ÿ—‘๏ธ Delete a Branch

git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

Deletes a branch that has been merged. Use -D to force delete.


๐ŸŒŽ Working with Remote Repositories

๐Ÿ” View Remote Repositories

git remote -v
Enter fullscreen mode Exit fullscreen mode

Lists the remote repositories linked to your project.

๐Ÿ”— Add a Remote Repository

git remote add origin <repository-url>
Enter fullscreen mode Exit fullscreen mode

Links a local repo to a remote server.

๐Ÿ”„ Fetch Updates

git fetch origin
Enter fullscreen mode Exit fullscreen mode

Retrieves changes from the remote without merging.

โฌ‡๏ธ Pull Changes

git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Updates your local branch with remote changes.

โฌ†๏ธ Push Changes

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Uploads your commits to the remote repository.


๐Ÿ“œ Viewing History and Tracking Changes

๐Ÿ“š View Commit History

git log
Enter fullscreen mode Exit fullscreen mode

Displays the commit history. Use:

git log --oneline --graph --decorate
Enter fullscreen mode Exit fullscreen mode

for a compact view.

๐Ÿ” Show Specific Commit Details

git show <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Displays details of a particular commit.

๐Ÿ”ฌ Compare Changes

git diff
Enter fullscreen mode Exit fullscreen mode

Shows differences between working directory and staged files.

๐Ÿ“Œ Stash Changes Temporarily

git stash
git stash pop
Enter fullscreen mode Exit fullscreen mode

Saves unfinished work without committing and restores it later.


๐Ÿ› ๏ธ Advanced Git Commands

๐Ÿงน Rebase (Reapply Commits)

git rebase <branch-name>
Enter fullscreen mode Exit fullscreen mode

Reapplies commits on top of another base.

๐Ÿ”„ Reset to a Previous Commit

git reset --soft <commit-hash>
git reset --hard <commit-hash>
Enter fullscreen mode Exit fullscreen mode

--soft keeps changes staged, --hard erases them.

โช Revert a Commit

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Creates a new commit that undoes a previous commit.

๐Ÿ’ Cherry-Pick Specific Commits

git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Applies a specific commit from another branch.


๐ŸŽฏ Best Practices & Pro Tips

โœ… Commit Often & Use Meaningful Messages ๐Ÿ“Œ

โœ… Use Feature Branches for New Changes ๐ŸŒฟ

โœ… Regularly Sync with Remote (git pull) ๐Ÿ”„

โœ… Resolve Merge Conflicts Carefully ๐Ÿค

โœ… Keep Your Repository Clean & Organized ๐Ÿงน


๐Ÿ Conclusion

Mastering Git is essential for efficient software development. This Git cheatsheet serves as a quick reference for everyday commands and best practices. Keep practicing, experiment with different commands, and soon you'll be a Git expert! ๐Ÿš€

Happy coding! ๐Ÿ‘จโ€๐Ÿ’ปโœจ

Top comments (0)