| title | Git | ||||
|---|---|---|---|---|---|
| date | 2020-11-25 10:28:43 -0800 | ||||
| background | bg-[#d7593e] | ||||
| tags |
|
||||
| categories |
|
||||
| intro | This cheat sheet summarizes commonly used Git command line instructions for quick reference. | ||||
| plugins |
|
Create a new local repository
$ git init [project name]Clone a repository
$ git clone git_urlClone a repository into a specified directory
$ git clone git_url my_directoryShow modified files in working directory, staged for your next commit
$ git statusStages the file, ready for commit
$ git add [file]Stage all changed files, ready for commit
$ git add .Commit all staged files to version history
$ git commit -m "commit message"Commit all your tracked files to version history
$ git commit -am "commit message"Discard changes in working directory which is not staged
$ git restore [file]Unstage a staged file or file which is staged
$ git restore --staged [file]Unstage a file, keeping the file changes
$ git reset [file]Revert everything to the last commit
$ git reset --hardDiff of what is changed but not staged
$ git diffDiff of what is staged but not yet committed
$ git diff --stagedApply any commits of current branch ahead of specified one
$ git rebase [branch]Set the name that will be attached to your commits and tags
$ git config --global user.name "name"Set an email address that will be attached to your commits and tags
$ git config --global user.email "email"Enable some colorization of Git output
$ git config --global color.ui autoEdit the global configuration file in a text editor
$ git config --global --editList all local branches
$ git branchList all branches, local and remote
$ git branch -avSwitch to my_branch, and update working directory
$ git checkout my_branchCreate a new branch called new_branch
$ git checkout -b new_branchDelete the branch called my_branch
$ git branch -d my_branchMerge branchA into branchB
$ git checkout branchB
$ git merge branchATag the current commit
$ git tag my_tagShow the commit history for the currently active branch
$ git logShow the commits on branchA that are not on branchB
$ git log branchB..branchAShow the commits that changed file, even across renames
$ git log --follow [file]Show the diff of what is in branchA that is not in branchB
$ git diff branchB...branchAShow any object in Git in human-readable format
$ git show [SHA]Fetch down all the branches from that Git remote
$ git fetch [alias]Merge a remote branch into your current branch to bring it up to date
$ git merge [alias]/[branch]
# No fast-forward
$ git merge --no-ff [alias]/[branch]
# Only fast-forward
$ git merge --ff-only [alias]/[branch]Transmit local branch commits to the remote repository branch
$ git push [alias] [branch]Fetch and merge any commits from the tracking remote branch
$ git pullMerge just one specific commit from another branch to your current branch
$ git cherry-pick [commit_id]Add a git URL as an alias
$ git remote add [alias] [url]Show the names of the remote repositories you've set up
$ git remoteShow the names and URLs of the remote repositories
$ git remote -vRemove a remote repository
$ git remote rm [remote repo name]Change the URL of the git repo
$ git remote set-url origin [git_url]Save modified and staged changes
$ git stashList stack-order of stashed file changes
$ git stash listWrite working from top of stash stack
$ git stash popDiscard the changes from top of stash stack
$ git stash dropDelete the file from project and stage the removal for commit
$ git rm [file]Change an existing file path and stage the move
$ git mv [existing-path] [new-path]Show all commit logs with indication of any paths that moved
$ git log --stat -M/logs/*
# "!" means don't ignore
!logs/.gitkeep
/# Ignore Mac system files
.DS_store
# Ignore node_modules folder
node_modules
# Ignore SASS config files
.sass-cache
A .gitignore file specifies intentionally untracked files that Git should ignore
-
$ git branch -m <new_name>
-
$ git push origin -u <new_name>
-
{.marker-timeline}
$ git push origin --delete <old>
Search change by content
$ git log -S'<a term in the source>'Show changes over time for specific file
$ git log -p <file_name>Print out a cool visualization of your log
$ git log --pretty=oneline --graph --decorate --allList files changed in a commit
$ git log --name-only <commit-id>List files changed in commits excluding merges
$ git log --no-merges --name-onlyLimit commits to the last N entries
$ git log -<number>Limit commits to the last N entries (alternate syntax)
$ git log -n <number>Limit commits to a maximum of N entries
$ git log --max-count=<number>Show commits more recent than a specific date
$ git log --after="YYYY-MM-DD HH:MM:SS ±HHMM"Show commits older than a specific date
$ git log --before="YYYY-MM-DD HH:MM:SS ±HHMM"Filter commits by author matching a pattern
$ git log --author=<pattern>Filter commits by commit message matching a pattern
$ git log --grep=<pattern>Show commit graph of the current branch with reference decorations
$ git log --graph --decorateShow commit graph of a specific branch without switching to it
$ git log --graph --decorate <branch>Show commit graph for commits in one branch not in another
$ git log --graph --decorate <branchB>..<branchA>Show commit graph of all branches
$ git log --graph --decorate --allList all branches and their upstreams
$ git branch -vvQuickly switch to the previous branch
$ git checkout -Get only remote branches
$ git branch -rCheckout a single file from another branch
$ git checkout <branch> -- <file>Rewrite last commit message
$ git commit --amend -m "new message"Amend the latest commit without changing the commit message.
$ git commit --amend --no-editSee also: Rewriting history
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st statusSee also: More Aliases
Create a new working tree at the specified path. If a branch is specified, it checks out that branch. If no branch is given, a new one is created based on HEAD and named after the new directory.
$ git worktree add <path> [<branch>]List all of the active working trees associated with the repository.
$ git worktree listRemove the specified working tree.
$ git worktree remove <path>Clean up stale or manually deleted working trees.
$ git worktree pruneRelocate the working tree to a new path.
$ git worktree move <old_path> <new_path>Create a new submodule within your repository:
$ git submodule add <repository_url> <path>Clone a repository and initialize its submodules:
$ git clone --recursive <repository_url>Update all the submodules in your repository to the latest commit of their respective branches:
$ git submodule updatePull the latest changes from the remote repositories of the submodules and update them in your main repository:
$ git submodule update --remoteRemove a submodule from your repository:
$ git submodule deinit <path>
$ git rm <path>
$ git commit -m "Removed submodule"Cherry-picking allows you to apply a specific commit from one branch to another branch.
$ git cherry-pick <commit_hash>Display the reflog, showing the history of HEAD and branch movements:
$ git reflogFind the hash of the lost commit or branch using the reflog and then checkout to that hash to restore it:
$ git checkout <commit_or_branch_hash>