Git Cheatsheet
#
Git is a distributed version control system created by Linus Torvalds in 2005 to manage the development of the Linux kernel. It is designed for speed, efficiency, and support for distributed, non-linear workflows. Git has become the most widely used version control system in the world, known for its branching and merging capabilities.
Repository Management
#
Command/Option | Example | Description |
---|
git init | git init | Initialize a new Git repository |
git clone | git clone https://github.com/user/repo.git | Clone an existing repository |
Staging and Committing
#
Command/Option | Example | Description |
---|
git add | git add file.txt | Add file contents to the index |
git commit | git commit -m "Commit message" | Record changes to the repository |
git status | git status | Show the working tree status |
git diff | git diff | Show changes between commits, commit and working tree, etc. |
Branching and Merging
#
Command/Option | Example | Description |
---|
git branch | git branch new-branch | List, create, or delete branches |
git checkout | git checkout new-branch | Switch branches or restore working tree files |
git merge | git merge new-branch | Join two or more development histories together |
git rebase | git rebase master | Reapply commits on top of another base tip |
Remote Repositories
#
Command/Option | Example | Description |
---|
git fetch | git fetch origin | Download objects and refs from another repository |
git pull | git pull origin master | Fetch from and integrate with another repository or a local branch |
git push | git push origin master | Update remote refs along with associated objects |
git remote | git remote add origin https://github.com/user/repo.git | Manage set of tracked repositories |
Undoing Changes
#
Command/Option | Example | Description |
---|
git reset | git reset --hard HEAD~1 | Reset current HEAD to the specified state |
git rm | git rm file.txt | Remove files from the working tree and the index |
git stash | git stash | Stash the changes in a dirty working directory away |
Viewing History and Logs
#
Command/Option | Example | Description |
---|
git log | git log | Show commit logs |
git show | git show HEAD | Show various types of objects |
git reflog | git reflog | Show history of commands associated with the reference |
Advanced Commands
#
Command/Option | Example | Description |
---|
git tag | git tag -a v1.0 -m "Version 1.0" | Create, list, delete, or verify a tag object signed with GPG |
git archive | git archive -o archive.zip HEAD | Create an archive of files from a named tree |
git bisect | git bisect start | Use binary search to find the commit that introduced a bug |
git cherry-pick | git cherry-pick commitSHA | Apply the changes introduced by some existing commits |
This cheatsheet covers the most commonly used Git commands and options, helping you to manage repositories, commits, branches, merges, and more effectively.