http://schacon.github.io/git/git-diff.html

 

git-diff(1) Manual Page

NAME

git-diff - Show changes between commits, commit and working tree, etc

SYNOPSIS

git diff [options] [<commit>] [--] [<path>…] git diff [options] --cached [<commit>] [--] [<path>…] git diff [options] <commit> <commit> [--] [<path>…] git diff [options] [--no-index] [--] <path> <path>

DESCRIPTION

Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, or changes between two files on disk.

git diff [--options] [--] [<path>…]

This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell git to further add to the index but you still haven’t. You can stage these changes by using git-add(1).

If exactly two paths are given and at least one points outside the current repository, git diff will compare the two files / directories. This behavior can be forced by --no-index.

git diff [--options] --cached [<commit>] [--] [<path>…]

This form is to view the changes you staged for the next commit relative to the named <commit>. Typically you would want comparison with the latest commit, so if you do not give <commit>, it defaults to HEAD. If HEAD does not exist (e.g. unborned branches) and <commit> is not given, it shows all staged changes. --staged is a synonym of --cached.

git diff [--options] <commit> [--] [<path>…]

This form is to view the changes you have in your working tree relative to the named <commit>. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch.

git diff [--options] <commit> <commit> [--] [<path>…]

This is to view the changes between two arbitrary <commit>.

git diff [--options] <commit>..<commit> [--] [<path>…]

This is synonymous to the previous form. If <commit> on one side is omitted, it will have the same effect as using HEAD instead.

git diff [--options] <commit>...<commit> [--] [<path>…]

This form is to view the changes on the branch containing and up to the second <commit>, starting at a common ancestor of both <commit>. "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B". You can omit any one of <commit>, which has the same effect as using HEAD instead.

Just in case if you are doing something exotic, it should be noted that all of the <commit> in the above description, except in the last two forms that use ".." notations, can be any <tree>. The third form (git diff <commit> <commit>) can also be used to compare two <blob> objects.

For a more complete list of ways to spell <commit>, see "SPECIFYING REVISIONS" section in gitrevisions(7). However, "diff" is about comparing two endpoints, not ranges, and the range notations ("<commit>..<commit>" and "<commit>...<commit>") do not mean a range as defined in the "SPECIFYING RANGES" section in gitrevisions(7).

OPTIONS

-p-u--patch

Generate patch (see section on generating patches). This is the default.

-U<n>--unified=<n>

Generate diffs with <n> lines of context instead of the usual three. Implies -p.

--raw

Generate the raw format.

--patch-with-raw

Synonym for -p --raw.

--patience

Generate a diff using the "patience diff" algorithm.

--stat[=<width>[,<name-width>[,<count>]]]

Generate a diffstat. You can override the default output width for 80-column terminal by --stat=<width>. The width of the filename part can be controlled by giving another width to it separated by a comma. By giving a third parameter <count>, you can limit the output to the first <count> lines, followed by  if there are more.

These parameters can also be set individually with --stat-width=<width>, --stat-name-width=<name-width> and --stat-count=<count>.

--numstat

Similar to --stat, but shows number of added and deleted lines in decimal notation and pathname without abbreviation, to make it more machine friendly. For binary files, outputs two - instead of saying 0 0.

--shortstat

Output only the last line of the --stat format containing total number of modified files, as well as number of added and deleted lines.

--dirstat[=<param1,param2,…>]

Output the distribution of relative amount of changes for each sub-directory. The behavior of --dirstat can be customized by passing it a comma separated list of parameters. The defaults are controlled by the diff.dirstat configuration variable (see git-config(1)). The following parameters are available:

changes

Compute the dirstat numbers by counting the lines that have been removed from the source, or added to the destination. This ignores the amount of pure code movements within a file. In other words, rearranging lines in a file is not counted as much as other changes. This is the default behavior when no parameter is given.

lines

Compute the dirstat numbers by doing the regular line-based diff analysis, and summing the removed/added line counts. (For binary files, count 64-byte chunks instead, since binary files have no natural concept of lines). This is a more expensive --dirstat behavior than the changes behavior, but it does count rearranged lines within a file as much as other changes. The resulting output is consistent with what you get from the other --*stat options.

files

Compute the dirstat numbers by counting the number of files changed. Each changed file counts equally in the dirstat analysis. This is the computationally cheapest --dirstat behavior, since it does not have to look at the file contents at all.

cumulative

Count changes in a child directory for the parent directory as well. Note that when using cumulative, the sum of the percentages reported may exceed 100%. The default (non-cumulative) behavior can be specified with the noncumulative parameter.

<limit>

An integer parameter specifies a cut-off percent (3% by default). Directories contributing less than this percentage of the changes are not shown in the output.

Example: The following will count changed files, while ignoring directories with less than 10% of the total amount of changed files, and accumulating child directory counts in the parent directories: --dirstat=files,10,cumulative.

--summary

Output a condensed summary of extended header information such as creations, renames and mode changes.

--patch-with-stat

Synonym for -p --stat.

-z

When --raw, --numstat, --name-only or --name-status has been given, do not munge pathnames and use NULs as output field terminators.

Without this option, each pathname output will have TAB, LF, double quotes, and backslash characters replaced with \t, \n, \", and \\, respectively, and the pathname will be enclosed in double quotes if any of those replacements occurred.

--name-only

Show only names of changed files.

--name-status

Show only names and status of changed files. See the description of the --diff-filter option on what the status letters mean.

--submodule[=<format>]

Chose the output format for submodule differences. <format> can be one of short and log. short just shows pairs of commit names, this format is used when this option is not given. log is the default value for this option and lists the commits in that commit range like the summary option of git-submodule(1) does.

--color[=<when>]

Show colored diff. The value must be always (the default for <when>), never, or auto. The default value is never. It can be changed by the color.ui and color.diff configuration settings.

--no-color

Turn off colored diff. This can be used to override configuration settings. It is the same as --color=never.

--word-diff[=<mode>]

Show a word diff, using the <mode> to delimit changed words. By default, words are delimited by whitespace; see --word-diff-regex below. The <mode> defaults to plain, and must be one of:

color

Highlight changed words using only colors. Implies --color.

plain

Show words as [-removed-] and {added}. Makes no attempts to escape the delimiters if they appear in the input, so the output may be ambiguous.

porcelain

Use a special line-based format intended for script consumption. Added/removed/unchanged runs are printed in the usual unified diff format, starting with a +/-/` ` character at the beginning of the line and extending to the end of the line. Newlines in the input are represented by a tilde ~ on a line of its own.

none

Disable word diff again.

Note that despite the name of the first mode, color is used to highlight the changed parts in all modes if enabled.

--word-diff-regex=<regex>

Use <regex> to decide what a word is, instead of considering runs of non-whitespace to be a word. Also implies --word-diff unless it was already enabled.

Every non-overlapping match of the <regex> is considered a word. Anything between these matches is considered whitespace and ignored(!) for the purposes of finding differences. You may want to append |[^[:space:]] to your regular expression to make sure that it matches all non-whitespace characters. A match that contains a newline is silently truncated(!) at the newline.

The regex can also be set via a diff driver or configuration option, see gitattributes(1) or git-config(1). Giving it explicitly overrides any diff driver or configuration setting. Diff drivers override configuration settings.

--color-words[=<regex>]

Equivalent to --word-diff=color plus (if a regex was specified) --word-diff-regex=<regex>.

--no-renames

Turn off rename detection, even when the configuration file gives the default to do so.

--check

Warn if changes introduce whitespace errors. What are considered whitespace errors is controlled by core.whitespace configuration. By default, trailing whitespaces (including lines that solely consist of whitespaces) and a space character that is immediately followed by a tab character inside the initial indent of the line are considered whitespace errors. Exits with non-zero status if problems are found. Not compatible with --exit-code.

--full-index

Instead of the first handful of characters, show the full pre- and post-image blob object names on the "index" line when generating patch format output.

--binary

In addition to --full-index, output a binary diff that can be applied with git-apply.

--abbrev[=<n>]

Instead of showing the full 40-byte hexadecimal object name in diff-raw format output and diff-tree header lines, show only a partial prefix. This is independent of the --full-index option above, which controls the diff-patch output format. Non default number of digits can be specified with --abbrev=<n>.

-B[<n>][/<m>]--break-rewrites[=[<n>][/<m>]]

Break complete rewrite changes into pairs of delete and create. This serves two purposes:

It affects the way a change that amounts to a total rewrite of a file not as a series of deletion and insertion mixed together with a very few lines that happen to match textually as the context, but as a single deletion of everything old followed by a single insertion of everything new, and the number m controls this aspect of the -B option (defaults to 60%). -B/70% specifies that less than 30% of the original should remain in the result for git to consider it a total rewrite (i.e. otherwise the resulting patch will be a series of deletion and insertion mixed together with context lines).

When used with -M, a totally-rewritten file is also considered as the source of a rename (usually -M only considers a file that disappeared as the source of a rename), and the number n controls this aspect of the -B option (defaults to 50%). -B20% specifies that a change with addition and deletion compared to 20% or more of the file’s size are eligible for being picked up as a possible source of a rename to another file.

-M[<n>]--find-renames[=<n>]

Detect renames. If n is specified, it is a threshold on the similarity index (i.e. amount of addition/deletions compared to the file’s size). For example, -M90% means git should consider a delete/add pair to be a rename if more than 90% of the file hasn’t changed.

-C[<n>]--find-copies[=<n>]

Detect copies as well as renames. See also --find-copies-harder. If n is specified, it has the same meaning as for -M<n>.

--find-copies-harder

For performance reasons, by default, -C option finds copies only if the original file of the copy was modified in the same changeset. This flag makes the command inspect unmodified files as candidates for the source of copy. This is a very expensive operation for large projects, so use it with caution. Giving more than one -C option has the same effect.

-D--irreversible-delete

Omit the preimage for deletes, i.e. print only the header but not the diff between the preimage and /dev/null. The resulting patch is not meant to be applied with patch nor git apply; this is solely for people who want to just concentrate on reviewing the text after the change. In addition, the output obviously lack enough information to apply such a patch in reverse, even manually, hence the name of the option.

When used together with -B, omit also the preimage in the deletion part of a delete/create pair.

-l<num>

The -M and -C options require O(n^2) processing time where n is the number of potential rename/copy targets. This option prevents rename/copy detection from running if the number of rename/copy targets exceeds the specified number.

--diff-filter=[(A|C|D|M|R|T|U|X|B)…[*]]

Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (i.e. regular file, symlink, submodule, …) changed (T), are Unmerged (U), are Unknown (X), or have had their pairing Broken (B). Any combination of the filter characters (including none) can be used. When * (All-or-none) is added to the combination, all paths are selected if there is any file that matches other criteria in the comparison; if there is no file that matches other criteria, nothing is selected.

-S<string>

Look for differences that introduce or remove an instance of <string>. Note that this is different than the string simply appearing in diff output; see the pickaxe entry in gitdiffcore(7) for more details.

-G<regex>

Look for differences whose added or removed line matches the given <regex>.

--pickaxe-all

When -S or -G finds a change, show all the changes in that changeset, not just the files that contain the change in <string>.

--pickaxe-regex

Make the <string> not a plain string but an extended POSIX regex to match.

-O<orderfile>

Output the patch in the order specified in the <orderfile>, which has one shell glob pattern per line.

-R

Swap two inputs; that is, show differences from index or on-disk file to tree contents.

--relative[=<path>]

When run from a subdirectory of the project, it can be told to exclude changes outside the directory and show pathnames relative to it with this option. When you are not in a subdirectory (e.g. in a bare repository), you can name which subdirectory to make the output relative to by giving a <path> as an argument.

-a--text

Treat all files as text.

--ignore-space-at-eol

Ignore changes in whitespace at EOL.

-b--ignore-space-change

Ignore changes in amount of whitespace. This ignores whitespace at line end, and considers all other sequences of one or more whitespace characters to be equivalent.

-w--ignore-all-space

Ignore whitespace when comparing lines. This ignores differences even if one line has whitespace where the other line has none.

--inter-hunk-context=<lines>

Show the context between diff hunks, up to the specified number of lines, thereby fusing hunks that are close to each other.

--exit-code

Make the program exit with codes similar to diff(1). That is, it exits with 1 if there were differences and 0 means no differences.

--quiet

Disable all output of the program. Implies --exit-code.

--ext-diff

Allow an external diff helper to be executed. If you set an external diff driver with gitattributes(5), you need to use this option with git-log(1) and friends.

--no-ext-diff

Disallow external diff drivers.

--textconv--no-textconv

Allow (or disallow) external text conversion filters to be run when comparing binary files. See gitattributes(5) for details. Because textconv filters are typically a one-way conversion, the resulting diff is suitable for human consumption, but cannot be applied. For this reason, textconv filters are enabled by default only for git-diff(1) and git-log(1), but not for git-format-patch(1) or diff plumbing commands.

--ignore-submodules[=<when>]

Ignore changes to submodules in the diff generation. <when> can be either "none", "untracked", "dirty" or "all", which is the default Using "none" will consider the submodule modified when it either contains untracked or modified files or its HEAD differs from the commit recorded in the superproject and can be used to override any settings of the ignore option in git-config(1) or gitmodules(5). When "untracked" is used submodules are not considered dirty when they only contain untracked content (but they are still scanned for modified content). Using "dirty" ignores all changes to the work tree of submodules, only changes to the commits stored in the superproject are shown (this was the behavior until 1.7.0). Using "all" hides all changes to submodules.

--src-prefix=<prefix>

Show the given source prefix instead of "a/".

--dst-prefix=<prefix>

Show the given destination prefix instead of "b/".

--no-prefix

Do not show any source or destination prefix.

For more detailed explanation on these common options, see also gitdiffcore(7).

<path>…

The <paths> parameters, when given, are used to limit the diff to the named paths (you can give directory names and get diff for all files under them).

Raw output format

The raw output format from "git-diff-index", "git-diff-tree", "git-diff-files" and "git diff --raw" are very similar.

These commands all compare two sets of things; what is compared differs:

git-diff-index <tree-ish>

compares the <tree-ish> and the files on the filesystem.

git-diff-index --cached <tree-ish>

compares the <tree-ish> and the index.

git-diff-tree [-r] <tree-ish-1> <tree-ish-2> [<pattern>…]

compares the trees named by the two arguments.

git-diff-files [<pattern>…]

compares the index and the files on the filesystem.

The "git-diff-tree" command begins its output by printing the hash of what is being compared. After that, all the commands print one output line per changed file.

An output line is formatted this way:

in-place edit :100644 100644 bcd1234... 0123456... M file0 copy-edit :100644 100644 abcd123... 1234567... C68 file1 file2 rename-edit :100644 100644 abcd123... 1234567... R86 file1 file3 create :000000 100644 0000000... 1234567... A file4 delete :100644 000000 1234567... 0000000... D file5 unmerged :000000 000000 0000000... 0000000... U file6

That is, from the left to the right:

  1. a colon.

  2. mode for "src"; 000000 if creation or unmerged.

  3. a space.

  4. mode for "dst"; 000000 if deletion or unmerged.

  5. a space.

  6. sha1 for "src"; 0{40} if creation or unmerged.

  7. a space.

  8. sha1 for "dst"; 0{40} if creation, unmerged or "look at work tree".

  9. a space.

  10. status, followed by optional "score" number.

  11. a tab or a NUL when -z option is used.

  12. path for "src"

  13. a tab or a NUL when -z option is used; only exists for C or R.

  14. path for "dst"; only exists for C or R.

  15. an LF or a NUL when -z option is used, to terminate the record.

Possible status letters are:

  • A: addition of a file

  • C: copy of a file into a new one

  • D: deletion of a file

  • M: modification of the contents or mode of a file

  • R: renaming of a file

  • T: change in the type of the file

  • U: file is unmerged (you must complete the merge before it can be committed)

  • X: "unknown" change type (most probably a bug, please report it)

Status letters C and R are always followed by a score (denoting the percentage of similarity between the source and target of the move or copy), and are the only ones to be so.

<sha1> is shown as all 0’s if a file is new on the filesystem and it is out of sync with the index.

Example:

:100644 100644 5be4a4...... 000000...... M file.c

When -z option is not used, TAB, LF, and backslash characters in pathnames are represented as \t, \n, and \\, respectively.

diff format for merges

"git-diff-tree", "git-diff-files" and "git-diff --raw" can take -c or --cc option to generate diff output also for merge commits. The output differs from the format described above in the following way:

  1. there is a colon for each parent

  2. there are more "src" modes and "src" sha1

  3. status is concatenated status characters for each parent

  4. no optional "score" number

  5. single path, only for "dst"

Example:

::100644 100644 100644 fabadb8... cc95eb0... 4866510... MM describe.c

Note that combined diff lists only files which were modified from all parents.

Generating patches with -p

When "git-diff-index", "git-diff-tree", or "git-diff-files" are run with a -p option, "git diff" without the --raw option, or "git log" with the "-p" option, they do not produce the output described above; instead they produce a patch file. You can customize the creation of such patches via the GIT_EXTERNAL_DIFF and the GIT_DIFF_OPTS environment variables.

What the -p option produces is slightly different from the traditional diff format:

  1. It is preceded with a "git diff" header that looks like this:

    diff --git a/file1 b/file2

    The a/ and b/ filenames are the same unless rename/copy is involved. Especially, even for a creation or a deletion, /dev/null is not used in place of the a/ or b/ filenames.

    When rename/copy is involved, file1 and file2 show the name of the source file of the rename/copy and the name of the file that rename/copy produces, respectively.

  2. It is followed by one or more extended header lines:

    old mode <mode> new mode <mode> deleted file mode <mode> new file mode <mode> copy from <path> copy to <path> rename from <path> rename to <path> similarity index <number> dissimilarity index <number> index <hash>..<hash> <mode>

    File modes are printed as 6-digit octal numbers including the file type and file permission bits.

    Path names in extended headers do not include the a/ and b/ prefixes.

    The similarity index is the percentage of unchanged lines, and the dissimilarity index is the percentage of changed lines. It is a rounded down integer, followed by a percent sign. The similarity index value of 100% is thus reserved for two equal files, while 100% dissimilarity means that no line from the old file made it into the new one.

    The index line includes the SHA-1 checksum before and after the change. The <mode> is included if the file mode does not change; otherwise, separate lines indicate the old and the new mode.

  3. TAB, LF, double quote and backslash characters in pathnames are represented as \t, \n, \" and \\, respectively. If there is need for such substitution then the whole pathname is put in double quotes.

  4. All the file1 files in the output refer to files before the commit, and all the file2 files refer to files after the commit. It is incorrect to apply each change to each file sequentially. For example, this patch will swap a and b:

    diff --git a/a b/b rename from a rename to b diff --git a/b b/a rename from b rename to a

combined diff format

Any diff-generating command can take the ‘-c` or --cc option to produce a combined diff when showing a merge. This is the default format when showing merges with git-diff(1) or git-show(1). Note also that you can give the `-m’ option to any of these commands to force generation of diffs with individual parents of a merge.

A combined diff format looks like this:

diff --combined describe.c index fabadb8,cc95eb0..4866510 --- a/describe.c +++ b/describe.c @@@ -98,20 -98,12 +98,20 @@@ return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1; } - static void describe(char *arg) -static void describe(struct commit *cmit, int last_one) ++static void describe(char *arg, int last_one) { + unsigned char sha1[20]; + struct commit *cmit; struct commit_list *list; static int initialized = 0; struct commit_name *n; + if (get_sha1(arg, sha1) < 0) + usage(describe_usage); + cmit = lookup_commit_reference(sha1); + if (!cmit) + usage(describe_usage); + if (!initialized) { initialized = 1; for_each_ref(get_name);

  1. It is preceded with a "git diff" header, that looks like this (when -c option is used):

    diff --combined file

    or like this (when --cc option is used):

    diff --cc file

  2. It is followed by one or more extended header lines (this example shows a merge with two parents):

    index <hash>,<hash>..<hash> mode <mode>,<mode>..<mode> new file mode <mode> deleted file mode <mode>,<mode>

    The mode <mode>,<mode>..<mode> line appears only if at least one of the <mode> is different from the rest. Extended headers with information about detected contents movement (renames and copying detection) are designed to work with diff of two <tree-ish> and are not used by combined diff format.

  3. It is followed by two-line from-file/to-file header

    --- a/file +++ b/file

    Similar to two-line header for traditional unified diff format, /dev/null is used to signal created or deleted files.

  4. Chunk header format is modified to prevent people from accidentally feeding it to patch -p1. Combined diff format was created for review of merge commit changes, and was not meant for apply. The change is similar to the change in the extended index header:

    @@@ <from-file-range> <from-file-range> <to-file-range> @@@

    There are (number of parents + 1) @ characters in the chunk header for combined diff format.

Unlike the traditional unified diff format, which shows two files A and B with a single column that has - (minus — appears in A but removed in B), + (plus — missing in A but added to B), or " " (space — unchanged) prefix, this format compares two or more files file1, file2,… with one file X, and shows how X differs from each of fileN. One column for each of fileN is prepended to the output line to note how X’s line is different from it.

A - character in the column N means that the line appears in fileN but it does not appear in the result. A + character in the column N means that the line appears in the result, and fileN does not have that line (in other words, the line was added, from the point of view of that parent).

In the above example output, the function signature was changed from both files (hence two - removals from both file1 and file2, plus ++ to mean one line that was added does not appear in either file1 nor file2). Also eight other lines are the same from file1 but do not appear in file2 (hence prefixed with +).

When shown by git diff-tree -c, it compares the parents of a merge commit with the merge result (i.e. file1..fileN are the parents). When shown by git diff-files -c, it compares the two unresolved merge parents with the working tree file (i.e. file1 is stage 2 aka "our version", file2 is stage 3 aka "their version").

other diff formats

The --summary option describes newly added, deleted, renamed and copied files. The --stat option adds diffstat(1) graph to the output. These options can be combined with other options, such as -p, and are meant for human consumption.

When showing a change that involves a rename or a copy, --stat output formats the pathnames compactly by combining common prefix and suffix of the pathnames. For example, a change that moves arch/i386/Makefile to arch/x86/Makefile while modifying 4 lines will be shown like this:

arch/{i386 => x86}/Makefile | 4 +--

The --numstat option gives the diffstat(1) information but is designed for easier machine consumption. An entry in --numstat output looks like this:

1 2 README 3 1 arch/{i386 => x86}/Makefile

That is, from left to right:

  1. the number of added lines;

  2. a tab;

  3. the number of deleted lines;

  4. a tab;

  5. pathname (possibly with rename/copy information);

  6. a newline.

When -z output option is in effect, the output is formatted this way:

1 2 README NUL 3 1 NUL arch/i386/Makefile NUL arch/x86/Makefile NUL

That is:

  1. the number of added lines;

  2. a tab;

  3. the number of deleted lines;

  4. a tab;

  5. a NUL (only exists if renamed/copied);

  6. pathname in preimage;

  7. a NUL (only exists if renamed/copied);

  8. pathname in postimage (only exists if renamed/copied);

  9. a NUL.

The extra NUL before the preimage path in renamed case is to allow scripts that read the output to tell if the current record being read is a single-path record or a rename/copy record without reading ahead. After reading added and deleted lines, reading up to NUL would yield the pathname, but if that is NUL, the record will show two paths.

EXAMPLES

Various ways to check your working tree

$ git diff <1> $ git diff --cached <2> $ git diff HEAD <3>

  1. Changes in the working tree not yet staged for the next commit.

  2. Changes between the index and your last commit; what you would be committing if you run "git commit" without "-a" option.

  3. Changes in the working tree since your last commit; what you would be committing if you run "git commit -a"

Comparing with arbitrary commits

$ git diff test <1> $ git diff HEAD -- ./test <2> $ git diff HEAD^ HEAD <3>

  1. Instead of using the tip of the current branch, compare with the tip of "test" branch.

  2. Instead of comparing with the tip of "test" branch, compare with the tip of the current branch, but limit the comparison to the file "test".

  3. Compare the version before the last commit and the last commit.

Comparing branches

$ git diff topic master <1> $ git diff topic..master <2> $ git diff topic...master <3>

  1. Changes between the tips of the topic and the master branches.

  2. Same as above.

  3. Changes that occurred on the master branch since when the topic branch was started off it.

Limiting the diff output

$ git diff --diff-filter=MRC <1> $ git diff --name-status <2> $ git diff arch/i386 include/asm-i386 <3>

  1. Show only modification, rename and copy, but not addition nor deletion.

  2. Show only names and the nature of change, but not actual diff output.

  3. Limit diff output to named subtrees.

Munging the diff output

$ git diff --find-copies-harder -B -C <1> $ git diff -R <2>

  1. Spend extra cycles to find renames, copies and complete rewrites (very expensive).

  2. Output diff in reverse.

SEE ALSO

diff(1), git-difftool(1), git-log(1), gitdiffcore(7), git-format-patch(1), git-apply(1)

GIT

Part of the git(1) suite

Last updated 2011-03-15 23:30:13 UTC

'DevOps > Git' 카테고리의 다른 글

git commit 규약  (0) 2019.09.26
.gitkeep 파일로 empty folder를 git하기  (0) 2019.09.18
gitignore bestpractice  (0) 2019.09.05
git stash커맨드에 대해 알아보자  (0) 2019.08.23
git reflog  (0) 2019.07.24

- ブランチの命名規約

機能追加:

 feature/JIRAチケットID-機能の名称 or 開発内容名

バグ修正:

 fix/修正対象(内容)

クリティカルなバグ修正:

 hotfix/修正対象(内容)

リファクタリング等:

 clean/改修内容

機能削除:

  remove/機能の名称

 

■コミットコメントのフォーマット

1行目:[コミット種別] 概要 JIRAのチケットID

2行目 :空行

3行目以降:詳細(「なぜ」「何を」を記述)

 

3行目以降の内容はJIRAと連携される

 

■コミット種別

fix:バグ修正

hotfix:クリティカルなバグ修正

add:新規(ファイル)機能追加

update:機能修正(バグではない)

change:仕様変更

clean:整理(リファクタリング等)

disable:無効化(コメントアウト等)

remove:削除(ファイル)

upgrade:バージョンアップ

revert:変更取り消し

 

例)

[add] アップロード結果テーブルのページ切り替え機能の追加 #AIPOWAZZ-82

'DevOps > Git' 카테고리의 다른 글

git-diff(1) Manual Page  (0) 2020.01.10
.gitkeep 파일로 empty folder를 git하기  (0) 2019.09.18
gitignore bestpractice  (0) 2019.09.05
git stash커맨드에 대해 알아보자  (0) 2019.08.23
git reflog  (0) 2019.07.24

https://davidwalsh.name/git-empty-directory
https://medium.com/@kinduff/hey-git-please-keep-those-folders-eb0ed37621c8

There are times when you'd like to track an empty directory within git but there's a problem: git wont allow you to add a directory that doesn't have a file in it.  The easy solution is putting an empty stub file within the directory, and the industry standard for that stub file name is .gitkeep.

You can quickly create the file and commit the "empty" directory from command line:

# ignore files in folder foo
foo/*
# but keep the folder by keeping .gitkeep file
!foo/.gitkeep


touch foo/.gitkeep #add .gitkeep file

git add . git commit -m "Adding my empty directory"

The problem is simple, the solution is easy, but I wanted to highlight that .gitkeep is the industry standard.

'DevOps > Git' 카테고리의 다른 글

git-diff(1) Manual Page  (0) 2020.01.10
git commit 규약  (0) 2019.09.26
gitignore bestpractice  (0) 2019.09.05
git stash커맨드에 대해 알아보자  (0) 2019.08.23
git reflog  (0) 2019.07.24

.gitignore bestpractice

source

be cautious

  • somefolder/* : somefolder안의 모든 파일을 깃 대상에서 제외
  • !somefolder/.gitkeep : 단 somefolder는 유지
    # exclude everything
    somefolder/*
    

exception to the rule

!somefolder/.gitkeep


#### example

Byte-compiled / optimized / DLL files

pycache/ *.py[cod]

C extensions

*.so

Distribution / packaging

bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

Installer logs

pip-log.txt
pip-delete-this-directory.txt

Unit test / coverage reports

.tox/
.coverage
.cache
nosetests.xml
coverage.xml

Translations

*.mo

Mr Developer

.mr.developer.cfg
.project
.pydevproject

Rope

.ropeproject

Django stuff:

*.log
*.pot

Sphinx documentation

docs/_build/
```

'DevOps > Git' 카테고리의 다른 글

git commit 규약  (0) 2019.09.26
.gitkeep 파일로 empty folder를 git하기  (0) 2019.09.18
git stash커맨드에 대해 알아보자  (0) 2019.08.23
git reflog  (0) 2019.07.24
Rebase하기  (0) 2019.04.03

https://qiita.com/akasakas/items/768c0b563b96f8a9be9d


色々な git stash

この記事は最終更新日から1年以上が経過しています。

git stash コマンドを個人的によく使っていますが、オプションなどを忘れてしまいがちなので、備忘録として残しておきます。

:black_square_button: スタッシュする

git stash 
git stash save

:black_square_button: メッセージをつけてスタッシュする

git stash save "message"

:black_square_button: スタッシュしたリストを表示

git stash list 

:black_square_button: N番目にスタッシュしたファイルの一覧を表示

git stash show stash@{N}

:black_square_button: N番目にスタッシュしたファイルの変更差分を表示

git stash show -p stash@{N}

:black_square_button: スタッシュを適用し、適用したスタッシュを削除する

git stash pop # 最新のスタッシュを適用し、削除 
git stash pop stash@{N} # N番目のスタッシュを適用し、削除

:black_square_button: スタッシュは適用し、適用したスタッシュを残す

git stash apply # 最新のスタッシュを適用し、残す
git stash apply stash@{N} # N番目のスタッシュを適用し、残す

:black_square_button: N番目のスタッシュを削除する

git stash drop # 最新のスタッシュを削除
git stash drop stash@{N} # N番目のスタッシュを削除

:black_square_button: unstage ファイルを全てスタッシュ

git stash -k

:black_square_button: untrackファイルも含めて全てスタッシュ

git stash -u

:black_square_button: スタッシュを全削除する

git stash clear


'DevOps > Git' 카테고리의 다른 글

.gitkeep 파일로 empty folder를 git하기  (0) 2019.09.18
gitignore bestpractice  (0) 2019.09.05
git reflog  (0) 2019.07.24
Rebase하기  (0) 2019.04.03
jenkins와 gitlab연동(github아니고 gitlab임!)  (0) 2018.10.31

1. git reflog

  • git rebase 또는 git reset 등으로 커밋이 삭제될 수 있다.
  • 하지만, git 이력은 보관되고 있는데 이러한 이력을 볼 수 있는 명령어가 git reflog

12-1

2. commit 복구하기

  1. git reflog 명령어로 삭제된 commit id 확인 후
  2. git reset --hard <커밋해시id>

3. branch 복구하기

  1. git reflog 또는 git reflog |grep 브랜치명 으로 log확인
  2. git checkout -b <삭제한 브랜치명> <커밋해시id>


Git 브랜치 - Rebase하기(https://git-scm.com/book/ko/v1/Git-%EB%B8%8C%EB%9E%9C%EC%B9%98-%EB%A6%AC%EB%AA%A8%ED%8A%B8-%EB%B8%8C%EB%9E%9C%EC%B9%98)

Rebase하기

Git에서 한 브랜치에서 다른 브랜치로 합치는 방법은 두 가지가 있다. 하나는 Merge이고 다른 하나는 Rebase다. 이 절에서는 Rebase가 무엇인지, 어떻게 사용하는지, 좋은 점은 뭐고, 어떤 상황에서 사용하고 어떤 상황에서 사용하지 말아야 하는지 알아 본다.

Rebase의 기초

앞의 Merge 절에서 살펴본 예제로 다시 돌아가 보자(그림 3-27). 두 개의 나누어진 브랜치의 모습을 볼 수 있다. (브랜치를 나누면 브랜치가 생성된 시점의 커밑은 두 브랜치 양쪽에 속해진다. 가령 아래에서 c2커밑은 master브랜치와 experiment브랜치 모두에 속한다)

그림 3-27. 두 개의 브랜치로 나누어진 커밋 히스토리

이 두 브랜치를 합치는 가장 쉬운 방법은 앞에서 살펴본 대로 Merge 명령을 사용하는 것이다. 두 브랜치의 마지막 커밋 두 개(C3, C4)와 공통 조상(C2)을 사용하는 3-way Merge로 그림 3-28처럼 새로운 커밋을 만들어 낸다.

그림 3-28. 나뉜 브랜치를 Merge하기

비슷한 결과를 만드는 다른 방식으로, C3에서 변경된 사항을 패치(Patch)로 만들고 이를 다시 C4에 적용시키는 방법이 있다. Git에서는 이런 방식을Rebase라고 한다. Rebase 명령으로 한 브랜치에서 변경된 사항을 다른 브랜치에 적용할 수 있다.

위의 예제는 다음과 같은 명령으로 Rebase한다:

$ git checkout experiment
$ git rebase master
First, rewinding head to replay your work on top of it... Applying: added staged command

rebase를 할시 C'3는 C3와 내용상 동일하지만 다른 넘버의 커밑이다.

 

실제로 일어나는 일을 설명하자면 일단 두 브랜치가 나뉘기 전인 공통 커밋으로 이동하고 나서 그 커밋부터 지금 Checkout한 브랜치가 가리키는 커밋까지 diff를 차례로 만들어 어딘가에 임시로 저장해 놓는다. Rebase할 브랜치(역주 - experiment)가 합칠 브랜치(역주 - master)가 가리키는 커밋을 가리키게 하고 아까 저장해 놓았던 변경사항을 차례대로 적용한다. 그림 3-29는 이러한 과정을 나타내고 있다.

그림 3-29. C3의 변경사항을 C4에 적용하는 Rebase 과정

그리고 나서 master 브랜치를 Fast-forward 시킨다.

그림 3-30. master 브랜치를 Fast-forward시키기

C3'로 표시된 커밋에서의 내용은 Merge 예제에서 살펴본 C5 커밋에서의 내용과 같을 것이다. Merge이든 Rebase든 둘 다 합치는 관점에서는 서로 다를 게 없다. 하지만, Rebase가 좀 더 깨끗한 히스토리를 만든다. Rebase한 브랜치의 Log를 살펴보면 히스토리가 선형적이다. 일을 병렬로 동시에 진행해도 Rebase하고 나면 모든 작업이 차례대로 수행된 것처럼 보인다.

Rebase는 보통 리모트 브랜치에 커밋을 깔끔하게 적용하고 싶을 때 사용한다. 아마 이렇게 Rebase하는 리모트 브랜치는 직접 관리하는 것이 아니라 그냥 참여하는 브랜치일 것이다. 메인 프로젝트에 패치를 보낼 준비가 되면 하는 것이 Rebase이니까 브랜치에서 하던 일을 완전히 마치고 origin/master로 Rebase한다. 프로젝트 관리자는 어떠한 통합작업도 필요 없다. 그냥 master 브랜치를 Fast-forward 시키면 된다.

Rebase를 하든지, Merge를 하든지 최종 결과물은 같고 커밋 히스토리만 다르다는 것이 중요하다. Rebase의 경우는 브랜치의 변경사항을 순서대로 다른 브랜치에 적용하면서 합치고 Merge의 경우는 두 브랜치의 최종결과만을 가지고 합친다.

좀 더 Rebase

Rebase는 단순히 브랜치를 합치는 것만 아니라 다른 용도로도 사용할 수 있다. 그림 3-31과 같은 히스토리가 있다고 하자. server 브랜치를 만들어서 서버 기능을 추가하고 그 브랜치에서 다시 client 브랜치를 만들어 클라이언트 기능을 추가한다. 마지막으로 server 브랜치로 돌아가서 몇 가지 기능을 더 추가한다.

그림 3-31. 다른 토픽 브랜치에서 갈라져 나온 토픽 브랜치

이때 테스트가 덜 된 server 브랜치는 그대로 두고 client 브랜치만 master로 합치려는 상황을 생각해보자. server와는 아무 관련이 없는 client 커밋은 C8, C9이다. 이 두 커밋을 master 브랜치에 적용하기 위해서--onto옵션을 사용하여 아래와 같은 명령을 실행한다:

$ git rebase --onto master server client

이 명령은 client 브랜치를 Checkout하고 server와 client의 공통조상 이후의 패치를 만들어 master에 적용한다. 조금 복잡하긴 해도 꽤 쓸모 있다. 그림 3-32를 보자.

그림 3-32. 다른 토픽 브랜치에서 갈라져 나온 토픽 브랜치를 Rebase하기

이제 master 브랜치로 돌아가서 Fast-forward 시킬 수 있다:

$ git checkout master
$ git merge client

그림 3-33. master 브랜치를 client 브랜치 위치로 진행 시키기

server 브랜치의 일이 다 끝나면git rebase [basebranch] [topicbranch]라는 명령으로 Checkout하지 않고 바로 server 브랜치를 master 브랜치로 rebase할 수 있다. 이 명령은 토픽(server) 브랜치를 Checkout하고 베이스(master) 브랜치에 Rebase한다:

$ git rebase master server

server 브랜치의 수정사항을 master 브랜치에 적용했다. 그 결과는 그림 3-34와 같다.

그림 3-34. master 브랜치에 server 브랜치의 수정 사항을 적용

그리고 나서 master 브랜치를 Fast-forward 시킨다:

$ git checkout master
$ git merge server

모든 것이 master 브랜치에 통합됐기 때문에 더 필요하지 않다면 client나 server 브랜치는 삭제해도 된다. 브랜치를 삭제해도 커밋 히스토리는 그림 3-35와 같이 여전히 남아 있다:

$ git branch -d client
$ git branch -d server

그림 3-35. 최종 커밋 히스토리

Rebase의 위험성

Rebase가 장점이 많은 기능이지만 단점이 없는 것은 아니니 조심해야 한다. 그 주의사항은 다음 한 문장으로 표현할 수 있다:

이미 공개 저장소에 Push한 커밋을 Rebase하지 마라

이 지침만 지키면 Rebase를 하는 데 문제 될 게 없다. 하지만, 이 주의사항을 지키지 않으면 사람들에게 욕을 먹을 것이다(역주 - 아마도 가카의 호연지기가 필요해질 것이다).

Rebase는 기존의 커밋을 그대로 사용하는 것이 아니라 내용은 같지만 다른 커밋을 새로 만든다. 새 커밋을 서버에 Push하고 동료 중 누군가가 그 커밋을 Pull해서 작업을 한다고 하자. 그런데 그 커밋을git rebase로 바꿔서 Push해버리면 동료가 다시 Push했을 때 동료는 다시 Merge해야 한다. 그리고 동료가 다시 Merge한 내용을 Pull하면 내 코드는 정말 엉망이 된다.

이미 공개 저장소에 Push한 커밋을 Rebase하면 어떤 결과가 초래되는지 예제를 통해 알아보자. 중앙 저장소에서 Clone하고 일부 수정을 하면 커밋 히스토리는 그림 3-36과 같아 진다.

그림 3-36. 저장소를 Clone하고 일부 수정함

이제 팀원 중 누군가 커밋, Merge하고 나서 서버에 Push 한다. 이 리모트 브랜치를 Fetch, Merge하면 그림 3-37과 같이 된다.

그림 3-37. Fetch한 후 Merge함

그런데 Push했던 팀원은 Merge한 일을 되돌리고 다시 Rebase한다. 서버의 히스토리를 새로 덮어씌우려면git push --force명령을 사용해야 한다. 이후에 저장소에서 Fetch하고 나면 아래 그림과 같은 상태가 된다:

그림 3-38. 한 팀원이 다른 팀원이 의존하는 커밋을 없애고 Rebase한 커밋을 다시 Push함

기존 커밋이 사라졌기 때문에 이미 처리한 일이라고 해도 다시 Merge해야 한다. Rebase는 커밋의 SHA-1 해시를 바꾸기 때문에 Git은 새로운 커밋으로 생각한다. 사실 C4는 이미 히스토리에 적용되어 있지만, Git은 모른다.

그림 3-39. 같은 Merge를 다시 한다

다른 개발자와 계속 같이 일하려면 이런 Merge도 해야만 한다. Merge하면 C4와 C4' 커밋 둘 다 히스토리에 남게 된다. 실제 내용과 메시지가 같지만 SHA-1 해시 값이 전혀 다르다.git log로 히스토리를 확인해보면 저자, 커밋 날짜, 메시지가 같은 커밋이 두 개 있을 것이다. 이렇게 되면 혼란스럽다. 게다가 이 히스토리를 서버에 Push하면 같은 커밋이 두 개 있기 때문에 다른 사람들도 혼란스러워한다.

Push하기 전에 정리하려고 Rebase하는 것은 괜찮다. 또 절대 공개하지 않고 혼자 Rebase하는 경우도 괜찮다. 하지만, 이미 공개하여 사람들이 사용하는 커밋을 Rebase하면 틀림없이 문제가 생길 것이다.


jenkins과 gitlab연동하기




Jenkins side

1.GitLab플러그인 설치


젠킨스 쪽에서 가장 먼저 해야할 것은 gitlab과의 연동을 도와주는 플러그인을 설치하는 것이다. 


 Jenkinsの管理 ->  plugin 管理】


위의 패스를 통해 들어가면 아래와 같은 화면이 나오는데, 거기서 gitlab을 검색하면 gitlab plugin을 설치할 수 있다.





2.Jenkins에 gitlab유저정보 등록하기


認証情報 -> System -> グローバルドメイン -> 認証情報の追加】


위의 패스를 통해 들어가면, 아래와 같은 화면이 나오는데, 여기에 gitlab의 유저 정보를 기입한다.

이때, gitlab쪽에서 jenkins 전용 유저아이디를 만들어 사용하면, 사용자가 겹치지 않으므로 jenkins작업에 유리하다.




3.Jenkins 프로젝트 작성


[소스코드 관리 설정]

Repository URL : 대상이 되는 gitlab의 레포지토리 주소를 적어둔다

Credentials : 2의 유저정보등록에서 미리 등록해둔 gitlab의 정보를 사용한다.

Branch : 갱신을 확인할 브랜치를 기입한다.







[빌드트리거]

1. Build when a change is pushed .. 를 선택하고, 형광팬 표시된 url은 gitlab의 webhook등록에서 사용되므로 복사해두자.






[빌드트리거]-시크릿키 생성

gitlab에서 jenkins로 webhook을 리퀘스트 할 시, 필요한 스키릿키이다. 

시크릿키도  gitlab의 webhook등록시 사용되므로 복사해 두도록하자.







4.Gitlab webhook작성



3번에서 복사해둔, URL과 시크릿키를 각각 입력한다.

입력이 끝난 후에는 test버튼을 눌러 연결을 확인해본다.

















+ Recent posts