Master Git and GitHub with this comprehensive cheat sheet. From basics to advanced tricks, this guide has everything you need to manage and collaborate on code like a pro! 🚀
git status
🛑 View modified, untracked, or staged files in your working directory.
git init
📂 Turn a project directory into a Git-tracked repository.
git clone <repository_url>
📥 Copy a remote repository to your local machine.
git add <file_name> # Add a specific file
git add . # Add everything
📂 Prepare changes for commit.
git commit -m "Your message"
💾 Save staged changes with a descriptive message.
git reset <file_name>
🚧 Remove files from staging without discarding changes.
git checkout -b <branch_name>
🌿 Create and move to a new branch in one step.
git merge <branch_name>
🔀 Combine changes from another branch into your current branch.
git branch -d <branch_name>
🗑️ Remove a branch you no longer need.
git push origin <branch_name>
🌐 Upload your local branch to the remote repository.
git pull origin <branch_name>
📥 Fetch and merge changes from the remote branch.
git rebase <branch_name>
🪄 Move your branch to start from the latest changes in another branch.
Example Workflow:
- Switch to the feature branch:
git checkout feature-branch
- Rebase onto main:
git rebase main
✨ This creates a cleaner commit history by replaying your commits on top of the target branch.
git rebase -i HEAD~<number_of_commits>
📦 Combine multiple commits into one to simplify history.
Example Workflow:
- Use
git rebase -i HEAD~3
to squash the last 3 commits. - Mark commits as
squash
ors
in the interactive editor. - Save and exit to merge the commits.
git cherry-pick <commit_hash>
🍒 Apply a specific commit from one branch to another.
Example Workflow:
- Find the commit hash using
git log
. - Apply it to your branch:
git cherry-pick abc1234
.
git stash
🧳 Save your changes for later without committing.
Retrieve Stashed Changes:
git stash pop
🪄 Apply and remove the latest stash.
git commit --amend -m "Updated commit message"
✏️ Edit the last commit message or add files you forgot to include.
git log
git log --oneline
📜 View detailed or one-line commit history.
git diff <file_name>
🔍 Compare changes in a specific file.
git bisect start
git bisect bad
git bisect good <commit_hash>
🐞 Automates binary search to pinpoint the bad commit.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
📇 Identify yourself for all repositories.
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
⚡ Create shortcuts for frequent commands.
-
Clean Up Old Branches Locally and Remotely
git branch -d <branch_name> # Delete local branch git push origin --delete <branch_name> # Delete remote branch
-
Force Pull (Override Local Changes)
git fetch origin git reset --hard origin/<branch_name>
⚠️ Warning: This overwrites your local changes. -
Track a Remote Branch Locally
git checkout --track origin/<branch_name>
👣 Follows a remote branch for easy pulls and pushes.
-
Create a branch for new features:
git checkout -b feature/new-feature
-
Commit your work:
git add . git commit -m "Add new feature"
-
Push to GitHub:
git push origin feature/new-feature
-
Open a pull request on GitHub to merge into
main
ordevelop
.
You’re now equipped with beginner and advanced Git skills. Start applying them to your projects, squash your bugs, and keep your commit history neat and clean. 🐙✨
Got feedback or more advanced topics you'd like to cover? Let me know! 😊