Git is a powerful distributed version control system used for tracking changes in source code during software development. Whether you're new to Git or looking for a quick reference, this cheat sheet provides essential Git commands and concepts.
Table of Contents
- Configuring Git
- Creating and Cloning Repositories
- Basic Git Workflow
- Branching and Merging
- Checking Status and History
- Staging and Committing
- Undoing Changes
- Collaboration and Remote Repositories
- Advanced Git
Configuring Git
Configure User Information
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Set Default Text Editor
git config --global core.editor "nano" # Set your preferred text editor
Creating and Cloning Repositories
Initialize a New Repository
git init
Clone a Repository
git clone repository_url
Basic Git Workflow
Check Repository Status
git status
Update Local Repository
git pull origin branch_name
Push Changes to Remote
git push origin branch_name
Branching and Merging
Create a New Branch
git checkout -b new_branch
Switch Branches
git checkout branch_name
Merge Branch into Current Branch
git merge branch_name
Delete a Branch
git branch -d branch_name
Checking Status and History
View Changes in Files
git diff
View Commit History
git log
View Remote Branches
git remote -v
Staging and Committing
Stage Changes for Commit
git add filename
Commit Changes
git commit -m "Descriptive message"
Undoing Changes
Discard Changes in Working Directory
git checkout -- filename
Unstage Changes
git reset filename
Undo the Last Commit (Keep Changes)
git reset HEAD~1
Undo the Last Commit (Discard Changes)
git reset --hard HEAD~1
Collaboration and Remote Repositories
Add a Remote Repository
git remote add remote_name repository_url
Fetch Changes from a Remote Repository
git fetch remote_name
Create a Pull Request
After pushing your branch to a remote repository, create a pull request via the repository's web interface.
Review and Merge Pull Request
Review and merge pull requests on the repository's web interface.
Advanced Git
Rebase Commits
git rebase branch_name
Resolve Merge Conflicts
Edit conflicted files, then commit changes.
Tag a Commit
git tag tag_name
Cherry-Pick Commits
git cherry-pick commit_hash
Amend the Last Commit
git commit --amend
This Git cheatsheet covers essential Git commands and concepts for everyday use. Refer to it as you work with Git to manage version control in your software development projects.
Estimated Reading Time: 5 minutes
Top comments (0)