How to count git commit numbers which contains filename (*_test.cpp)? - bash

git log --grep "xxx" only search commit log while git log -- *_test.cpp only shows commit just contains *_test.cpp.
Is there a way to show commits which contains filename (*_test.cpp)? I'd like to count both commit1, commit2 and commit3 case.
commit1:
/path_a/file_a.cpp;
/path_a/file_b.cpp;
/path_b/file_a_test.cpp;
commit2: /path_c/file_b_test.cpp
commit3: /path_d/file_d_test.cpp
/path_e/file_e_test.cpp
Thanks.

Your title says count, your question body says show.
Count:
git rev-list --count # -- \*_test.cpp
list:
git rev-list # -- \*_test.cpp
show:
git log # -- \*_test.cpp
and I'd recommend adding either --no-merges or --first-parent to avoid redundantly listing every merge that forwarded changes along with the commit that made it.
git log -- *_test.cpp only shows commit just contains *_test.cpp
That's not how git log works for me, and its not how it's documented to work. Check your evidence, I think you're misunderstanding whatever led you to say that, for instance, in my git repo:
$ git log -10 --pretty=format: --name-only --no-merges # -- \*-*.c
refs/files-backend.c
builtin/cat-file.c
builtin/cat-file.c
fmt-merge-msg.c
t/helper/test-drop-caches.c
stable-qsort.c
builtin/update-index.c
read-cache.c
builtin/receive-pack.c
builtin/cat-file.c
builtin/rev-list.c
ref-filter.c
$
but adding the --full-diff option gives a much longer list of files touched by those commits.

I think using the ** wildcard will get you what you want:
git log '**/*_test.cpp'
** matches any number of directories. For more details, see the documentation.

Related

git difference does not work properly after git clone

I have a bash script in which I have to clone and get difference of some repositories.
I am trying to get difference between range of a date
git clone $repository
cd $path
git diff master#{2019-10-1}..master#{2019-10-14} -- package.json
but it shows error:
warning: Log for 'master' only goes back to Tue, 15 Oct 2019 09:51:16 +0000.
But this repository is old and has many commits.
when I do it locally on the machine in which I had cloned same repository some weeks back I get proper difference.
$ git diff master#{2019-10-1}..master#{2019-10-14} -- package.json
diff --git a/package.json b/package.json
index d29ffcb..8766fde 100644
--- a/package.json
+++ b/package.json
## -1,6 +1,6 ##
{
"name": "accountd",
- "version": "0.0.95",
+ "version": "0.0.102",
"main": "dist/src/index.js",
"private": true,
"scripts": {
## -8,7 +8,8 ##
"start-dev": "npm run build && nodemon . port=5000 stage",
"start-sand": "npm run build && nodemon . port=5000 stage",
- "test": "nyc --extension .ts --reporter=html --reporter=cobertura --reporter=text mocha -r ts-node/register src/**/*.spec.ts --exit",
+ "test": "mocha -r ts-node/register test/**/*.spec.ts test/**/**/*.spec.ts --exit",
I want the changes made in this specific file over a period of time.
The notation you're using (master#{<date>}) says that you want to refer to the version of master on <date> according to the local reflogs. That is, you're saying that you want to know what this particular clone's master ref pointed to on that date - not what of the current master commits had been committed on that date.
And git is telling you "this clone didn't have a master ref on that date".
To do what you mean, you first have to find the last commit before the "then' date, then diff against that. There are a number of ways, but something like
git diff $(git rev-list -n1 --before="<date>" --first-parent master) master
might be more what you want
The #{date} syntax will not get you what you want. I'd go into what you do want, but I got beaten to the answer; see Mark Adelsberger's answer instead. :-)
What to know about the #{...} syntax
With the #{date} syntax, it doesn't matter how old the repository source is. It does not matter how many commits you have. All that matters is what is in this particular clone's so-called reflogs.
Every Git repository—every clone—has its own reflogs. In general, no two different Git repositories will agree as to what goes into the reflogs. The reflogs are not meant for this kind of job. Which kind of job? This kind:
I want the changes made in this specific file over a period of time.
The reflogs tell you absolutely nothing about changes in files, or changes not in files, over time. They tell you about changes in your Git's references over time.
This may leave you puzzling over what, precisely, a ref or reference is. We probably should not go into too much detail here, but every branch name like master is actually a reference whose full spelling is refs/heads/master; every tag name like v1.2 is a ref whose full spelling is refs/tags/v1.2; and every remote-tracking name like origin/master is a ref whose full spelling starts with refs/remotes/. So they're just a generalization of the various names that humans tend to use, to talk about commits. What Git needs, to talk about commits, is their raw hash IDs. The names are, to some extent, just there so that us weak humans don't have to remember arbitrary, big ugly hash IDs.1
The key to understanding this, and the #{...} syntax, is to realize that these names—e.g., refs/heads/master—change over time. Right now, your master commit might be a123456.... Yesterday, it might have been some other commit, and tomorrow, it might be yet another commit. Your Git's master will change over time, and every time it does change, your Git keeps a record of what it was. This record only goes back so far: the commits are permanent, but the record of which hash ID master meant, when, is not. Moreover, it's not carried from one clone to another: Every clone's record of what master meant when is private to that one particular Git. In a fresh clone—which has all the commits2—there's only one value master has ever had, which is what it has right now.
Note that you can also use #{number}. This selects the number'th entry in the reflog.
To view the actual reflog for any particular ref, run git reflog ref, e.g., git reflog master. The git reflog command has various other sub-commands for dealing with the logs, too. See its documentation for details.
1The names do have other functions. For much more about this, see Think Like (a) Git.
2Assuming it's not a shallow clone, that is, and not using some of the new "promisor pack" features that are not yet in general use. Also, if you cloned with -b or --single-branch, or the upstream repository is set up in a less-usual fashion, you might not have a branch named master yet at all.

How to only list .png files that have been modified in Git

How would one go about only listing the png files that have been modified in the current branch on Git?
My goal is to copy those files to a different directory (I need to send an email).
Suppose I have:
$ git status
On branch update_assessment_pt1
Your branch is up-to-date with 'upstream/devel'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: assessment/LWR/validation/HbepR1/analysis/hbepr1_plot.py
deleted: assessment/LWR/validation/HbepR1/doc/figures/AxialPowerProfile.pdf
deleted: assessment/LWR/validation/HbepR1/doc/figures/AxialProfile.pdf
deleted: assessment/LWR/validation/HbepR1/doc/figures/CladDisp.pdf
deleted: assessment/LWR/validation/HbepR1/doc/figures/FissionGas.pdf
modified: assessment/LWR/validation/HbepR1/doc/figures/FissionGas.png
deleted: assessment/LWR/validation/HbepR1/doc/figures/InterGasPress.pdf
deleted: assessment/LWR/validation/HbepR1/doc/figures/Mesh.pdf
deleted: assessment/LWR/validation/HbepR1/doc/figures/Power.pdf
modified: assessment/LWR/validation/HbepR1/doc/figures/Power.png
new file: assessment/LWR/validation/IFA_431/analysis/ifa431_plot.py
modified: assessment/LWR/validation/IFA_431/doc/figures/431_bol_rod_power.png
modified: assessment/LWR/validation/IFA_431/doc/figures/431r1.png
modified: assessment/LWR/validation/IFA_431/doc/figures/431r2.png
modified: assessment/LWR/validation/IFA_431/doc/figures/431r3.png
How would I go about getting the following, so I can copy those files?
modified: assessment/LWR/validation/HbepR1/doc/figures/FissionGas.png
modified: assessment/LWR/validation/HbepR1/doc/figures/Power.png
modified: assessment/LWR/validation/IFA_431/doc/figures/431_bol_rod_power.png
modified: assessment/LWR/validation/IFA_431/doc/figures/431r1.png
modified: assessment/LWR/validation/IFA_431/doc/figures/431r2.png
modified: assessment/LWR/validation/IFA_431/doc/figures/431r3.png
Use git diff --cached --diff-filter=M --name-only to obtain these file names. Add -- '*.png' if needed to keep the list filtered to just *.png files—the command will list any to be committed file whose status is M (modified).
Things to know to keep this from just being a "use this magic command" answer
In text, you first called these modified in the current branch. This phrase doesn't mean any one specific thing. Fortunately you then went on to show git status output, where they were listed under Changes to be committed.
Git doesn't store diffs at all. Git stores snapshots—whole files, intact, inside the main unit of storage, which is the commit. That means that in order to see a change, you have to pick two commits: $old and $new. Git will extract both, then compare them. Whatever is different between commit $old and commit $new, Git will tell you about that. The actual change can be any of a number of change-status-es:
A means Added: the file is not in $old and is in $new.
M means Modified: the file is different between $old and $new. The difference could just be the mode of the file: executable, or not.
D means Deleted: the file is in $old, but not in $new.
R, C, T, and some other rare cases can also occur, though some of them may require extra flags to git diff: you won't see an R status unless you enable rename-detection, for instance. (Rename detection defaults to on in the most modern Git versions, but off in older Git versions.)
Using --name-status, git diff will show you the file names and status letters, instead of showing an actual diff. (Try this out to see.) The --diff-filter argument lets you tell Git: only tell me about files whose status meets the letters I pick.
Note, by the way, that the special name HEAD always means the current commit. It does not matter how you made this commit become the current commit, though one typical way is by using git checkout: you git checkout a commit by its hash ID, for instance, and that commit is now checked out and is the current commit. Or, you git checkout a branch name, and the tip commit of that branch is now out and is the current commit. There is always1 a current commit, and you can name it by writing the name HEAD in all uppercase.2
All of the above talks about comparing commits, but there are two other places that files can exist, that are not commits. Note that both of these places are temporary: they get wiped out by various operations, and once wiped out, cannot be recovered in Git: you have to copy from these temporary places, into actual commits, to make the files permanent. Once the files are in commits, they're frozen for all time, and can be restored to useful form in the future for as long as the commit itself exists (which tends to be "forever", or as long as the repository exists).
These two places are:
the index, which Git also calls the staging area or (rarely) the cache, and
the work-tree or working tree or any of several variants on this name.
Files that are in the index right now are ready to be committed. Every file that will be committed is in the index right now, even if the index copy matches the current (HEAD) commit copy.
You can, at any time, compare the HEAD commit to whatever is in the index right now. One command that does this is git diff --cached. For every file in HEAD and/or in the index, Git compares the two copies of the file. If they are different, the file is modified. If the index file exists but there is no such file in HEAD, the file is added. If the file exists in HEAD but not in the index, the file is deleted.
You can also, at any time, compare HEAD to the work-tree, or the index to the work-tree. The commands that do this are git diff HEAD and git diff (with no name). Again, for every file on the left-hand side (HEAD or the index), and every file on the right-hand side (in the work-tree), Git compares the two copies of the file.
Last, note that git status runs two git diffs. It does a quick git diff --cached to compare HEAD vs index. Whatever is different here, git status lists that file as to be committed. It also does a quick git diff (with no extra arguments except for --name-only) to compare index vs work-tree. Whatever is different here, git status lists that file as changes not staged for commit.
You wanted to compare HEAD vs index, so you want git diff --cached. You then wanted to list only those files that are Modified, so you can add --diff-filter=M. You didn't want to see the actual differences—nor even the status letters; file names only please!—so you can add --name-only. You also wanted only to list files whose name matches *.png, so add -- '*.png'—the quotes protect the * from the shell; we want Git to see the * so that Git can treat it as a pathspec—to get just those.
1Actually, this is really almost always. There's a special state in which HEAD exists and contains a branch name, but the branch name itself doesn't exist. This state mostly occurs when you create a new, totally-empty repository. Git requires a branch name like master to identify some existing, valid commit hash ID. There are no commits, so there are no valid hash IDs, so master itself is not allowed to exist. Nonetheless, HEAD holds the name master, so that Git will create the master branch when you make the first commit.
2On Windows and MacOS, you can sometimes get away with using head (lowercase) instead of HEAD (all-uppercase). This misbehaves if you start using git worktree add, so it's a bad habit to get into. If you don't like typing HEAD in all capitals, consider using the symbol #, which is a synonym for HEAD.

Git how to get hashid of last GitHub release tag

Every 10-20 commits I have a commit of the form "version uped to x.y.z" that is also marked a tag, since its a GitHub release point. Example below. I need to get hashid of last such commit so I can use it in a script like "git rebase -i $(hashid)", which is a point of freezing, where existing commits should not changed. There are 2 possible means to get it: search for last commit with message starting with "version uped" or search for last commit being a tag. I am not skilled with bash, so please assist.
dfd48cd (HEAD -> master, origin/master, origin/HEAD) Operator [:] for GreedyRange removed
b610256 Array GreedyRange docs updated
e6a1446 Embedded docstring updated
825bf83 moved gallery and deprecated_gallery to new folders
9414a55 Kaitai comparison schemas moved to a folder
61e9ccb Padded fixed, negative length check and docstring
ad6148c FixedSized updated, changed build semantics
979538d FixedSized NullTerminated NullStripped fixed, _parsereport and docstrings
4719d67 lib/py3compat updated, supportsintflag supportsintflag more accurate
9c164d4 makefile added xfails profile
672fefa (tag: v2.9.40) version uped to 2.9.40
In this example, output would be: 672fefa52b537c17f5ede90996b9156eb0e040ac
Here is one way:
git tag --sort -v:refname | head -1 | xargs git rev-list -n 1
Explained:
Get the list of all tags, sorted descending by version number
Pick the first one (the one with the highest version number)
Pass it to git rev-list to find the commit hash it references

How can I run `git diff --staged` with Fugitive?

The command :Gdiff is equivalent to running git diff on that file.
What's the equivalent for git diff --staged or git diff --cached?
I've found a way to do this. Run :Git, you should get a window with contents like the following:
# Head: master
# Merge: origin/master
# Help: g?
#
# Staged (1)
# M example.txt
#
Scroll down to the staged file, example.txt, and press dd. This will open a diff view, comparing what's in HEAD and what's in the index. You'll notice on the bar on the bottom that both the filenames are special Fugitive filenames.
Also while in :Git preview window, you can press g?, which will list all the mappings valid in the current context.
While vim-fugitive does not supply direct analogues for git diff --staged or git diff --cached, it does supply a general-purpose Vim command for piping the output of arbitrary git commands into read-only Vim buffers: :Git!.
Inapplicable Answers
Before we get to that, let's explicitly restate the question. git diff --staged and git diff --cached are synonyms for the same underlying operation: diffing the contents of the index (the set of all staged changes) against the contents of the HEAD (the most recent commit for the current branch), typically for reviewing changes prior to commit. The stated question then becomes:
What is the most effective means of reviewing all staged changes in vim-fugitive?
It should be clear that the currently accepted self-answer fails to address this question. The next highest rated self-answer is no better.
:Gstatus bindings only apply to the file on the current line and hence cannot by definition be used to review all staged changes. Moreover, the :Gstatus D binding doesn't even review all staged changes for the file on the current line. It only diffs the index and working tree copies of that file, rather than diffing the index and most recently committed copies of that file (which is an altogether different beast).
:Gdiff HEAD is similarly inapplicable. It only diffs the most recently committed and working tree copies of the file corresponding to the current buffer. :Gdiff without an argument is equivalent to the :Gstatus D binding, again diffing the index and working tree copies of that file. Neither reviews all staged changes.
Applicable Answers
emaniacs struck the closest to a working solution with this comment to the latter answer:
:Git diff --staged
Now we're approximating the truth!
:Git pipes the output of the passed git command to the current external pager, permitting a liesurely review of all staged changes external to Vim. But there's the rub: external to Vim. That means no Vim bindings, buffers, or syntax highlighting. Ideally, we'd prefer a read-only Vim buffer syntax highlighting the output of git diff --staged. Can we do this?
The Solution
We can, or Tim Pope isn't the Vim Pope. The !-suffixed variant of :Git does just that, permitting a liesurely review of all staged changes within Vim complete with Vim-based syntax highlighting of change differences:
:Git! diff --staged
Yeah. It's pretty awesomeness.
But let's go a step farther. In the time-honoured tradition of slothful slackers everywhere, let's define a new Vim command :Greview encapsulating this operation and a new binding <leader>gr running this command. Just stash the following into your .vimrc:
command Greview :Git! diff --staged
nnoremap <leader>gr :Greview<cr>
Assuming <leader> to be ,, reviewing all staged changes reduces to ,gr. It couldn't get any Vimmier.
:Gdiff HEAD
Gdiff takes an revision argument. So you can pass it HEAD. This is not equivalent to git diff --staged, but it can serve a similar purpose.
Update: 3/28/2017,
Current version of fugitive will do this automatically when you press D on a file.
if the file is staged, only staged changes will be showed in the diff.
If the file is not staged, then only change that are not staged will be visible.
As already noted, Gdiff, Gdiff : or Gdiff :0 gives you the diff with the index,
Gdiff - or Gdiff HEAD gives the diff with the HEAD.
Triple-split approach
So doing a diff first with : then with - show 3 diff-panes in vim:
HEAD
index ("cached" or "staged")
working tree
command! -bar Gvstage :Gvdiff -|Gvdiff : " vertical 3-split
command! -bar Gsstage :Gsdiff -|Gsdiff : " horizontal 3-split
Of course you can also just to Gvdiff - if you're already in diff mode.
Now pushing and getting changes is sligthly more complicated with 3 open windows, however, you can diffput from index to working tree and vice-versa easily, as on the HEAD modifiable is off, so it can never be targeted.
Otherwise, you can add some shortcuts for the diffput and diffget commands, knowing that they can take a "buffer specifier", which can be a pattern (see :help merge) or the buffer number. I modified the previous commands to save the initial buffer's number and use patterns for the others:
command! -bar Gvstage :let t:working_copy=bufnr('%')|Gvdiff -|Gvdiff : " vertical 3-split
command! -bar Gsstage :let t:working_copy=bufnr('%')|Gsdiff -|Gsdiff : " horizontal 3-split
nnoremap <Leader>hg :diffget fugitive://*/.git//[0-9a-f][0-9a-f]*/<CR> " HEAD get
nnoremap <Leader>ig :diffget fugitive://*/.git//0/<CR> " index get
nnoremap <Leader>ip :diffput fugitive://*/.git//0/<CR> " index put
nnoremap <Leader>wg :diffget <C-R>=t:working_copy<CR><CR> " work get
nnoremap <Leader>wp :diffput <C-R>=t:working_copy<CR><CR> " work put
Difftool approach
Alternately, if you just want a nice vimdiff view of what is staged instead of a patch, let me sugget:
command! Greview :exec "Git difftool --tool=vimdiff --staged " . fugitive#buffer().path()
This starts a new instance of vim, so when you quit it you go back to your tabs and windows you already had opened, which is perfect. This if faster (at least for me) than transitioning through the git status window, but has the drawback that you can't edit the staged file.
Use :Gtabedit #:% | Gdiff :.
This is better than the other answers because it opens in split view just like :Gdiff, rather than dumping diff syntax into a single buffer. When you're done, just :tabc to get back.
Explanation
Gtabedit open a new tab and edit a fugitive object:
from commit # (HEAD), a specific file :, the current file %.
Gdiff diff the current buffer against another fugitive object:
from the index : (you can specify the file again with :% but it's not needed).
Bonus
We now have fugitive equivalents for git diff (:Gdiff) and git diff --staged (the command above). To get the behaviour of git show on the current file, use :Gtabedit #~:% | Gdiff #.
References
https://github.com/tpope/vim-fugitive/issues/837#issuecomment-247985612
:help fugitive-object
In case you somebody stumbled on this question.
:Gdiff --staged
will do.. :)

'git diff' inconsistent between CLI and other clients

I'm trying to get a list of changed/added/deleted/etc. files for a commit in my Git repository. When I run the following in the shell, this is the output:
Indragie$ /usr/bin/git diff --name-status 0836
D INPopoverController.h
D INPopoverController.m
D INPopoverControllerDefines.h
D INPopoverWindow.h
D INPopoverWindow.m
D INPopoverWindowFrame.h
D Images/blue_progress_slice.png
M Images/next.png
M Images/pause.png
M Images/play.png
M Images/previous.png
D Images/progress_left_cap.png
When I check the list of changes in Xcode (or any other third party Git client), I see this:
Xcode diff http://cl.ly/2i3P3s0m0i3I10110h3E/Screen_Shot_2011-04-07_at_8.59.18_PM.png
Obviously these are just excerpts of the larger lists, but the point is that they are not the same at all. I've verified that the SHA1 hash of the commit I'm looking at is the same in both the CLI git and in Xcode. I'm new to git so there might be something fairly obvious I'm doing wrong, but even after pouring over man pages and git tutorials, I can't seem to find where I'm going wrong. Any help is appreciated.
Are you sure you're looking at the same things?
git diff <commit-id> will show you the differences between your current working tree and the tree at the time of that commit, not the changes introduced by that commit.
git show would show you just that commit's changes.

Resources