Git 7 🧩 Git Cheatsheet
7. Git Cheatsheet
a. Part 1: Basic & Everyday Commands
| Command | Description |
|---|---|
git init | Initializes a new local Git repository. |
git clone [url] | Creates a local copy of a remote repository. |
git status | Shows the status of changes as untracked, modified, or staged. |
git add . | Stages all changes in the current directory for the next commit. |
git add [file] | Stages a specific file for the next commit. |
git commit -m "[message]" | Records staged changes to the repository with a descriptive message. |
git push | Pushes committed changes to a remote repository. |
git pull | Fetches and merges changes from the remote repository. |
b. Part 2: Branching & Merging
| Command | Description |
|---|---|
git branch | Lists all local branches. |
git branch [name] | Creates a new branch. |
git checkout [name] | Switches to a different branch. |
git checkout -b [name] | Creates and switches to a new branch. |
git merge [branch] | Merges the specified branch’s history into the current branch. |
git branch -d [name] | Deletes a local branch. |
git fetch | Downloads objects and refs from another repository. It does not merge changes, leaving them for you to inspect. |
git rebase [target] | Reapplies commits from the current branch on top of another branch, creating a cleaner, linear history. |
c. Part 3: Undoing & Rewriting History
| Command | Description |
|---|---|
git reset [file] | Unstages a file without changing its content. |
git reset --hard | Discards all changes in the working directory and staging area, and moves HEAD to the specified commit. Caution: This is destructive. |
git checkout -- [file] | Discards changes in a working directory. Caution: This is destructive. |
git revert [commit] | Creates a new commit that undoes the changes from a previous commit. This is a safer way to “undo” a commit as it doesn’t rewrite history. |
git commit --amend | Amends the last commit, useful for adding forgotten files or correcting the commit message. |
git rebase -i [commit] | Opens an interactive rebase editor, allowing you to squash, reorder, or drop commits. Caution: Do not use this on public branches. |
d. Part 4: Remote Repositories & Collaboration
| Command | Description |
|---|---|
git remote -v | Lists the remote repositories you are connected to. |
git remote add [name] [url] | Adds a new remote repository. |
git remote rm [name] | Removes a remote repository. |
git fetch [remote] | Fetches all branches from the remote repository. |
git pull --rebase | Pulls changes from the remote and rebases them on top of your local commits, creating a cleaner history. |
e. Part 5: Tagging & Inspection
| Command | Description |
|---|---|
git log | Shows the commit history. |
git log --oneline | Shows a condensed, one-line view of the commit history. |
git show [commit] | Shows the changes for a specific commit. |
git diff | Shows changes in the working directory that are not yet staged. |
git diff --staged | Shows changes in the staging area that are not yet committed. |
git tag [name] | Creates a lightweight tag at the current commit. |
git tag -a [name] -m "[message]" | Creates an annotated tag with a message. |