This cheatsheet provides a comprehensive guide to Git and GitHub, ranging from beginner to advanced concepts. It includes practical examples and illustrations to help you understand and apply Git commands effectively.
- Beginner Level
- Intermediate Level
- Advanced Level
- Git Workflow Illustrations
- Practical Scenario: Contributing to an Open Source Project
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
-
Initialize a new Git repository:
mkdir my-project cd my-project git init
-
Clone a repository from GitHub:
git clone https://github.com/username/repo-name.git
-
Stage changes for commit:
git add file.txt git add . # Stage all changes
-
Commit staged changes:
git commit -m "Add initial project files"
-
Push commits to remote repository:
git push origin main
-
Fetch and merge changes from remote repository:
git pull origin main
- List branches:
git branch
- Create a new branch:
git branch <name>
- Switch to a branch:
git checkout <branch>
- Merge a branch into the current branch:
git merge <branch>
Example workflow:
git branch feature-login
git checkout feature-login
# Make changes
git add .
git commit -m "Implement login functionality"
git checkout main
git merge feature-login
-
Modify the last commit:
git commit --amend -m "Updated commit message"
-
Create a new commit that undoes specified commit:
git revert abc123 # Revert the commit with hash abc123
-
Reset to a specific commit, discarding all changes:
git reset --hard HEAD~1 # Reset to the commit before HEAD
- Temporarily store modified, tracked files:
git stash
- Apply stored stash content and remove it:
git stash pop
- List all stashed changesets:
git stash list
Example workflow:
# You're working on a feature but need to switch branches
git stash
git checkout other-branch
# Do some work
git checkout original-branch
git stash pop # Reapply your stashed changes
-
Add a new remote repository:
git remote add upstream https://github.com/original-owner/original-repository.git
-
Download objects and refs from another repository:
git fetch upstream
-
Fetch and rebase instead of merging:
git pull --rebase origin main
-
View commit history:
git log --oneline --graph --decorate
-
Show who last modified each line of a file:
git blame README.md
-
Reapply commits on top of another base:
git checkout feature-branch git rebase main
-
Interactive rebase for editing commits:
git rebase -i HEAD~3 # Interactively rebase the last 3 commits
-
Add a new submodule:
git submodule add https://github.com/username/library.git
-
Initialize and update submodules:
git clone https://github.com/username/project.git cd project git submodule update --init --recursive
Create scripts in .git/hooks/
directory to automate Git actions
Example (pre-commit hook):
#!/bin/sh
# .git/hooks/pre-commit
npm run lint
- Pull Requests: Propose changes and collaborate
- GitHub Actions: Automate workflows
- GitHub Pages: Host websites directly from a GitHub repository
- Objects: blobs, trees, commits, and tags
- Refs: pointers to commit objects
graph LR
A[Working Directory] -->|git add| B[Staging Area]
B -->|git commit| C[Local Repository]
C -->|git push| D[Remote Repository]
D -->|git pull| A
gitGraph
commit
branch feature
checkout feature
commit
commit
checkout main
merge feature
commit
gitGraph
commit
branch feature
commit
commit
checkout main
commit
checkout feature
commit
checkout main
merge feature
- Fork the repository on GitHub
- Clone your fork:
git clone https://github.com/your-username/project.git
- Set up upstream remote:
git remote add upstream https://github.com/original-owner/project.git
- Create a feature branch:
git checkout -b feature-name
- Make changes and commit:
git add . git commit -m "Implement new feature"
- Push to your fork:
git push origin feature-name
- Create a pull request on GitHub
- Update your pull request (if requested):
git add . git commit -m "Address review comments" git push origin feature-name
- Sync your fork with upstream:
git fetch upstream git checkout main git merge upstream/main git push origin main
This workflow demonstrates how to contribute to an open-source project using Git and GitHub, incorporating forking, branching, and pull requests.
© 2024 Md Shahed Fardous (Samy). All rights reserved.
Feel free to share this cheatsheet, but please provide attribution to the author.