Skip to content

rohit-ptl/Git-Cheat-Sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 

Repository files navigation

Git Cheat Sheet

Git commands cheat sheet

Command Effect
CREATE COMMANDS
git init Create a new local git repository
git clone https://github.com/rohit-patel/Git-Cheat-Sheet Clone a remote repository locally
LOCAL CHANGES
git status Get update on locally changed files
git diff Show difference between working directory and staging area
git diff --staged Show difference between staging area and last commit
git add <files> Add <files> to the next commit (stage)
git add . Add all existing changes, including untracked files but excluding ignored files, to the next commit
git add -p <file> Add selective changes(hunks) in <file> to the next commit interactively
git restore --staged <files> Unstage <files>
git restore --staged . Unstage all files
git restore <files> Discard changes of <files> in working directory. Revert to staged version
git restore . Discard all changes in working directory. Revert to staged version
git commit -a Stage and Commit all changes of currently tracked files
git commit Commit staged changes
git commit -m "message" Commit staged file with commit message without invoking editor
git commit --amend Amend the last commit
COMMIT HISTORY
git log Show all commits reachable from HEAD, newest first
git log <commit1> ^<commit2> Show all commits reachable from commit1 but isn't reachable by commit2. Branch name instead of commit hash can also be used
git log -p Show changes alongwith each commit reachable from HEAD
git log -p -- <file> Show changes of each commit reachable from HEAD
git log -p <commit> -- Show changes of each commit reachable from <commit>
git log --since="2 weeks ago" -- <file> Show changes during last two weeks to file <file>
git log --stat --summary Show overview of changes alongwith commits reachable from HEAD
git blame <file> See change history in <file> by user
BRANCHES AND TAGS
git branch List all existing local branches
git branch -r List all existing remote branches
git branch -a List all existing branches from both local and remote
git branch -v List all existing local branches alongwih sha1 and subject line of last commit of each branch
git branch --merged List of branches already merged into current branch
git branch --no-merged List of branches not merged into current branch
git switch <branch> Switch HEAD to <branch>
git checkout <branch> -- Switch HEAD to <branch>. switch command is safer than this
git branch <new_branch> Create branch <new_branch> based on current HEAD
git branch <new_branch> <base_branch> Create branch <new_branch> based on <base_branch>
git branch <new_branch> <tag_name> Create branch <new_branch> based on tag <tag_name>
git switch -c new-branch To create and swtich to new branch in one go
git checkout --track <remote/branch> Create new local branch based on a remote branch1 and switch to new branch
git branch --track <remote/branch> Create new local branch based on a remote branch1
git branch -d <branch> Delete local <branch>. Prevents deletion of unmerged branch
git branch -D <branch> Delete local <branch>. Unsafe as it can delete unmerged branch
git tag <tag_name> Create ligthweight tag for current commit
git tag -a <tag_name> -m "tag message" Create annotated tag for current commit
git tag <tag_name> <commit checksum> Create ligthweight tag for specified commit
git tag List tags in alphabetical order
git tag -l <pattern> List tags matching <pattern>
git tag -d <tag_name> Delete tag <tag_name>
UPDATE AND PUBLISH
git remote Shows a list of existing remotes
git remote -v List all currently configured remotes alongwith remote url
git ls-remote <remote> List references2 in remote repository <remote>
git remote show <remote> Show information about a remote
git remote add <shortname> <url> Add new <remote> repository
git fetch <remote> Download all changes3 from <remote> but don't integrate into HEAD
git pull <remote> <branch> Download changes and merge4 <remote> into HEAD
git push <remote> <branch> Push local changes to <remote>
git push -u <remote> <branch> Push local changes to <remote> and track it
git push <remote> --tags Push all local tags to
git push <remote> --delete <branch> Delete <branch> from <remote>5
git push <remote> --delete <tag_name> Delete tag <tag_name> from <remote>
git branch -dr <remote/branch> Delete a local remote tracking branch <remote/branch>
git push --tags Publish your tags
MERGE AND REBASE
git merge <branch> Merge branch into your current HEAD
git merge --abort Abort merging
git rebase <branch> Rebase your current HEAD onto <branch>
git rebase --abort Abort a rebase
git rebase --continue Continue a rebase after resolving conflicts
git mergetool Use your confgured merge tool to resolve conflicts
UNDO
git reset --hard HEAD Discard all local changes in your working directory
git checkout HEAD <file> Discard changes in a specific <file>
git revert <commit> Revert a commit by producing a new commit with contrary changes
git reset <commit> Reset HEAD pointer to <commit> and preserve changes as unstaged changes
git reset --hard <commit> Reset HEAD pointer to <commit> and discard all following changes
git reset --keep <commit> Reset HEAD pointer to <commit> and preserve uncommitted local changes
Footnotes

Footnotes

  1. Also create a tracking branch based on a remote branch 2

  2. In git references or refs are refrences to commits. E.g. branches, remote-tracking branches and tags.

  3. Download references alongwith objects necessary to complete their histories.

  4. Instead of merge we can also do rebasing

  5. It also delete corresponding remote tracking branch.

About

Git commands cheat sheet

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published