git version 2.25.1 Python 3.8.10
PATH=\$PATH:$installDir
" >> ~/.bashrcFlag | Usage |
---|---|
-f --force | git filter-repo does irreversible rewriting of history. It runs internal "safety checks" before actually altering data to avoid making changes to a repo for which the user doesn't have a good backup. But since no check is perfect, consider this --force flag as a Please proceed and alter datarequest (details). |
--path fileOrDir --path-match fileOrDir |
|
workDir=$(mktemp -d -p /run/user/$(id -u) tmp.git.XXXXXXXX); cd "$workDir" git init echo 'hello world' > fileToKeep; echo 'AZERTYUIOP' > fileToDelete; git add fileToKeep fileToDelete; git commit -m 'initial version' echo 'hello again' >> fileToKeep; git add fileToKeep; git commit -m 'keep 1' echo 'QSDFGHJKLM' >> fileToDelete; git add fileToDelete; git commit -m 'delete 1' echo 'how are you-yau de poêle ?' >> fileToKeep; git add fileToKeep; git commit -m 'keep 2' echo 'WXCVBNM' >> fileToDelete; git add fileToDelete; git commit -m 'delete 2' echo 'GIT LOG BEFORE'; git log --oneline git filter-repo --force --invert-paths --path fileToDelete echo 'GIT LOG AFTER'; git log --oneline cd ..; [ -d "$workDir" ] && rm -rf "$workDir"
GIT LOG BEFORE bd56afe (HEAD -> master) delete 2 3c05efe keep 2 6b7cf6e delete 1 b2d57ec keep 1 931abf3 initial version
Parsed 5 commits New history written in 0.01 seconds; now repacking/cleaning... Repacking your repo and cleaning out old unneeded objects HEAD is now at 614055a keep 2 Enumerating objects: 9, done. Counting objects: 100% (9/9), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (9/9), done. Total 9 (delta 1), reused 0 (delta 0) Completely finished after 0.04 seconds.
GIT LOG AFTER 614055a (HEAD -> master) keep 2 8ed0153 keep 1 d2a37c7 initial version
This command... | ... can safely be replaced with... |
---|---|
git checkout myBranch | git switch myBranch |
git checkout someFile | git restore someFile |
Due to data safety and performance issues, use of git filter-branch is not recommended anymore. Consider using an alternate tool such as git filter-repo (source).
Rewrite Git revision history by rewriting the specified branches, applying custom filters on each revision. Those filters can modify each tree (e.g. removing a file or running a perl rewrite on all files) or information about each commit. Otherwise, all information (including original commit times or merge information) will be preserved.Flag | Usage |
---|---|
--all | instructs to rewrite all commits |
--prune-empty | Some filters will generate empty commits that leave the tree untouched. This option instructs git filter-branch to remove such commits if they have exactly one or zero non-pruned (untouched ?) parents. |
--subdirectory-filter myDir | Only look at the history which touches the myDir directory. The result will contain that directory (and only that) as its project root. |
git fetch [options] [ [repository] [refspec] ... ]
Flag | Usage |
---|---|
refspec | Specifies which refs to fetch and which local refs to update. Format : +source:destination
Typically : remoteBranch:localBranch
|
$branchName
branch on repo2 :
$branchName
on repo2 :
$branchName
from repo2 :
atomic commitusage
There's an exception to this : if you have changed many files at once to
Flag | Usage |
---|---|
-A --all | add, modify, and remove index entries to match the working tree |
-i --interactive | enter the interactive mode where you can pick which changes to add into the index (detailed example) |
-p --patch | actually runs add --interactive and directly jumps to the patch subcommand (detailed example) |
-u --update | The manual says : Update the index just where it already has an entry matching fileToStage, i.e. this allows staging fileToStage while this file has already been deleted without telling Git first with git rm fileToStage |
git show otherBranch:myFile
This is equivalent to git checkout otherBranch && cat myFile, but better
Flag | Usage |
---|---|
-d branch --delete branch :branch |
delete the remote branch branch (example) |
-u --set-upstream | For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git pull and other commands. |
git push -u remote localBranchName
There are some additional subtleties about this, regarding local / remote branch names, pushing / pulling, ... (read here)You may have to create + push a local branch on a remote repository when working on a "bugfix" (or whatever) branch and sharing your work with colleagues. This means that :
However, it is possible to proceed with a merge squash :
git pull = git fetch + git merge FETCH_HEAD (details)
Flag | Usage |
---|---|
-f --force | When git fetch is used with remoteBranch:localBranch refspec, it refuses to update the local branch localBranch unless the remote branch remoteBranch it fetches is a descendant of localBranch. This option overrides that check. |
Flag | Usage | Highlight (try it) | |
---|---|---|---|
spaces and TABS ? |
changes ? | ||
(none) | defaults to -p / -u / --patch, which outputs a combined diff | Yes | whole line containing change |
--color-words | Instead of coloring changed lines, color changed words. | No | changed word |
--word-diff | Like --color-words : color changed words but also add some {++} and [--] to highlight added/removed words for monochrome terminals. | No | changed word with brackets and +/- |
Spaces
, TABS
, and various flavors of git-diff :line 2
/line 2
/' testFile; sed -ri 's/line 4
/LINE 4
/' testFile; sed -ri 's/line 6
/line\t6
/' testFile; echo >> testFile; for gitDiffOption in '' '--color-words' '--word-diff'; do echo -e "\nRESULT OF 'git diff $gitDiffOption' :"; git diff $gitDiffOption; done; cd "$previousDir"; rm -rf "$tmpDir"Flag | Usage |
---|---|
-a --annotate | make an annotated tag object |
-d tagName --delete tagName |
delete tag tagName |
|
|
git log missingFile fatal: ambiguous argument 'missingFile': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]' git log -- missingFile (normal git log output)
Flag | Usage |
---|---|
--abbrev-commit | Show short commit IDs instead of the 40-byte hexadecimal values |
--after=date --since=date | Show commits more recent than date |
--all | Show all commits, regardless of the branch currently checked out |
--before=date --until=date | Show commits older than date |
--branches=myBranch | (see also) |
--color=always | enable color for the whole output |
--decorate | Print out the ref names of any commits that are shown : HEAD, branch name (such as master), ... |
--follow | Continue listing the history of a file beyond renames (works only for a single file) |
-G regex | (approximately) like -S, but with a regex rather than a string |
--graph | Draw a text-based graphical representation of the commit history on the left hand side of the output (example) |
--name-only | Show only names of changed files See : How to list all files altered in a branch ? |
--name-status |
|
--oneline | Shorthand for --pretty=oneline --abbrev-commit |
-p, -u, --patch | Generate patch |
--pretty --pretty=format --format=format |
Pretty-print the contents of the commit logs in a given format, where format can be a custom format or one of :
|
--stat | Show number of insertions / deletions and name of files affected by each commit |
-S string |
|
--tags[=pattern] | display tags matching pattern (if omitted, show all) |
ef432527 2018-10-07 12:19:27 +0200 (12 days ago) an interesting commit message b69f1c61 2018-10-05 13:02:27 +0200 (13 days ago) an even more interesting commit message 5cd5196f 2018-10-05 10:19:42 +0200 (2 weeks ago) committing makes my day !
%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n
' --abbrev-commit --date=relative --branchesless -p "format:<string>$"
' git logstash@{0}: WIP on master: ce9100b a great commit message stash@{1}: WIP on master: 0765a47 another GREAT commit message stash@{2}: WIP on master: cfc2b09 this one is not as great as the others, but still good, though
stash@{0}
is the latest stash.someFile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Dropped refs/stash@{0} (20f3a793f379e266cde2131e740591ae186fc188) don't know (yet!) what this ID (commit ID?) refers to
stash@{0}: WIP on master: 0765a47 another GREAT commit message decremented stash IDs stash@{1}: WIP on master: cfc2b09 this one is not as great as the others, but still good, though
Dropped refs/stash@{1} (6615a1409aa713cc8ec9a8c6a8277b19e06cacc7)
stash@{0}: WIP on master: 0765a47 another GREAT commit message decremented stash IDs
git gc (garbage collection) runs a number of housekeeping tasks within the current repository, such as compressing file revisions (to reduce disk space and increase performance) and removing unreachable objects which may have been created from prior invocations of git add.
Users are encouraged to run this task on a regular basis within each repository to maintain good disk space utilization and good operating performance.
Enumerating objects: 30475, done. Counting objects: 100% (30475/30475), done. Delta compression using up to 2 threads Compressing objects: 100% (8783/8783), done. Writing objects: 100% (30475/30475), done. Total 30475 (delta 21751), reused 30115 (delta 21522)