Initializing a repository
git init
git config --global user.name "username"
git config --global user.email "email@example.com"
Command | Description |
---|---|
git add file1.js |
# Stages a single file |
git add file.js file2.js |
# Stages multiple files |
git add *.js |
# Stages with a pattern |
git add . |
# Stages the current directory and all its content |
Command | Description |
---|---|
git status |
# Full status |
git status -s |
# Short status |
Command | Description |
---|---|
git commit -m "Message" |
# Commits with a one-line message |
git commit |
# Opens the default editor to type a long message |
Command | Description |
---|---|
git rm file1.js |
# Removes from working directory & staging area |
git rm --cached file1.js |
# Removes fom staging area only |
Command | Description |
---|---|
git mv file.js file1.js |
# Renames or moves existing file to new |
Command | Description |
---|---|
git diff |
# Shows unstaged changes |
git diff --staged |
# Shows staged changes |
git diff --cached |
# Same as the above |
Command | Description |
---|---|
git log |
# Full history |
git log --oneline |
# Summary |
git log --reverse |
# Lists the commits from the oldest to the newest |
Command | Description |
---|---|
git show 93412ff |
# Shows the given commit |
git show HEAD |
# Shows the last commit |
git show HEAD~2 |
# Two steps before the last commmit |
git show HEAD:file.js |
# Shows the version of file.js store in the last commit |
git ls-tree HEAD~1 |
# Shows commit when multiple directorires are included |
Command | Description |
---|---|
git restore --staged file.css blob.js |
# Copies the last version of file from last snapshot to index |
Command | Description |
---|---|
git restore file.css |
# Copies file.css from index to working directory |
git restore file.css text.sh |
# Restore multiple files in working directory |
git restore . |
# Discards all local changes (except untracked files) |
git clean -fd |
# Removes all untracked files |
git restore --source=HEAD~2 file.js
Command | Description |
---|---|
git log --stat |
# Shows the list of modified files |
git log --patch |
# Shows the actual changes (patches) |
Command | Description |
---|---|
git log -3 |
# Shows the last 3 entries |
git log --author="cosmicqbit" |
# Filter commits by author |
git log --before="2022-03-6" |
# Show commits before date |
git log --after="one week ago" |
# Show commits after date |
git log --grep="GUI" |
# Commits with "GUI" in their message |
git log -S"Copy Tools" |
# Commits with "Copy Tools" in their patches |
git log hash1..hash2 |
# Range of commits |
git log file.txt |
#Commits that touched file.txt |
Command | Description |
---|---|
git log --pretty=format:"%an commited %H" |
# Shows author name commited hash |
git log --pretty=format:"%an commited %h on %cd " |
# Above with commit date |
git log --pretty=format:"%Cgreen%an%Creset commited %H" |
# Colorizes the author name |
Command | Description |
---|---|
git config --global alias.lg "log --oneline" |
# git lg will do the job |
git config --global alias.unstage "restore --staged |
# git unstage will do the job |
Command | Description |
---|---|
git show HEAD~2 |
# Alt method for displaying using hash |
git show HEAD~2 HEAD file.txt |
# Changes to file.txt only |
Command | Description |
---|---|
git diff HEAD~2 HEAD |
# Shows the changes between two commits |
git diff HEAD~2 HEAD file.txt |
# Changes to file.txt only |
Command | Description |
---|---|
git checkout rad763g |
# Checks out the given commit |
git checkout master |
# Checks out the master branch |
Command | Description |
---|---|
git bisect start |
# Initializing the method |
git bisect bad |
# Marks the current commit as a bad commit |
git bisect good ca49190 |
# Marks the given commit as a good commit |
git bisect reset |
# Terminates the bisect session |
Command | Description |
---|---|
git shortlog |
# List contributors list |
git shortlog -n -s -e |
# Lists contributors in order, suppresses commit m & displays email |
Command | Description |
---|---|
git log file.txt |
# Shows the commits that touched file.txt |
git log --oneline --stat file.txt |
# Shows stats (no of changes) for file.txt |
git log --patch file.txt |
# Shows the patches (changes) applied to file.txt |
Command | Description |
---|---|
git log --oneline -- toc.txt |
# List all commits which touched the file |
git checkout a64d33 toc.txt |
# Step 2 |
git status -s |
# Step 3 |
git commit -m "commit message" |
# Step 4 |
Command | Description |
---|---|
git blame file.txt |
# Shows the author of each line in file.txt |
Command | Description |
---|---|
git tag v1.0 |
# Tags the last commit as v1.0 |
git tag v.1.0.1 4e4a897 |
# Tags an earlier commit |
git checkout v.1.9 |
# Checks out the given tag |
git tag |
# Lists all the tags |
git tag -a v1.1 -m "My version 1.1" |
# Create annotated tag |
git tag -n |
# Display commit messages with annotated tags |
git show v1.1 |
# Show tag details |
git tag -d v1.1 |
# Deletes the given tag |
Command | Description |
---|---|
git branch bugfix |
# Creates a new branch called bugfix |
git checkout feature |
# Switches to the feature branch |
git switch feature |
# Same as the above |
git switch -C feature |
# Creates & switches |
git branch -d bugfix |
# Deletes the feature branch |
git status |
# Check the current branch name |
git branch -m feature new-feature |
# Renames the branch to new-feature |
Command | Description |
---|---|
git log master..bugfix |
# Lists the commits in the bugfix branch not in master |
git diff master..bugfix |
# Shows the summary of changes |
Command | Description |
---|---|
git stash push -m "Stash message" |
# Creates a new stash |
git stash push -a -m "Stash message" |
# Stashes untracked files as well |
git stash list |
# Lists all the stahes |
git stash show stash@(1) |
# Shows the given stash |
git stash show 1 |
# Shortcut for stash@(1) |
git stash apply 1 |
# Applies the given stash to the working dir |
git stash drop 1 |
# Deletes the given stash |
git stash clear |
# Deletes all the stashes |
Command | Description |
---|---|
git merge bugfix |
# Merges the bugfix branch into the current branch |
git merge --no-ff bugfix |
# Creates a merge commit even if FF is possible |
git merge --squash bugfix |
# Performs a squash merge |
git merge --abort |
# Aborts the merge |
Command | Description |
---|---|
git branch merged |
# Shows the merged branches |
git branch --no-merged |
# Shows the unmerged branches |
Rebasing
Command | Description |
---|---|
git rebase master |
# Changes the base of the current branch |