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! π