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.
Common Commands
Initializing & Cloning
git init: Initialize a new repositorygit clone <url>: Clone an existing repositorygit clone <url> <dir>: Clone into a different directory orgit clone <url> .: Clone into the current directory
Copy Cloning Steps from Repo to New Repo
Using ExistingProject for the repo you want to clone from and NewProject for the name of your new repo.
- Create
NewProjectrepo at GitHub, GitLab, BitBucket, etc. =><NewProject url>a. do not create any files, READMEs, etc. Leave blank. - Clone from
ExistingProjectrepo intoNewProjectdirectory. Clone entire repo or just a branch
git clone <ExistingProject url> {NewProject DirName}- or
git clone --b <branchname> --single-branch <ExistingProject url> {NewProject DirName}
- Change into your
NewProjectdirectory
cd {NewProject DirName}
- Set your remote to
<SecondProject url>
git remote set-url origin <SecondProject url>
- Push it
git push
Branching/Pruning
git branch <branch-name>: Create a new branchgit checkout <branch-name>: Switch to a specific branchgit switch <branch-name>: (Modern alternative to git checkout)git remote prune origin: prunes tracking branches not on the origingit branch -D <branch-name>: removes local branch
Staging/Committing
git add <file>: Add specific file to staginggit add .: Add all changes to staginggit add -p: Stage hunks interactivelygit 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 Token
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 branch- add
--allow-unrelated-historiesto allow merging two branches that started their lives independently
- add
git 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 at origin
git push -d origin <branchname>
To delete a local branch
git branch -d <branchname>
Work Flows
Turn Changes into a Branch
Here are the git commands to turn changes into a branch: Check for uncommitted changes.
- Check Status
git status
- Create and switch to a new branch:
git checkout -b <new-branch-name>
- If you have staged changes with git add, they will be carried over to the new branch. Commit changes.
git add .
git commit -m "Commit message"
Alternatively, use stash
- Stash changes
git stash
- Create and switch to the new branch:
git checkout -b <new-branch-name>
- Apply stashed changes.
git stash pop
- Commit changes.
git add .
git commit -m "Commit message"
Git Clone into Existing Folder
Don't clone, fetch instead. In the repo:
git init
git remote add origin $url_of_clone_source
git fetch origin
git checkout -b main --track origin/main # origin/main or clone's default
Merge Two Repos
- Navigate to your local "Repo A" directory.
cd path/to/your/repoA
- Add "Repo B" as a new remote. This command names the remote repoB and specifies its URL. You can use an SSH or HTTPS URL, or a local file path.
git remote add repoB https://github.com
# or if local:
# git remote add repoB /path/to/local/repoB
- Fetch the branches and commits from the new remote. This makes the history of "Repo B" available locally.
git fetch repoB
- Checkout the target branch. Switch to the branch in "Repo A" where you want to incorporate the changes.
git checkout your-branch-in-repoA
- Merge the desired branch from "Repo B". You will likely need to use the --allow-unrelated-histories flag, especially if the repositories started independently, to override Git's safety check.
git merge --allow-unrelated-histories repoB/main # or repoB/master, or specific-branch-name
- Resolve any merge conflicts. If conflicts occur, you must open the affected files, manually resolve the differences, git add the resolved files, and then commit the changes.
- Push the changes to your origin. Once the merge is complete and conflicts are resolved, push the combined history to your remote repository for "Repo A".
git push origin your-branch-in-repoA
- Clean up (optional). After a successful merge, you can remove the temporary remote reference to "Repo B" if it is no longer needed.
git remote rm repoB
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.
- Origin: This is just the default name for a remote from which a repo was originally cloned.