1. Git Configuration
| Command | Description | Example |
|---|
git config --global user.name "Your Name" | Set username globally | git config --global user.name "Pritesh Thamke" |
git config --global user.email "you@example.com" | Set email globally | git config --global user.email "pritesh@example.com" |
git config --list | View all config settings | git config --list |
git config --global core.editor nano | Set default editor | git config --global core.editor nano |
| Command Name | Description | Example |
|---|
git init | Initializes a new Git repository in the current directory. | git init Creates a .git folder in the current directory to start version control. |
git config --global user.name "<name>" | Sets the global username for commits. | git config --global user.name "Pritesh thamke" Configures the username as “John Doe” for all repositories. |
git config --global user.email "<email>" | Sets the global email for commits. | git config --global user.email "pritesh.cotocus@gmail.com" Sets the email for commit authorship. |
git config --list | Lists all Git configuration settings. | git config --list Displays all global and local config settings, e.g., user.name, user.email. |
git config --global core.editor "<editor>" | Sets the default text editor for Git (e.g., for commit messages). | git config --global core.editor "vim" Sets Vim as the default editor. |
git config --global color.ui auto | Enables colored output for Git commands. | git config --global color.ui auto Makes Git output (like status, diff) colorful. |
2. Git Repository Initialization
| Command | Description | Example |
|---|
git init | Initialize a new Git repo | git init |
git clone <url> | Clone remote repo | git clone https://github.com/user/repo.git |
| Command Name | Description | Example |
|---|
git clone <repo-url> | Clones a remote repository to your local machine. | git clone https://github.com/user/repo.git Downloads the repository to a local folder named repo. |
git remote add <name> <url> | Adds a remote repository to your local repository. | git remote add origin https://github.com/user/repo.git Links the local repo to a remote named “origin”. |
git remote -v | Lists all remote repositories and their URLs. | git remote -v Shows the fetch and push URLs for remotes like “origin”. |
git remote remove <name> | Removes a remote repository from the local config. | git remote remove origin Deletes the “origin” remote from the repository. |
git remote rename <old-name> <new-name> | Renames a remote repository. | git remote rename origin upstream Renames the “origin” remote to “upstream”. |
3. Staging and Snapshot
| Command | Description | Example |
|---|
git status | Show status of files | git status |
git add <file> | Add file to staging | git add index.html |
git add . | Add all files to staging | git add . |
git commit -m "message" | Commit staged changes | git commit -m "Initial commit" |
git commit -a -m "message" | Commit all tracked files | git commit -a -m "Updated config" |
git restore <file> | Discard changes to file | git restore index.html |
git reset <file> | Unstage file | git reset style.css |
| Command Name | Description | Example |
|---|
git add <file> | Adds a specific file to the staging area. | git add index.html Stages index.html for the next commit. |
git add . | Adds all modified and new files in the current directory to the staging area. | git add . Stages all changes in the working directory. |
git add -A | Stages all changes (including deletions) in the entire repository. | git add -A Stages all modified, new, and deleted files. |
git commit -m "<message>" | Commits staged changes with a message. | git commit -m "Add new feature" Creates a commit with the message “Add new feature”. |
git commit -a -m "<message>" | Stages and commits all modified tracked files in one step. | git commit -a -m "Update styles" Stages and commits changes to tracked files. |
git commit --amend | Modifies the most recent commit (e.g., to change the message or add files). | git commit --amend -m "Updated message" Updates the last commit’s message. |
4. Branching and Merging
| Command | Description | Example |
|---|
git branch | List branches | git branch |
git branch <name> | Create branch | git branch feature-x |
git checkout <name> | Switch branch | git checkout feature-x |
git switch <name> | Switch branch (modern way) | git switch main |
git merge <branch> | Merge branch into current | git merge feature-x |
git branch -d <name> | Delete branch | git branch -d feature-x |
git checkout -b <name> | Create and switch | git checkout -b bugfix |
| Command Name | Description | Example |
|---|
git branch | Lists all branches in the repository (current branch marked with *). | git branch Shows branches like main, feature-x. |
git branch <branch-name> | Creates a new branch. | git branch feature-x Creates a branch named feature-x. |
git checkout <branch-name> | Switches to the specified branch. | git checkout feature-x Switches to the feature-x branch. |
git checkout -b <branch-name> | Creates and switches to a new branch in one step. | git checkout -b feature-x Creates and switches to feature-x. |
git branch -d <branch-name> | Deletes a branch (only if merged). | git branch -d feature-x Deletes the feature-x branch if merged. |
git branch -D <branch-name> | Force deletes a branch (even if unmerged). | git branch -D feature-x Deletes feature-x regardless of merge status. |
git branch -m <new-name> | Renames the current branch. | git branch -m new-feature Renames the current branch to new-feature. |
| Command Name | Description | Example |
|---|
git merge <branch-name> | Merges the specified branch into the current branch. | git merge feature-x Merges feature-x into the current branch (e.g., main). |
git merge --abort | Aborts a merge in progress in case of conflicts. | git merge --abort Cancels a conflicted merge and restores the pre-merge state. |
git rebase <branch-name> | Reapplies commits from the current branch onto another branch. | git rebase main Reapplies current branch’s commits on top of main. |
git rebase -i <commit-hash> | Interactively rebase commits (e.g., squash, reorder). | git rebase -i HEAD~3 Interactively rebases the last 3 commits. |
git rebase --continue | Continues a rebase after resolving conflicts. | git rebase --continue Proceeds with rebase after fixing conflicts. |
git rebase --abort | Aborts a rebase in progress. | git rebase --abort Cancels a rebase and restores the original state. |
5. Remote Repositories
| Command | Description | Example |
|---|
git remote -v | Show remotes | git remote -v |
git remote add origin <url> | Add a remote repo | git remote add origin https://github.com/user/repo.git |
git push -u origin main | Push first time | git push -u origin main |
git push | Push latest commits | git push |
git pull | Fetch + merge from remote | git pull origin main |
git fetch | Download changes (no merge) | git fetch origin |
git remote remove origin | Remove a remote | git remote remove origin |
| Command Name | Description | Example |
|---|
git push | Pushes local commits to the default remote branch. | git push Pushes commits from the current branch to its remote counterpart. |
git push origin <branch-name> | Pushes commits to a specific remote branch. | git push origin feature-x Pushes feature-x to the remote origin. |
git push --force | Force pushes commits, overwriting remote history (use cautiously). | git push --force Overwrites the remote branch with local changes. |
git push --set-upstream origin <branch-name> | Pushes a branch and sets it to track the remote branch. | git push --set-upstream origin feature-x Pushes and tracks feature-x on origin. |
git pull | Fetches and merges changes from the remote branch. | git pull Pulls updates from the tracked remote branch. |
git pull origin <branch-name> | Pulls changes from a specific remote branch. | git pull origin main Pulls updates from main on origin. |
git fetch | Downloads objects and refs from the remote without merging. | git fetch Fetches updates from all remotes without merging. |
git fetch origin <branch-name> | Fetches a specific branch from the remote. | git fetch origin main Fetches main from origin. |
6. Stashing (Temporary Save)
| Command | Description | Example |
|---|
git stash | Save changes temporarily | git stash |
git stash list | List all stashes | git stash list |
git stash pop | Apply and remove last stash | git stash pop |
git stash apply | Apply last stash (keep it) | git stash apply |
| Command Name | Description | Example |
|---|
git stash | Saves modified and staged changes to a stack and resets the working directory. | git stash Stashes current changes and cleans the working directory. |
git stash push -m "<message>" | Stashes changes with a custom message. | git stash push -m "WIP feature" Stashes changes with the message “WIP feature”. |
git stash list | Lists all stashed changes. | git stash list Shows all stashes, e.g., stash@{0}: WIP feature. |
git stash apply | Applies the most recent stash without removing it. | git stash apply Reapplies the latest stash to the working directory. |
git stash apply stash@{n} | Applies a specific stash by index. | git stash apply stash@{1} Reapplies the stash at index 1. |
git stash pop | Applies the most recent stash and removes it from the stack. | git stash pop Applies and deletes the latest stash. |
git stash drop stash@{n} | Removes a specific stash from the stack. | git stash drop stash@{0} Deletes the stash at index 0. |
git stash clear | Removes all stashes from the stack. | git stash clear Deletes all stashed changes. |
7. Logs and History
| Command | Description | Example |
|---|
git log | Show commit history | git log |
git log --oneline | One-line summary | git log --oneline |
git show <commit> | Show commit details | git show f3e1a90 |
git blame <file> | Show who changed each line | git blame app.py |
| Command Name | Description | Example |
|---|
git status | Shows the current state of the working directory and staging area. | git status Displays modified, staged, and untracked files. |
git log | Shows the commit history of the current branch. | git log Lists all commits with their details (hash, author, message). |
git log --oneline | Displays commit history in a compact, one-line format. | git log --oneline Shows each commit as a single line with hash and message. |
git log --graph | Shows a graphical representation of the commit history. | git log --graph Displays branch and merge history visually. |
git log --author="<name>" | Filters commits by author. | git log --author="John" Shows commits made by “John”. |
git diff | Shows changes between the working directory and the staging area. | git diff Displays unstaged changes in tracked files. |
git diff --staged | Shows changes between the staging area and the last commit. | git diff --staged Displays staged changes ready to be committed. |
git show <commit-hash> | Shows details and changes for a specific commit. | git show abc123 Displays details of the commit with hash abc123. |
8. Undo and Reset
| Command | Description | Example |
|---|
git reset HEAD~1 | Undo last commit (keep files) | git reset HEAD~1 |
git reset --hard HEAD~1 | Completely remove last commit | git reset --hard HEAD~1 |
git revert <commit> | Revert specific commit | git revert f3e1a90 |
| Command Name | Description | Example |
|---|
git reset <file> | Unstages a file but preserves its changes in the working directory. | git reset index.html Removes index.html from the staging area. |
git reset --hard <commit-hash> | Resets the repository to a specific commit, discarding changes. | git reset --hard abc123 Resets to commit abc123, discarding all later changes. |
git revert <commit-hash> | Creates a new commit that undoes the changes of a specific commit. | git revert abc123 Creates a commit that reverses changes from abc123. |
git checkout -- <file> | Discards changes in a file, restoring it to the last committed state. | git checkout -- index.html Reverts index.html to its last committed version. |
git clean -f | Removes untracked files from the working directory. | git clean -f Deletes untracked files and directories. |
git clean -fd | Removes untracked files and directories (including folders). | git clean -fd Deletes untracked files and directories recursively. |
9. Git Diff Tools
| Command | Description | Example |
|---|
git diff | See unstaged changes | git diff |
git diff --staged | See staged changes | git diff --staged |
git diff <branch1> <branch2> | Compare branches | git diff main dev |
10. Tags (Releases/Versions)
| Command | Description | Example |
|---|
git tag | List tags | git tag |
git tag v1.0 | Create tag | git tag v1.0 |
git tag -a v1.1 -m "Release v1.1" | Annotated tag | git tag -a v1.1 -m "Stable release" |
git push origin v1.1 | Push tag to remote | git push origin v1.1 |
| Command Name | Description | Example |
|---|
git tag <tag-name> | Creates a lightweight tag at the current commit. | git tag v1.0 Creates a tag named v1.0 for the current commit. |
git tag -a <tag-name> -m "<message>" | Creates an annotated tag with a message. | git tag -a v1.0 -m "Release 1.0" Creates an annotated tag with a message. |
git tag | Lists all tags in the repository. | git tag Shows tags like v1.0, v2.0. |
git push origin <tag-name> | Pushes a specific tag to the remote repository. | git push origin v1.0 Pushes the v1.0 tag to origin. |
git push origin --tags | Pushes all tags to the remote repository. | git push origin --tags Pushes all local tags to origin. |
git tag -d <tag-name> | Deletes a tag locally. | git tag -d v1.0 Deletes the v1.0 tag locally. |
git push origin --delete <tag-name> | Deletes a tag from the remote repository. | git push origin --delete v1.0 Deletes the v1.0 tag from origin. |
11. Git Ignore
| File | Description | Example |
|---|
.gitignore | Ignore files/folders | Add node_modules/ or *.log in .gitignore |
12. Git Aliases (Optional)
| Command | Description | Example |
|---|
git config --global alias.co checkout | Set alias | git co dev |
git config --global alias.st status | git st for status | git st |
13. Miscellaneous Commands
Additional useful Git commands for various purposes.
| Command Name | Description | Example |
|---|
git blame <file> | Shows who last modified each line of a file. | git blame index.html Displays author and commit details for each line. |
git bisect start | Starts a binary search to find a bug-introducing commit. | git bisect start Begins the bisect process. |
git bisect good <commit-hash> | Marks a commit as good during bisect. | git bisect good abc123 Marks commit abc123 as good. |
git bisect bad <commit-hash> | Marks a commit as bad during bisect. | git bisect bad def456 Marks commit def456 as bad. |
git bisect reset | Ends the bisect process and returns to the original state. | git bisect reset Exits bisect mode. |
git cherry-pick <commit-hash> | Applies a specific commit to the current branch. | git cherry-pick abc123 Applies the changes from commit abc123. |
git archive --format=zip --output=<file>.zip <branch> | Creates a zip archive of a branch or commit. | git archive --format=zip --output=repo.zip main Creates a repo.zip of the main branch. |
Git Quick Command Cheatsheet
| Use Case | Command |
|---|
| Initialize repo | git init |
| Clone repo | git clone URL |
| Check status | git status |
| Stage files | git add . |
| Commit changes | git commit -m "msg" |
| Push changes | git push origin branch |
| Pull changes | git pull |
| Create branch | git checkout -b branch |
| Merge branches | git merge branch |
| View history | git log --oneline |
| Reset commit | git reset HEAD~1 |
| View remotes | git remote -v |