- stash
- workspace
- index
- local repository
- upstream repository
git’s man page | Git. |
git version’s man page | Display version information about Git. |
git -v
git --version
git version | Print the version of Git. |
git --attr-source=<tree-ish> | Read gitattributes from <tree-ish> instead of the worktree. |
git init’s man page | Create an empty local repository. | |
git init -q
git init --quiet | Only print error and warning messages; all other output will be suppressed. | |
| Create an empty server repository. |
git clone’s man page | Clone a repository into a new directory. |
git clone <bare repository directory> | Create a repository which is the clone of a bare repository. |
git clone <bare repository directory> <my directory name> | Create a repository with name <my directory name> which is the clone of a bare repository (if <my directory name> is not specified, the name of the remote repository is used). |
git clone --bare <path to directory containing .git> | Create a bare repository which is the clone of a repository. |
git clone --single-branch | Clone only the history leading to the tip of a single branch, either specified by the --branch option or the primary branch remote’s HEAD points at. |
git clone --depth <depth> | Create a shallow clone with a history truncated to the specified number of commits. Imply --single-branch unless --no-single-branch is given to fetch the histories near the tips of all branches. |
git clone --recurse-submodules | Clone all submodules. |
git clone --quiet
git clone -q | Progress is not reported to the standard error stream. |
git clone --verbose
git clone -v | Run verbosely, but progress is reported as usual on stderr. |
git clone --config <key>=<value>
git clone -c <key>=<value> | Set a configuration variable in the created repository, after the repository is initialised, before the remote history is fetched or any files checked out. |
git clone --no-checkout
git clone -n | No checkout of HEAD is performed after the clone is complete. |
git remote’s man page | List the upstream and downstream repositories. |
git remote | Display the list of existing remotes. |
git remote -v | The same more verbose and with remote URL after name. |
git remote add <remotename> <url> | Add a remote named <remotename> for the repository at <url>. |
git remote remove <remotename>
git remote rm <remotename> | Remove the remote named <remotename>. All remote-tracking branches and configuration settings for the remote are removed. |
git config --get remote.origin.url | Display the URL the local repository has been cloned from. |
git remote -v rm origin | Remove the tracking of the remote "origin". |
-
Workaround to clone a repository which is too big to be transferred (shallow clone).
git clone --depth 5 https://gitlab.com/foo/bar.git
git fetch --unshallow
-
Clone a directory with file paths/names too long for Windows.
git clone -c core.longpaths=true https://gitlab.com/foo/bar.git
-
Clone a repository while replacing symbolic links by text files.
git clone -c core.symlinks=false https://gitlab.com/foo/bar.git
-
On Windows, the Windows Credential Manager (Gestionnaire d'identification) is used by default (see
git config --system --get credential.helper
). -
To be able to use Git in a directory owned by someone else (this was implemented to fix CVE-2022-24765)
git config set --global --append safe.directory `pwd`
git config get --global --all safe.directory
git config unset --global --value `pwd` safe.directory
git ls-files’s man page | List files which are in the workspace or in the index. |
git ls-files -m | List modified files. |
git ls-files -o | List files which are in the workspace and not in the index. |
git add’s man page | Add file contents to the index. |
git add <file> | Update <file> in the index using the current content found in the working tree. |
git add . | Update the content of the current directory in the index. |
git add -i
git add --interactive | Add modified contents in the working tree interactively to the index.
Optional path arguments may be supplied to limit operation to a subset of the working tree. |
git stage’s man page | git stage is a synonym of git add . |
git rm’s man page | Remove files from the working tree and from the index. |
git rm <file> | Remove <file> from the index, or from the working tree and the index. |
git status’s man page | Display
|
git commit’s man page | Record changes to the repository. |
git commit -m <msg> | Store the current contents of the index in a new commit along with message <msg>. |
git commit -a
git commit --all | Automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected. |
git commit -o
git commit --only | Make a commit by taking the updated working tree contents of the paths specified on the command line, disregarding any contents that have been staged for other paths.
(This is the default of git commit if any paths are given on the command line. So, in this case the -o is useless.) |
git commit --amend | Replace the tip of the current branch by creating a new commit. |
git commit --amend --no-edit | Amend a commit without changing its commit message. |
git commit --amend -m "an updated commit message" | Change the commit message. |
git commit --amend --reset-author | Declare that the authorship of the new commit belongs to the committer. |
git commit --amend --author="John <John@foo.bar>" | Declare that the authorship of the new commit belongs to the specified name/email. |
git commit -S
git commit --gpg-sign | GPG-sign commits. |
git notes’s man page | Add or inspect object notes |
git notes add -m <message> | Add notes to the HEAD object. |
git notes add -f -m <message> | Overwrite notes of the HEAD object. |
git notes append -m <message> | Append the message to the notes of the HEAD object. (If it has no notes, this will create them.) |
git diff’s man page | Display the differences between the workspace and the index. |
git diff --cached | Display the differences between the index and the local repository. |
git diff origin/main | Display the differences between the work space and the main branch on the upstream repository. |
git diff --name-only | Display only the filenames. |
git diff <commit 1> <commit 2> <file> | Compare two revisions of a file. |
git diff <branch 1> <branch 2> -- <file> | Compare two versions of a file in different branches,
git diff master remotes/origin/hisbranch -- pom.xml |
git diff <branch> -- <file> | Compare the current version of a file with one in a different branch. |
git diff <commit 1>:<file 1> <commit 2>:<file 2> | Compare two different files in two different revisions. |
git diff --word-diff | Show a word diff. |
git diff -U<n>
git diff --unified=<n> | Generate diffs with <n> lines of context instead of the usual three. |
git apply’s man page | Apply a patch. |
git log’s man page | Show commit logs. |
git log --all | Pretend as if all the refs in refs/ , along with HEAD , are listed on the command line as <commit>. |
git log --full-history | Do not prune some history as the default mode (useful to see the merges). |
-- | Separate paths from revisions.
git log -- **/*.md will show logs of commits impacting the Markdown files. |
-L<start>,<end>:<file>
-L:<funcname>:<file> | Show the code changes for a part of a file.
<start> and <end> are numbers or regexps.
<end> can be defined as an offset.
<funcname> is a regexp. |
git log --full-history | Does not prune some history. |
git log --full-history --simplify-merges | Same as --full-history but remove some needless merges. |
git log --full-history -- <myfile> | Display the log of a file even if this one does not exist anymore. |
git log -<n> | Limit the log to the n last commits. |
git log --since "2020-09-05 00:00" --until "2020-09-06 24:00" | Limit the log between two datetimes. |
git log --committer=<regex> | Filter on a given committer (if there are several --committer flags, return commits of committers matching any of the regexps). |
git log --author=<regex> | Filter on a given author (if there are several --author flags, return commits of authors matching any of the regexps). |
git log --grep=<regexp> | Filter the commits to the ones with log message that matches the specified pattern (regular expression).
With more than one --grep=<regexp> , commits whose message matches any of the given patterns are chosen.
With more than one --grep=<regexp> and ---all-match , limit the commits output to ones that match all given regular expressions. |
--invert-grep | Limit the commits output to ones with log message that do not match. |
-i
--regexp-ignore-case | Match the regular expression limiting patterns without regard to letter case. |
--basic-regexp | Consider the limiting patterns to be basic regular expressions; this is the default. |
-E
--extended-regexp | Consider the limiting patterns to be extended regular expressions instead of the default basic regular expressions. |
-F
--fixed-strings | Consider the limiting patterns to be fixed strings. |
-P
--perl-regexp | Consider the limiting patterns to be Perl-compatible regular expressions. |
git log -S<string> | Look for differences that change the number of occurrences of the specified string. |
git log -S<regexp> --pickaxe-regex | Look for differences that change the number of occurrences of the specified regexp. |
git log -G<regexp> | Look for differences whose patch text contains added/removed lines that match the specified regular expression.
(Use git log -G<regexp> -p to display all the matching changes.
Use git log -G<regexp> --all to look in all branches.) |
--pickaxe-all | When -S or --G finds a change, show all the changes in that changeset, not just the files that contain the string/regexp. |
git log --pretty=<format> | Show more and more information.
|
git log --pretty="%cI %s" | Show the commit date and the commit subject. |
git log --pretty="%ce,%cd" --date=short > commits.csv | Dump the authors and dates of the commits in a file. |
git log --name-only | Show only names of changed files. |
git log --name-status | Show only names and status of changed files. |
git log --abbrev-commit | SInstead of showing the full 40-byte hexadecimal commit object name, show a prefix that names the object uniquely. |
git log --oneline | Shorthand for --pretty=oneline --abbrev-commit . |
git log decorate
git log decorate=short | Print out the ref names of any commits that are shown. The ref name prefixes refs/heads/ , refs/tags/ , and refs/remotes/ are not printed. |
git log decorate=long | Print out the full ref names of any commits that are shown. |
git log decorate=auto | If the output is going to a terminal, the ref names are shown as if short was given, otherwise no ref names are shown. |
git log --stat | Indicate the modified files with a graph of the stats of the changes. |
git log --shortstat | Indicate the numbers of modified files, added and deleted lines. |
git log -p
git log --patch | Show the code changes in each commit. |
git log -U<n>
git log --unified=<n> | Generate diffs with n lines of context instead of the usual 3. Implies -p . |
git log --graph | Draw a text-based graphical representation. |
git log --reverse | Output the commits chosen to be shown in reverse order. |
git whatchanged’s man page | Show logs with difference each commit introduces. |
git whatchanged | List the last commits: hash, author, date, and list of impacted files. |
git shortlog’s man page | Summarize git log output. |
git shortlog -n
git shortlog --numbered | Sort output according to the number of commits per author instead of author alphabetic order. |
git shortlog -s
git shortlog --summary | Suppress commit description and provide a commit count summary only. |
git shortlog -e
git shortlog --email | Show the email address of each author. |
git shortlog -nse | List of authors with their emails sorted by number of commits. |
git show’s man page | Show changes between commits, commit and working tree… |
git show <commit> | Show the log message and textual diff. |
git show --name-only <commit> | Show the log message and the name of the modified files. |
git show --name-status <commit> | Show the log message and the name and status of the modified files. |
git show <commit>:<file> | Display a given version of a file. |
git blame’s man page | Show what revision and author last modified each line of a file. |
git blame <file> | Annotate each line of the file with information from the revision which last modified the line. |
git blame -L <start>,<end>
git blame -L :<funcname> | Annotate only the line range given by <start> ,<end> , or by the function name regex <funcname> . May be specified multiple times. Overlapping ranges are allowed. |
git blame -M | Detect moved or copied lines within a file. |
git blame -C | Detect lines moved or copied from other files that were modified in the same commit.
When this option is given twice ( ), the command additionally looks for copies from other files in the commit that creates the file. When this option is given three times( ), the command additionally looks for copies from other files in any commit. |
git blame -w | Ignore whitespaces. |
git blame -f
git blame --show-name | Show the filename in the original commit (by default the filename is shown if there is any line that came from a file with a different name, due to rename detection). |
git blame --date=format:%Y%d | Define a format for the dates. |
git blame --show-email | Show the author email instead of author name. |
git blame -l | indicate all (40) characters of the revision. |
git annotate’s man page | Annotate file lines with commit information. |
git annotate <file> | Same feature as git blame but the output is slightly different. |
git reflog’s man page | Manage reflog information |
git reflog
git reflog show | Show the log of HEAD. |
git reflog <ref>
git reflog show <ref> | Show the log of the reference <ref>. |
git stash’s man page | Stash the changes in a dirty working directory away. |
git stash
git stash push | Save local modifications away and revert the working directory to match the HEAD commit (in the working tree and in the index). |
git stash push --message <message>
git stash push -m <message> | Attach a description to the stashed state. |
git stash push --keep-index | All changes already added to the index are left intact. |
git stash push --include-untracked
git stash push --all
git stash -u
git stash -a | --include-untracked , -u : all untracked files are also stashed and then cleaned up with git clean
--all , -a : the ignored files are stashed and cleaned in addition to the untracked files. |
git stash push -u foo.txt bar.txt new.txt | Stash a list of modified and new files. |
git stash push -p
git stash push --patch | Interactively select hunks from the diff between HEAD and the working tree to be stashed. |
git stash list | List the stashed entries. |
git stash list --pretty | List the stashed entries with more information. |
git stash apply [<stash>] | Restore the entry <stash> (the last one if <stash> is not present). The entry is kept among the stashed entries. |
git stash apply --index [<stash>] | Try to reinstate not only the working tree’s changes, but also the index’s ones. |
git stash drop [<stash>] | Remove the stash entry <stash> (the last one if <stash> is not present). |
git stash pop [--index] [<stash>] | Apply and remove the stash entry <stash> (the last one if <stash> is not present). |
git stash branch <branchname> [<stash>] | Create and check out a new branch <branchname> starting from the commit at which <stash> was created, apply the changes recorded in <stash> to the new working tree and index. |
git stash clear | Remove all stash entries. |
git worktree’s man page | Manage multiple working trees. |
git worktree add <path> <branchname> | Create a worktree in directory <path> for branch <branchname>. |
git worktree list | List details of each working tree. The main working tree is listed first. |
git worktree remove <worktree> | Remove a worktree. The main working tree cannot be removed. |
git reset’s man page | Reset current HEAD to the specified state. |
git reset <commit> | Reset the index entries for all paths to a previous commit
If no commit is specified, the current HEAD is used. The local changes are not lost. |
git reset HEAD~ | Cancel the last commit. |
git reset <commit> <pathspec> | Reset the index <pathspec> entry to a previous commit
if no commit is specified, the current HEAD is used. This means that git reset <pathspec> is the opposite of git add <pathspec> . |
git reset --soft <commit> | Do not touch the index file or the working tree. Only resets the head to <commit>, just like all modes do. |
git reset --mixed <commit> | Reset the index but not the working tree
this is the default. |
git reset --hard <commit> | Reset the index and the working tree. |
git reset --hard HEAD~2 | Delete the last two commits from the local repository. |
git restore’s man page | Restore working tree files. |
git restore <file>
git checkout <file>
git checkout -- <file> | Replace <file> in the working tree by its version in HEAD.
git checkout <file> will not work if a branch is named <file>, this will checkout that branch. |
git restore --staged <file> | Remove the file will from staging, its modifications in the workspace remain untouched. |
git restore --source <commit> <file> | Replace <file> in the working tree by its version in commit <commit>. |
git restore --source HEAD@{10.minutes.ago} <file>
git checkout HEAD@{10.minutes.ago} -- <file> | Replace <file> in the working tree by its HEAD version as it was 10 minutes ago. |
git restore -p <file>
git restore --patch <file>
git checkout -p -- <file>
git checkout --patch -- <file> | Interactively select hunks in the difference between the restore source and the restore location. |
git sparse-checkout’s man page | Reduce your working tree to a subset of tracked files |
git sparse-checkout set MY/DIR1 SUB/DIR2 | Enable the necessary sparse-checkout config settings (core.sparseCheckout , core.sparseCheckoutCone , and index.sparse ) if they are not already set to the desired values, populate the sparse-checkout file from the list of arguments following the set subcommand, and update the working directory to match. |
git sparse-checkout add MY/DIR1 SUB/DIR2 | Update the sparse-checkout file to include additional directories (in cone mode) or patterns (in non-cone mode). |
REPO=https://github.com/danascheider/tessitura-front-end.git
|
git revert’s man page | Revert some existing commits. |
git revert <commit1> <commit2> … | Revert some commits (a revert is a staged commit reverting a given past commit). If we indicate several commits on the command line, there will be one revert per indicated commit. |
git replace’s man page | Create, list, delete refs to replace objects. |
git fetch’s man page | Download objects and refs from another repository. |
git fetch | Download objects and refs from upstream repository into local repository.
The names of refs that are fetched, together with the object names they point at, are written to .git/FETCH_HEAD . |
git fetch --all | Fetch all remotes. |
git fetch --append
git fetch -a | Append ref names and object names of fetched refs to the existing contents of .git/FETCH_HEAD . Without this option old data in .git/FETCH_HEAD will be overwritten. |
git fetch --depth=<depth> | Limit fetching to the specified number of commits from the tip of each remote branch history.
Tags for the deepened commits are not fetched. |
git fetch --deepen=<depth> | Similar to --depth , except it specifies the number of commits from the current shallow boundary instead of from the tip of each remote branch history. |
git fetch --unshallow | If the source repository is complete, convert a shallow repository to a complete one, removing all the limitations imposed by shallow repositories.
If the source repository is shallow, fetch as much as possible so that the current repository has the same history as the source repository. |
git fetch origin dev:dev | Update the dev branch to be the head of the remote dev branch. |
git fetch -p
git fetch --prune | Before fetching, remove any remote-tracking references that no longer exist on the remote. |
git pull’s man page | Fetch from and integrate with another repository or a local branch. |
git pull | Incorporate changes of the remote repository into the current branch, it is the same as
|
git pull --rebase | Perform a rebase instead of a merge. |
git pull -p
git pull --prune | Before fetching, remove any remote-tracking references that no longer exist on the remote. |
git push’s man page | Update remote refs along with associated objects. |
git push <remotename> <branchname> | Push commits made on a local branch to a remote repository in the branch of same name. |
git push https://username:password@mygithost.com/file.git | Specify the credentials on the command line. |
git push --all | Push all branches. |
git push -u origin <branchname>
git push --set-upstream origin <branchname> | For every branch that is up to date or successfully pushed, add upstream (tracking) reference.
This is to be used when a branch has been created locally and does not exist in the remote repository. |
git push --force
git push -f | Do not check that the remote ref is an ancestor of the local ref.
Do not use, use --force-with-lease instead. |
git push --force-with-lease | Only allow to force-push if no-one else has pushed changes up to the remote in the interim. |
git push --mirror | All refs under refs/ (which includes but is not limited to refs/heads/ , refs/remotes/ , and refs/tags/) are mirrored to the remote repository. Newly created local refs are pushed to the remote end, locally updated refs are force updated on the remote end, and deleted refs are removed from the remote end. |
git push --dry-run
git push -n | Do everything except actually send the updates. |
git push --signed | GPG-sign the push request. |
git push --tags | All refs under refs/tags are pushed, in addition to refspecs explicitly listed on the command line. |
git clean’s man page | Delete untracked files from the working tree. |
git clean -d | Recurse into untracked directories. |
git clean -e <pattern>
git clean --exclude=<pattern> | Exclude the files/directories matching <pattern> in addition to the standard ignore rules. |
git clean -x | Don’t use the standard ignore rules, but still use the ignore rules given with -e options from the command line. This allows removing all untracked files, including build products. |
git clean -X | Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files. |
git clean -f
git clean --force | Clean file with no interaction. Perform the deletion if the Git configuration variable clean.requireForce is not set to false .
Untracked nested git repositories (directories with a .git subdirectory) are ignored unless a second -f is given. |
git clean -i
git clean --interactive | Show what would be done and clean files interactively. |
git clean -n
git clean --dry-run | Does not remove anything, list the files and directories that would be removed. |
git branch’s man page | List, create, or delete branches. | |
git branch --show-current | Print the name of the current branch. | |
git branch <new branch> | Create a new branch (but does not switch to it). | |
git branch <new branch> <commit> | Create a new branch from a given commit. | |
git branch
git branch -l
git branch --list | List the local branches, the current one is prefixed with * . |
|
git branch --column | Display branch listing in columns.
Can be set as default with the configuration variables column.branch or column.ui . |
|
git branch --sort=<key> | Sort based on the key given.
Prefix - to sort in descending order of the value.
The default sort can be defined with the configuration variable branch.sort . |
|
git branch -v
git branch --verbose | List the local branches, indicating sha1 and commit subject line for each head, and relationship to upstream branch (if any). | |
git branch -vv | The same with path of the linked worktree (if any) and the name of the upstream branch. | |
git branch -a
git branch --all | List the local and remote branches. | |
git branch --contains <commit> | Only list branches which contain the specified commit. | |
git branch --no-contains <commit> | Only list branches which do not contain the specified commit. | |
git branch -m <new name>
git branch --move <new name> | Rename the current branch, with its config and reflog. | |
git branch -m <new name> -f
git branch --move <new name> --force
git branch -M <new name> | Rename the current branch, with its config and reflog, even if a branch <new name> already exists. | |
git branch -c <name>
git branch --copy <name> | Copy the current branch, with its config and reflog. | |
git branch -c <name> -f
git branch --copy <name> --force
git branch -C <name> | Copy the current branch, with its config and reflog, even if a branch <name> already exists. | |
git switch’s man page | Switch branches. | |
git checkout’s man page | Switch branches or restore working tree files. | |
git checkout -q
git checkout --quiet | Quiet, suppress feedback messages. | |
git checkout --progress | Enable progress reporting even if not attached to a terminal. | |
git switch <branch>
git checkout <branch> | Switch to another branch. | |
git switch -
git checkout - | Switch back to the previous branch. | |
git checkout -f
git checkout --force | When switching branches, proceed even if the index or the working tree differs from HEAD, and even if there are untracked files in the way. This is used to throw away local changes and any untracked files or directories that are in the way. | |
git switch -c <branch>
git switch --create <branch>
git checkout -b <branch> | Create a new branch (while leaving the working tree unchanged) and check it out. | |
git switch -C <branch>
git switch --force-create <branch>
git checkout -B <branch> | The same except that if <branch> already exists, it is reset to the current head. | |
git switch -c <branch> <starting point>
git switch --create <branch> <starting point>
git checkout -b <branch> <starting point> | Create a new branch from a starting point and check it out. | |
git switch -C <branch> <starting point>
git switch --force-create <branch> <starting point>
git checkout -B <branch> <starting point> | The same except that if <branch> already exists, it is reset to <starting point>. | |
git switch -d <commit>
git switch --detach <commit>
git checkout --detach <commit> | Switch to a commit for inspection and discardable experiments. (Just do something such as git switch main to leave the detached head state.) |
|
git branch -d <branch>
git branch --delete <branch> | Delete a local branch. The branch must be fully merged in its upstream branch. | |
git branch -d -f <branch>
git branch --delete --force <branch>
git branch -D <branch> | Delete a local branch even if it has not been merged. | |
git push origin --delete <branch> | Delete a branch in the remote repository. | |
git merge’s man page | Join two or more development histories together. | |
git merge <branch> | Merge a branch in the workspace. | |
git merge <branch> --no-commit --no-ff | Merge a branch in the workspace without committing. | |
| After a conflict resolution, add the fixed files to the index and continue the merge. | |
git merge --abort | Cancel the current ongoing merge (if there was a conflict or with the --no-commit flag). |
|
git merge --squash | Update the working tree and index by merging the branch, but the commit is not performed (the commit will have to be done later). This can be used to merge one or several branches in a single commit. | |
git merge -s ours
git merge --strategy=ours | Create a merge that keeps the current branch head (i.e. it ignores all the changes done on the other branches). | |
git merge --allow-unrelated-histories | Allow merging histories of two projects that started their lives independently. | |
git merge -m <msg> | Define the message of the merge commit. | |
git cherry-pick’s man page | Apply the changes introduced by some existing commits. | |
git cherry-pick <commit> | Cherry pick a commit and commit it. | |
git cherry-pick <commit> -m 1
git cherry-pick <commit> --mainline 1 | Use to cherry pick a merge commit, -m 1 indicates to replay the change relative to the first parent. |
|
git cherry-pick <commit> -n
git cherry-pick <commit> --no-commit | Cherry pick a commit, but does not commit it. | |
git mergetool’s man page | Run merge conflict resolution tools to resolve merge conflicts. | |
git mergetool | Perform merge conflict resolution with a tool. | |
git mergetool --tool-help | List the supported merge tools, indicating which ones are available. | |
git checkout --patch <commit> . | Does not change the current branch.
Bring and manually accept the files of <commit> in the index. |
git tag’s man page | Create, list, delete or verify a tag object signed with GPG. |
git tag
git tag -l
git tag --list | List the existing tags. |
git tag -l <wildcard>
git tag --list <wildcard> | List the existing tags matching a wildcard, e.g.:
git tag --list "*.RELEASE" |
git tag --contains <commit> | Only list tags which contain the specified commit. |
git tag --no-contains <commit> | Only list tags which do not contain the specified commit. |
git tag -a <tagname> -m <tagcomment> | Create an annotated tag. |
git tag <tagname> | Create a lightweight tag. |
git tag -a <tagname> <commitchecksum> | Create a tag corresponding to a past commit. |
git push origin <tagname> | Push a tag on the remote. |
git push origin --tags | Push all tags on the remote. |
git tag -d <tagname> | Delete a tag locally. |
git push origin :refs/tags/<tagname>
git push origin --delete <tagname> | Delete a tag on the remote. |
git bisect’s man page | Use binary search to find the commit that introduced a bug. |
git show-ref’s man page | List the references in the local repository and their associated commits. |
git hash-object’s man page | Compute object ID and optionally create an object from a file. |
git hash-object <file> | Compute the file hash. |
git hash-object --stdin | Read data to hash from standard input. |
git hash-object -w | Actually write the object into the object database. |
git hash-object -t <type> | Specify the type (default: "blob"). |
git cat-file’s man page | Provide content or type and size information for repository objects. |
git cat-file -t 47281c0d4fae6480a95aced8887244544feba9c5 | Display the type of a repository object. |
git cat-file -p 47281c0d4fae6480a95aced8887244544feba9c5 | Display the content (pretty printed) of a repository object. |
git count-objects’s man page | Display the number of unpacked object files and disk space consumed by them. |
git grep’s man page | Print lines matching a pattern. |
git update-ref’s man page | Update the object name stored in a ref safely. |
git update-ref -d <ref> | Delete reference <ref> in the local repository. |
git gc’s man page | Compress file revision and remove unreachable objects. |
git fsck’s man page | Verifies the connectivity and validity of the objects in the database. |
git maintenance’s man page | Run tasks to optimise Git repository data. |
git maintenance register | Initialize Git config values so any scheduled maintenance will start running on this repository. |
git maintenance unregister | Remove the current repository from background maintenance. |
git submodule’s man page | Initialize, update or inspect submodules. |
git submodule add https://github.com/chaconinc/DbConnector | Add an existing Git repository as a submodule. |
git config’s man page | Get and set repository or global options.
When reading, the values are read from the system, global, and repository local configuration files by default, and options --system , --global , --local , --worktree and --file <filename> can be used to tell the command to read from only that location.
When writing, the new value is written to the repository local configuration file by default, and options --system , --global , --worktree , --file <filename> can be used to tell the command to write to that location (we can use --local but that is the default). |
git config get user.name
old syntax: git config --get user.name | Display the value of user.name . |
git config get --all user.name
old syntax: git config --get-all user.name | Display all the values of user.name . |
git config get --all --show-origin user.name
old syntax: git config --get-all --show-origin user.name | Display all the values of user.name with their origin. |
old syntax:
git config -l
git config --list | List all variables set in config file with their values. |
old syntax:
git config --global --list | Display the global configuration. |
git config --show-origin | Augment the output of all queried config options with the origin type and the actual origin. |
git config --show-scope | Augment the output of all queried config options with the scope of that value. |
git config -l --show-origin | List all variables set in config file with, their values, and the files in which they are defined. |
git config --global --edit | Open an editor to edit the global configuration file. |
old syntax:
git config --global user.name "John Doe"
git config set --global user.name "John Doe" | Set the name in the global configuration file. |
git config set --global user.email johndoe@example.com
old syntax: git config --global user.email johndoe@example.com | Set the email in the global configuration file. |
git config set --system user.email johndoe@example.com
old syntax: git config --system user.email johndoe@example.com | Set the email in the system-wide configuration file. |
git config set user.name "John Doe"
git config set --local user.name "John Doe"
old syntax: git config user.name "John Doe"
git config --local user.name "John Doe" | Set the name in the local repository configuration file. |
git config unset <name>
git config unset --local <name>
old syntax: git config --unset <name>
git config --local --unset <name> | Remove the line matching the key <name> from the configuration file. |
old syntax:
git config --remove-section <name> | Remove the section <name> from the configuration file. |
old syntax:
git config --replace-all | Default behaviour is to replace at most one line. This replaces all lines matching the key. |
old syntax:
git config --add | Adds a new line to the option without altering any existing values. |
git config --rename-section <old-name> <new-name> | Rename the section <old-name> into <new-name> . |
git config --global alias.ac "commit -am" | Define an alias. The alias can be used as git ac "Fixed my bug" . |
git config --global alias.ac "!myscript.sh" | Define an alias of a shell command. |
include
and includeIf
sections allow to include config directives from another source.
It is possible to ignore files/directories in a given directory by listing them in the
.gitignore
file of that directory.
In order to commit an empty directory, create a
.gitkeep
file in it. This is not defined by Git, but it is a common usage.
To remove the last commit locally and on the remote repository (nobody should have pulled meanwhile)
#> git reset --hard HEAD^
|
git rebase’s man page | Reapply commits on top of another base tip. |
git rebase --interactive
git rebase -i | Start an interactive rebase: the list of commits is generated and the user can edit them. |
git rebase --continue | Restart the rebasing process after having resolved a merge conflict. |
git rebase --abort | Abort the rebase operation and reset HEAD to the original branch. |
git rebase --quit | Abort the rebase operation but HEAD is not reset back to the original branch. |
git rebase --update-refs | Automatically force-update any branches that point to commits that are being rebased. |
git rebase --exec <cmd>
git rebase -x <cmd> | Append exec <cmd> after each line creating a commit in the final history. |
git replay’s man page | Replay commits on a new base, works with bare repos too. |
git rerere’s man page | Reuse recorded resolution of conflicted merges.
Must be enabled by git config --global rerere.enabled true .
git merge and git commit automatically invoke git rerere , so there is no need to call it directly. |
git commit --fixup=<commit> | Create a fixup! commit which changes the content of <commit> but leaves its log message untouched. |
git commit --fixup=amend:<commit> | Similar but create an amend! commit which also replaces the log message of <commit> with the log message of the amend! commit. |
git commit --fixup=reword:<commit>
git commit --fixup=amend:<commit> --only | Create an amend! commit which replaces the log message of <commit> with its own log message but makes no changes to the content of <commit>. |
git commit --squash=<commit> | Construct a commit message for use with rebase --autosquash . The commit message subject line is taken from the specified commit with a prefix of squash! . |
git rebase --autosquash | When the commit log message begins with squash! … or fixup! … or amend! … , and there is already a commit in the todo list that matches the same … , automatically modify the todo list of rebase -i , so that the commit marked for squashing comes right after the commit to be modified, and change the action of the moved commit from pick to squash or fixup or fixup -C respectively.
A commit matches the … if the commit subject matches, or if the … refers to the commit’s hash. As a fall-back, partial matches of the commit subject work, too. |
git credential’s man page | Retrieve and store user credentials | |
git credential fill | retrieve the credentials
|
|
git credential reject | forget the credentials
|
|
git credential approve | record credentials (the OS will display a mesage for login in)
|
git column’s man page | Display data in columns. |
Changing the author of old commits
-
git rebase -i <commit>
where <commit> is the commit before the first one to be edited. Usegit rebase -i --root
if the first commit must be edited. -
In the list of commits, change
pick
toedit
for the commits to be edited. -
When the interactive rebase pauses,
git commit --amend --author="Author Name <email@address.com>"
, thengit rebase --continue
. -
git push -f
.
Update a hierarchy of workspaces
find . -type d -name ".git" -execdir git pull -v \; |
Script to mirror a repository in another (to be completed to support GitHub)
#!/bin/bash
|
Script to find the nearest version of a file (
$1
) compared to a local file ($2
)
#!/bin/bash
|
Git on Windows
-
To properly manage newlines
#> git config --global core.autocrlf true
\r\n
if on Windows.
When committing code, this converts line endings to Unix-style\n
.
Git SVN
-
git svn’s man page
-
Create a Git repo linked to a SVN repo
git svn clone -s file:///cygdrive/m/laurent_svn_repository/homepage
-
Fetch revisions from the SVN repo of the current HEAD and rebase the current (uncommitted to SVN) work against it
git svn rebase
Fix a repository created by Eclipse
-
Eclipse may configure a repository tracking only the current and main branches:
#> git config --local --list
…
remote.origin.fetch=+refs/heads/55-proper-support-of-removed-articles:refs/remotes/origin/55-proper-support-of-removed-articles
remote.origin.fetch=+refs/heads/main:refs/remotes/origin/main
… -
to fix it and track all branches:
#> git config --local --replace-all remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
-
then
git fetch
will fetch all branches.
Git BASH on Windows
-
The home directory is defined by the
%HOME%
Windows environment variable (which is not used by Windows, so it is safe to set/modify it). -
To display the remote repository in the browser:
start `git remote get-url origin`
. -
To run Python:
winpty python
-
To display an HTML file:
start report.html
My configuration
-
Set up prompting for autocorrect
#> git config --global help.autocorrect prompt