This command... | ... can safely be replaced with... |
---|---|
git checkout myBranch | git switch myBranch |
git checkout someFile | git restore someFile |
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
|
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) |
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 |
-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 |
|
list existing tags |
git log has options to filter which commits will be listed : see man git log + search Commit Limiting.
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) |
--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) |
--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 | arrived here via How to view 'git log' of a renamed file ?. The description of '-S' in the manual isn't very helpful in this context and looks like a nice useful hack |
If myFile is not in the working tree anymore (i.e. deleted + committed), you can still view its history with :
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 !
git log variants : git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' --abbrev-commit --date=relative --branches https://stackoverflow.com/questions/2421011/output-of-git-branch-in-tree-like-fashion#answer-2421063
stash@{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)