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
- Getting Started with Git
- Basic Git Commands
- Branching and Merging
- Working with Remote Repositories
- Viewing History and Tracking Changes
- Advanced Git Commands
- Best Practices & Pro Tips
π Getting Started with Git
β Install Git
Download and install Git from git-scm.com, then verify the installation:
git --version
π― Initialize a Repository
git init
Creates a new Git repository in the current directory.
π₯ Clone an Existing Repository
git clone <repository-url>
Copies a remote repository to your local machine.
π₯ Basic Git Commands
π§ Check Repository Status
git status
Shows the current state of your working directory.
π Stage Changes
git add <file>
git add .
Adds files to the staging area (.
stages all changes).
β Commit Changes
git commit -m "Descriptive commit message"
Saves the staged changes to your local repository.
π οΈ Modify Last Commit
git commit --amend -m "Updated commit message"
Allows you to edit the last commit (only use before pushing!).
πΏ Branching and Merging
π± Create a New Branch
git branch <branch-name>
Creates a new branch.
π Switch Branches
git checkout <branch-name>
Switches to the specified branch. Alternatively, use:
git switch <branch-name>
π Create and Switch in One Step
git checkout -b <branch-name>
Creates and switches to a new branch.
π Merge Branches
git merge <branch-name>
Integrates changes from the specified branch into the current branch.
ποΈ Delete a Branch
git branch -d <branch-name>
Deletes a branch that has been merged. Use -D
to force delete.
π Working with Remote Repositories
π View Remote Repositories
git remote -v
Lists the remote repositories linked to your project.
π Add a Remote Repository
git remote add origin <repository-url>
Links a local repo to a remote server.
π Fetch Updates
git fetch origin
Retrieves changes from the remote without merging.
β¬οΈ Pull Changes
git pull origin <branch-name>
Updates your local branch with remote changes.
β¬οΈ Push Changes
git push origin <branch-name>
Uploads your commits to the remote repository.
π Viewing History and Tracking Changes
π View Commit History
git log
Displays the commit history. Use:
git log --oneline --graph --decorate
for a compact view.
π Show Specific Commit Details
git show <commit-hash>
Displays details of a particular commit.
π¬ Compare Changes
git diff
Shows differences between working directory and staged files.
π Stash Changes Temporarily
git stash
git stash pop
Saves unfinished work without committing and restores it later.
π οΈ Advanced Git Commands
π§Ή Rebase (Reapply Commits)
git rebase <branch-name>
Reapplies commits on top of another base.
π Reset to a Previous Commit
git reset --soft <commit-hash>
git reset --hard <commit-hash>
--soft
keeps changes staged, --hard
erases them.
βͺ Revert a Commit
git revert <commit-hash>
Creates a new commit that undoes a previous commit.
π Cherry-Pick Specific Commits
git cherry-pick <commit-hash>
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)