Revision+CheatSheet+Git+&+GitHub+Commands+
Revision+CheatSheet+Git+&+GitHub+Commands+
org
Make Changes
git status
● Use: Shows the status of changes as untracked, modified, or staged.
● When: Before and after making changes.
● Example: git status
● Explanation: Helps you understand what's happening in your repository.
git add <filename>
● Use: Adds specific files to the staging area.
● When: After making changes you wish to commit.
● Example: git add README.md
● Explanation: This command stages the changes you made to README.md.
git add . or git add -A
● Use: Adds all changed files to the staging area.
● When: When you have multiple files to stage.
● Example: git add .
● Explanation: Stages all modified and new (untracked) files.
git commit -m "Commit message"
● Use: Records staged changes in the repository.
● When: After staging changes.
● Example: git commit -m "Initial commit"
● Explanation: Saves your changes with a descriptive message.
Branching
git branch
● Use: Lists all local branches in the repository.
● When: To check or manage branches.
● Example: git branch
● Explanation: Displays all branches and highlights the current branch.
git branch <branchname>
● Use: Creates a new branch.
● When: When starting a new feature or fix.
● Example: git branch feature-x
● Explanation: Creates a new branch named feature-x.
git checkout <branchname>
● Use: Switches to another branch.
● When: To work on a different branch.
● Example: git checkout feature-x
● Explanation: Switches your working directory to the feature-x branch.
git merge <branchname>
● Use: Merges changes from one branch to another.
● When: To integrate changes from one branch to another (like master).
● Example: git merge feature-x
● Explanation: Merges changes from feature-x into the current branch.
Remote Repositories
git push origin <branchname>
● Use: Sends your commits to the remote repository.
● When: After committing your changes locally and ready to share.
● Example: git push origin master
● Explanation: Uploads your commits on the master branch to the remote
repository.
git pull
● Use: Fetches and merges changes from the remote repository to your
local repo.
● When: To update your local repository with changes from the remote.
● Example: git pull
● Explanation: Downloads changes and merges them into your local branch.
git remote -v
● Use: Lists all remote connections to other repositories.
● When: To view remote repositories.
● Example: git remote -v
● Explanation: Shows the URLs that Git has stored for the push and fetch
operations.
Key Differences
git fetch vs git pull
● git fetch downloads data from a remote repository but doesn't integrate
it into your working files. git pull does both: fetches and then merges.
git merge vs git rebase
● git merge combines the source and target branches via a new commit.
git rebase rewrites the project history by creating brand new commits for
each commit in the original branch.
git reset vs git revert
● git reset is used to undo local changes to the state of a Git repo, while
git revert is used to undo changes that are already committed to a
public repository. git revert keeps the project history, whereas git
reset can alter it.
Further Resources
This cheat sheet provides the foundational knowledge for Git usage. As you explore
more, you'll discover additional commands and nuances of Git.