Git Cheat Sheet
Git Commands Cheat Sheet
Git is a distributed version control system that helps developers track and manage changes to their codebase. It enables multiple contributors to work on a project simultaneously without interfering with each other's progress.
Git tracks the history of changes, allowing developers to revert to previous versions if needed and to understand the evolution of their project over time. It's an essential tool for modern software development, facilitating collaboration, code versioning, and maintaining the integrity of a project's history.
Key Git Terms
- Repository (Repo): A storage space where your project lives. It can be local to a folder on your computer, or it can be a storage space on GitHub or another online host.
- Commit: An individual change to a file (or set of files). It's like taking a snapshot of your project at a specific point in time.
- Branch: A parallel version of a repository. It diverges from the main project to prevent disrupting the main line of development.
- Merge: Taking the changes from one branch (in the same repository or from a fork) and applying them into another.
- Clone: A copy of a repository that resides on your computer instead of on a website's server.
- Fork: A personal copy of someone else's project. Forking a repository allows you to freely experiment with changes without affecting the original project.
- Pull Request (PR): A method of submitting contributions to an open source project. It's a request to the project owner to pull your changes into their repository.
- Push: Sending your committed changes to a remote repository on GitHub or another online host.
- Pull: Fetching and merging changes from a remote repository to your local repository.
- Remote: This is the version of a repository or branch that is hosted on a server, most likely on GitHub. It's where you push your changes when you are done working locally.
Common Commands
Initializing & Cloning
git init
: Initialize a new repositorygit clone <url>
: Clone an existing repository
Branching
git branch <branch-name>
: Create a new branchgit checkout <branch-name>
: Switch to a specific branchgit switch <branch-name>
: (Modern alternative to git checkout)
Staging
git add <file>
: Add specific file to staginggit add .
: Add all changes to staginggit add -p
: Stage hunks interactively
Committing
git commit -m "message"
: Commit with a messagegit commit -am "message"
: Add all tracked changes to staging and commit
Remote Operations
git remote add origin <url>
: Add a remote repositorygit pull origin <branch>
: Pull changes from remotegit push origin <branch>
: Push changes to remote
Adding Remote with HTTPS and Token (GitHub)
Typically, to work with Git and GitHub you need to set up SSH keys. However, if you don't want to set up SSH keys, you can use HTTPS and a personal access token instead. Here's how:
- Generate a personal access token on GitHub.
- When adding the remote replace
[TOKEN]
with your GitHub token,[USERNAME]
with your GitHub username and[REPOSITORY]
with the repository name
git remote add origin https://[TOKEN]@github.com/[USERNAME]/[REPOSITORY]
git push origin main
Miscellaneous
git status
: Check the status of changesgit log
: View commit historygit revert <commit-hash>
: Revert to a previous commit
Sync & Merge
git merge <branch>
: Merge another branch into your active branchgit fetch
: Fetch latest changes from remote (doesn't merge)git pull --rebase
: Fetch and rebase local commits on top
Stashing
git stash
: Stash changesgit stash pop
: Apply stashed changes
Delete Branch
To delete the branch locally and remote use the following two commands.
git push -d <remote_name> <branchname>
git branch -d <branchname>
Note: In most cases, <remote_name> will be origin.