git checkout -b my-new-branch
Creates and checks out (switch) to a new branch named "my-new-branch".
# Delete remote branch
git push origin --delete branch-to-delete
# Delete local branch
git branch -d branch-to-delete
Deletes the branch called "my-branch-to-delete".
First, checkout the branch you want to merge in, then run:
git merge branch-from
Merges the commits from "branch-from" into the currently checked out branch.
git commit
Concludes a merge after fixing conflicts.
# Delete local branch
git branch -d branch-to-delete
# Delete local branch forcefully
git branch -D branch-to-delete
Deletes the local branch called "my-branch-to-delete". Note that if the branch is not merged upstream, you need to force the deletion using the "-D" option instead of "-d".
git push origin --delete branch-to-delete
Deletes the branch called "my-branch-to-delete" from the "origin" remote.
# Add single file
git add file.txt
# Add multiple files
git add file1.txt file2.txt
# Add all text files in current dir
git add *.txt
# Add files in "my-dir"
git add my-dir
Adds files to staging.
Edit the .gitignore file (create one if it absent)
# Ignore all txt files
*.txt
# Track "file1.txt" even if all other txt files are ignored
!file1.txt
# Ignore a file called "credentials" in the current directory
/credentials
# Ignore all files in any directory named "logs"
logs/
# ignore all txt files in the "logs" folder, but not "logs/apache/log.txt"
logs/*.txt
# ignore all ".txt" files in the "logs" directory and any of its subdirectories
logs/**/*.txt
# Show all commits
git log
# Show the most recent 5 commits
git log -5
# Show commits on one line
git log --oneline
# Show a patch with changes introduced by each commit
# Limiting the result to the most recent two commits
git log -p -2
git revert abc123
Reverts changes introduced by that commit "abc123" by creatting another commit that is the "opposite" of "abc123".
Using the information in your staging area to amend the last commit
git commit --amend
If no change is made to the staging area since your last commit, only the message will be updated. WARNING: use this only for local commits that have not been pushed to a remote. Amending pushed commits will cause problems for your collaborators.
git remote add my-remote-name https://gitcheatsheet.org/example-git-repository.git
# Fetch from "origin" or the configured upstream branch
git fetch
# Fetch all branches from "my-remote-name" remote
git fetch my-remote-name
# Fetch "branch1" from "my-remote-name" remote
git fetch my-remote-name branch1
Downloads data from the remote repository into your local repository, without trying to merge anything with your work.
# Pull from "origin" remote
git pull
# Pull from "my-remote-name" remote
git pull my-remote-name
This command will try to fetch and then merge the remote branch into your local branch.
# Push the current branch on the remote repository
git push
# Push the main (a.k.a. master) branch to the main branch on the "origin" remote
git push origin main
git checkout develop
Checks out (switch) to an existing branch named "develop".
git add myFile.txt
Marks the "myFile.txt" as resolved after a merge conflict.
git merge --abort
Cancels the merge process and tries to go back to the state before the merge.
git branch -vv
Using "-v" instead of "-vv" shows less information.
git status
Shows valuable information, like which files are Untracked, Unmodified, Modified, Staged or Unstaged in the current branch, the current branch name, or whether the current branch is up-to-date, ahead or behind the remote branch.
git commit
This will open your registerd text editor to allow writing a commit message. Once the editor is closed, the commit will be performed.
git rm --cached file1.txt file2.txt
git reset HEAD fileToUnstage.txt
Unstages the file without changing the file contents. It will still be seen as modified, but not staged for commit. Note: this is an alternative to the "git restore" command, which was introduced in Git version 2.23.0.
The "restore" command was introduced in Git version 2.23.0
git restore --staged fileToUnstage.txt
Unstages the file without changing the file contents. It will still be seen as modified, but not staged for commit.
git checkout -- fileToRevert.txt
Replaces the file in the working directory by the latest staged or commited file. WARNING: any local changes made to the file are lost. Git replaces the file with the last staged or committed version.
git remote remove remote-name
Removes "remote-name", together with all remote-tracking branches and configuration settings related to that remote.
git mergetool
Opens up the configured merge tool for resolving conflicts. Additionally, depending on the tool you used, you might need to mark the files as resolved using "git add".
# Regular rebase
git rebase other-branch
# Interactive rebase
git rebase -i other-branch
Rewrites current branch's history so that it has all the commits of "other-branch" and then reapplies the changes in commits that were made in the current branch before the rebase. WARNING: since rebasing rewrites history, you should only use it on local branches.
git diff HEAD
Shows what changed since the last commit (both staged and unstaged files). Note: you can use "--cached" instead of "--staged". They mean the same thing.
git diff --staged
Shows what changed since the last commit and is staged for commit. Note: you can use "--cached" instead of "--staged". They mean the same thing.
git reset abc123
Goes back to commit "abc123" by resetting the staging area to match it, preserving the changed files in the working directory. WARNING: This deletes commits subsequent to "abc123".
git clone https://gitcheatsheet.org/example-git-repository.git
This will copy the remote repository into your current working directory.
git status
Shows the current branch name together with other valuable information.
git status
Shows whether the current branch is up-to-date, ahead or behind the remote branch, together with other valuable information.
git diff
Shows what changed since the last commit but is not yet staged.
git log -p -2
Note: Using -2 to limit the result to the most recent two commits.
git log --author='John'
Note: the command will display any author that contains the string John, e.g. "John Doe" or "Johnny".
git log --committer='John'
Note: the command will display any committer that contains the string John, e.g. "John Doe" or "Johnny".
Displaying commits made between 2021-01-01 (inclusive) and 2021-02-01 (exclusive):
# Using before and after
git log --after="2021-01-01" --before="2021-02-01"
# Using since and until
git log --since="2021-01-01" --until="2021-02-01"
Note: The "--after" and "--since" results INCLUDE the specified date ( >= ), while the "--before" and "--until" EXCLUDE the specified date ( < ).
Searching for the text "hello" in commit messages:
git log --grep="hello" -i
Note: The "-i" option is for case-insensitive search. You can also ad --oneline to make the log more compact.
git reset --hard abc123
Resets the working directory to the state of commit "abc123". WARNING: This deletes uncommitted changes and also deletes commits subsequent to "abc123".
git remote rename old-remote-name new-remote-name
Renames the "old-remote-name" into "new-remote-name".
git config user.email
From the documentation: "Options --system, --global, --local, --worktree and --file <filename> can be used to tell the command to read from only that location".
# Worktree
git config --worktree user.email
# Local (current repository)
git config --local user.email
# Global
git config --global user.email
# System
git config --system user.email
The precedence is: worktree, local, global, system.
git config user.name
From the documentation: "Options --system, --global, --local, --worktree and --file <filename> can be used to tell the command to read from only that location".
# Worktree
git config --worktree user.name
# Local (current repository)
git config --local user.name
# Global
git config --global user.name
# System
git config --system user.name
The precedence is: worktree, local, global, system.
git config --global user.email [email protected]
# Add single file
git add file.txt
# Add multiple files
git add file1.txt file2.txt
# Add all text files in current dir
git add *.txt
# Add files in "my-dir"
git add my-dir
Begins tracking files by adding them to staging.
# Stage new, modified and deleted files
git add .
# Stage new and modified, ignore deleted files
git add --ignore-removal .
# Stage modified and deleted files, ignore new files
git add -u
Inside the repository that you want to configure, run:
git config --local --edit
git config --global core.editor "'C:/path/to/executable' -parameters"
Inside the repository that you want to configure, run:
git config --local user.name "John Doe"
The --local parameter is optional, as it is the default.
Inside the repository that you want to configure, run:
git config --local user.email [email protected]
The --local parameter is optional, as it is the default.
git cherry-pick abc123
Merges only the commit "abc123" into the current branch. Additionally, you can use the "-x" option to automatically append a "cherry picked from commit" to the commit message, specifying which commit has been picked.
# Stash local modifications
git stash push -m "My Stash Message"
# Include untracked files
git stash push -u -m "Including untracked files"
# Stash only specified files
git stash push -u -m "Stashing specific files" -- file1.txt file2.txt
Moves the local modifications into a new stash entry. Using "-u" includes untracked files. The message provided with "-m" is optional.
# Apply the LATEST stash entry (index 0)
git stash apply
# Apply SPECIFIC stash entry (index 1)
git stash apply stash@{1}
# Pop the LATEST stash entry (index 0)
git stash pop
# Pop a SPECIFIC stash entry (index 1)
git stash pop stash@{1}
# Drop the LATEST stash entry (index 0)
git stash drop
# Drop a SPECIFIC stash entry (index 1)
git stash drop stash@{1}
git config --list
From the documentation: "Options --system, --global, --local, --worktree and --file <filename> can be used to tell the command to read from only that location".
git restore fileToRevert.txt
Replaces the file in the working directory by the latest staged or commited file. WARNING: any local changes made to the file are lost. Git replaces the file with the last staged or committed version. Note: the "restore" command was introduced in Git version 2.23.0.
# Show files in the LATEST stash entry (index 0), IGNORING untracked
git stash show
# Show files in the LATEST stash entry (index 0), INCLUDING untracked
git stash show --include-untracked
# Show files in SPECIFIC stash entry (index 1)
git stash show --include-untracked stash@{1}
# Show ONLY UNTRACKED files in stash entry (index 1)
git stash show --only-untracked stash@{1}
# Show ONLY UNTRACKED files in stash entry (index 1)
### Compatible with older versions of Git
git show stash@{1}^3:
Note: older versions of Git do not support the --include-untracked option.
git config --global core.editor "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
git config --global core.editor "code --wait"
git subtree add --prefix my-nested-repo https://gitcheatsheet.org/example-git-repository.git main --squash
Creates a "clone" of the "main" branch of the remote repository into a local directory called "my-nested-repo", squashing the history of the "cloned" repository. Note: instead of specifying the url directly, you can use a remote name, if configured.
git subtree pull --prefix my-nested-repo https://gitcheatsheet.org/example-git-repository.git main --squash
Pulls the "main" branch of the repository specified in the url into a local directory called "my-nested-repo", squashing the history of the pulled repository. Note: instead of specifying the url directly, you can use a remote name, if configured.
git subtree push --prefix my-nested-repo https://gitcheatsheet.org/example-git-repository.git main
Pushes the changes made in "my-nested-repo" into the "main" branch of the specified repository url. Note: instead of specifying the url directly, you can use a remote name, if configured.