git log with square bracket in name - windows

I'm trying to get the log for a file with square brackets in its name. In case it matters: the OS is Windows and i'm using git bash.
If you create a file with [] in its name like [Start_here].bat then git log will not work.
I tried:
git log '[Start_here].bat'
git log \[Start_here\].bat
git log '\[Start_here\].bat'
git log -- '[Start_here].bat'
git log -- \[Start_here\].bat
git log -- '\[Start_here\].bat'
And none of them seemed to work. Either it did not display any commits or displayed a list of unrelated commits.
Does anybody have a solution that works?
Note:
git mv '[Start_here].bat' 'start_here.bat'
git log --follow start_here.bat
does show me a history in which the file was changed.
Edit:
Using the following script executed in a new repo I can see git log behaving correctly untill you add a submodule. Then it starts listing all the commits that changed a submodule too...
#!/usr/bin/env bash
set -x
rm -rf test-repo
rm -rf test-submodule
mkdir test-submodule
pushd test-submodule
git init
git config --local user.name tester1
git config --local user.email test#here.com
echo "a single line" > file.txt
git add file.txt
git commit -m "first commit"
popd
mkdir test-repo
pushd test-repo
git init
git config --local user.name tester1
git config --local user.email test#here.com
git branch -m master
echo "first line" > somefile.txt
git add somefile.txt
git commit -m "First line"
echo "another line" >> somefile.txt
git add somefile.txt
git commit -a -m "Second line"
echo "A line" > "[__Odd__].txt"
git add "[__Odd__].txt"
git commit -m "Adding odd file"
echo "third line" >> somefile.txt
git commit -a -m "Another bold statement."
echo "2nd line" >> "[__Odd__].txt"
echo "more" >> somefile.txt
git add "[__Odd__].txt" somefile.txt
git commit -m "changed both in master1"
git checkout -b new_branch
echo "2nd line" >> "[__Odd__].txt"
echo "more" >> somefile.txt
git add "[__Odd__].txt" somefile.txt
git commit -m "changed both in new_branch"
git checkout master
echo "2nd line" >> "[__Odd__].txt"
echo "more" >> somefile.txt
git add "[__Odd__].txt" somefile.txt
git commit -m "changed both in master"
git submodule add -- ../test-submodule module
git commit -m "Added submodule"
git log -- "[__Odd__].txt"
This outputs:
commit c3ebc7e0daf68543d761fc3b06c7ab35e014efaf (HEAD -> master)
Author: tester1 <test#here.com>
Date: Fri Nov 17 13:06:07 2017 +0100
Added submodule
commit 03a935df578a2eaf3f42789bd1e89e313224797a
Author: tester1 <test#here.com>
Date: Fri Nov 17 13:06:06 2017 +0100
changed both in master
commit d617db69dd9468e88587e2363050fdf57ac10756
Author: tester1 <test#here.com>
Date: Fri Nov 17 13:06:06 2017 +0100
changed both in master1
commit 88a07e5c887d63ead4e6cedd6349dfaf85ec1866
Author: tester1 <test#here.com>
Date: Fri Nov 17 13:06:05 2017 +0100
Adding odd file
Notice the top entry, it should not be here. It is related only to the submodule not to the file i want the log for.
git log -- somefile.txt does not output changes related to the submodules

In CMD, try:
git log -- "[__Odd__].txt"
or
git log -- ./"[__Odd__].txt"
Proof: https://imgur.com/7qqsVJ6
If everything fails, you can install WSL, and use git for tricky situations there.
Update 1 - From the comments:
you, sir, have found a bug in git for windows! I can reproduce your git log, and I can't reproduce it in WSL using linux's git, but can reproduce it using git.exe within WSL!
Filed a report: github.com/git-for-windows/git/issues/1371, let's see what comes of it
Update 2: This was a real bug in the code Git software, which has been fixed due to this question! How amazing 🎉 is that?!?!

Git on Windows comes in two flavors:
one using the Bash shell, and another using DOS.
The former is called Git Bash, the latter Git Cmd.
In Git Bash, git log '[foo]' should work.
In Git Cmd, git log "[foo]" should work (this also works in Git Bash btw).
In a DOS shell, you cannot use single-quotes to enclose strings that contain special characters.
(Special in the sense of "special to the shell".)
In Bash you can.
This is why, it looks like you are using Git Cmd.
Also keep in mind that auto-completion is your friend.
Both Git Bash and Git Cmd can auto-complete path names.
For example in Git Cmd if you start typing git log "[ and then press TAB,
it should give you some options to auto-complete.
If you start typing git log '[ and then press TAB,
it won't give you any options.
That's how you know the syntax is already wrong and you can stop typing and try another way.

I just tested, with Git 2.15 for Windows, the double-quotes.
It works with git bash:
vonc#VONCAVN7:/mnt/d/git/talks$ git log -- "[r].txt"
commit 7a698bf83cb55fa479200a3585b03ece1ee5db0a (HEAD -> 201710_docker)
Author: VonC <vonc#laposte.net>
Date: Mon Nov 13 20:33:04 2017 +0100
test
Or directly from a CMD shell session:
vonc#VONCAVN7 D:\git\talks
> git log -- "[r].txt"
commit 7a698bf83cb55fa479200a3585b03ece1ee5db0a (HEAD -> 201710_docker)
Author: VonC <vonc#laposte.net>
Date: Mon Nov 13 20:33:04 2017 +0100
test
To be sure you don't have any other program interfering with Git, try the same Git log after having set a simplified PATH.

Related

Check whether a repository would be pushed in a shell script [duplicate]

How can I view any local commits I've made, that haven't yet been pushed to the remote repository? Occasionally, git status will print out that my branch is X commits ahead of origin/master, but not always.
Is this a bug with my install of Git, or am I missing something?
This gives a log of all commits between origin/master and HEAD:
git log origin/master..HEAD
When HEAD is on the master branch, this gives a log of unpushed commits.
Similarly, to view the diff:
git diff origin/master..HEAD
To see all commits on all branches that have not yet been pushed:
git log --branches --not --remotes
To see the most recent commit on each branch, as well as the branch names:
git log --branches --not --remotes --simplify-by-decoration --decorate --oneline
Show all commits that you have locally but not upstream with:
git log #{u}..
#{u} or #{upstream} means the upstream branch of the current branch (see git rev-parse --help or git help revisions for details).
git cherry -v
Taken from: Git: See all unpushed commits or commits that are not in another branch.
You can do this with git log:
git log origin/master..
This assumes that origin is the name of your upstream remote and master is the name of your upstream branch. Leaving off any revision name after .. implies HEAD, which lists the new commits that haven't been pushed.
All the other answers talk about "upstream" (the branch you pull from).
But a local branch can push to a different branch than the one it pulls from.
A master might not push to the remote-tracking branch "origin/master".
The upstream branch for master might be origin/master, but it could push to the remote tracking branch origin/xxx or even anotherUpstreamRepo/yyy.
Those are set by branch.*.pushremote for the current branch along with the global remote.pushDefault value.
It is that remote-tracking branch that counts when seeking unpushed commits: the one that tracks the branch at the remote where the local branch would be pushed to.
The branch at the remote can be, again, origin/xxx or even anotherUpstreamRepo/yyy.
Git 2.5+ (Q2 2015) introduces a new shortcut for that: <branch>#{push}
See commit 29bc885, commit 3dbe9db, commit adfe5d0, commit 48c5847, commit a1ad0eb, commit e291c75, commit 979cb24, commit 1ca41a1, commit 3a429d0, commit a9f9f8c, commit 8770e6f, commit da66b27, commit f052154, commit 9e3751d, commit ee2499f [all from 21 May 2015], and commit e41bf35 [01 May 2015] by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit c4a8354, 05 Jun 2015)
Commit adfe5d0 explains:
sha1_name: implement #{push} shorthand
In a triangular workflow, each branch may have two distinct points of interest: the #{upstream} that you normally pull from, and the destination that you normally push to. There isn't a shorthand for the latter, but it's useful to have.
For instance, you may want to know which commits you haven't
pushed yet:
git log #{push}..
Or as a more complicated example, imagine that you normally pull changes from origin/master (which you set as your #{upstream}), and push changes to your fork (e.g., as myfork/topic).
You may push to your fork from multiple machines, requiring you to integrate the changes from the push destination, rather than upstream.
With this patch, you can just do:
git rebase #{push}
rather than typing out the full name.
Commit 29bc885 adds:
for-each-ref: accept "%(push)" format
Just as we have "%(upstream)" to report the "#{upstream}" for each ref, this patch adds "%(push)" to match "#{push}".
It supports the same tracking format modifiers as upstream (because you may want to know, for example, which branches have commits to push).
If you want to see how many commit your local branches are ahead/behind compared to the branch you are pushing to:
git for-each-ref --format="%(refname:short) %(push:track)" refs/heads
I had a commit done previously, not pushed to any branch, nor remote nor local. Just the commit. Nothing from other answers worked for me, but with:
git reflog
There I found my commit.
Handy git alias for looking for unpushed commits in current branch:
alias unpushed = !GIT_CURRENT_BRANCH=$(git name-rev --name-only HEAD) && git log origin/$GIT_CURRENT_BRANCH..$GIT_CURRENT_BRANCH --oneline
What this basically does:
git log origin/branch..branch
but also determines current branch name.
You could try....
gitk
I know it is not a pure command line option but if you have it installed and are on a GUI system it's a great way to see exactly what you are looking for plus a whole lot more.
(I'm actually kind of surprised no one mentioned it so far.)
git branch -v will show, for each local branch, whether it's "ahead" or not.
I use the following alias to get just the list of files (and the status) that have been committed but haven't been pushed (for the current branch)
git config --global alias.unpushed \
"diff origin/$(git name-rev --name-only HEAD)..HEAD --name-status"
then just do:
git unpushed
I believe the most typical way of doing this is to run something like:
git cherry --abbrev=7 -v #{upstream}
However, I personally prefer running:
git log --graph --decorate --pretty=oneline --abbrev-commit --all #{upstream}^..
which shows the commits from all branches which are not merged upstream, plus the last commit in upstream (which shows up as a root node for all the other commits). I use it so often that I have created alias noup for it.
git config --global alias.noup \
'log --graph --decorate --pretty=oneline --abbrev-commit --all #{upstream}^..'
git cherry -v
This will list out your local comment history (not yet pushed) with corresponding message
I suggest you go see the script https://github.com/badele/gitcheck, i have coded this script for check in one pass all your git repositories, and it show who has not commited and who has not pushed/pulled.
Here a sample result
It is not a bug. What you probably seeing is git status after a failed auto-merge where the changes from the remote are fetched but not yet merged.
To see the commits between local repo and remote do this:
git fetch
This is 100% safe and will not mock up your working copy. If there were changes git status wil show X commits ahead of origin/master.
You can now show log of commits that are in the remote but not in the local:
git log HEAD..origin
This worked better for me:
git log --oneline #{upstream}..
or:
git log --oneline origin/(remotebranch)..
There is tool named unpushed that scans all Git, Mercurial and Subversion repos in specified working directory and shows list of ucommited files and unpushed commits.
Installation is simple under Linux:
$ easy_install --user unpushed
or
$ sudo easy_install unpushed
to install system-wide.
Usage is simple too:
$ unpushed ~/workspace
* /home/nailgun/workspace/unpushed uncommitted (Git)
* /home/nailgun/workspace/unpushed:master unpushed (Git)
* /home/nailgun/workspace/python:new-syntax unpushed (Git)
See unpushed --help or official description for more information. It also has a cronjob script unpushed-notify for on-screen notification of uncommited and unpushed changes.
To list all unpushed commit in all branches easily you can use this command:
git log --branches #{u}..
If the number of commits that have not been pushed out is a single-digit number, which it often is, the easiest way is:
$ git checkout
git responds by telling you that you are "ahead N commits" relative your origin. So now just keep that number in mind when viewing logs. If you're "ahead by 3 commits", the top 3 commits in the history are still private.
Similar: To view unmerged branches:
git branch --all --no-merged
Those can be suspect but I recommend the answer by cxreg
one way of doing things is to list commits that are available on one branch but not another.
git log ^origin/master master
I'm really late to the party, and I'm not sure when it was implemented, but to see what a git push would do, just use the --dry-run option:
$ git push --dry-run
To ssh://bitbucket.local.lan:7999/qarepo/controller.git
540152d1..21bd921c imaging -> imaging
As said above:
git diff origin/master..HEAD
But if you are using git gui
After opening gui interface, Select "Repository"->Under that "Visualize History"
Note: Some people like to use CMD Prompt/Terminal while some like to use Git GUI (for simplicity)
If you have git submodules...
Whether you do git cherry -v or git logs #{u}.. -p, don't forget to include your submodules via
git submodule foreach --recursive 'git logs #{u}..'.
I am using the following bash script to check all of that:
unpushedCommitsCmd="git log #{u}.."; # Source: https://stackoverflow.com/a/8182309
# check if there are unpushed changes
if [ -n "$($getGitUnpushedCommits)" ]; then # Check Source: https://stackoverflow.com/a/12137501
echo "You have unpushed changes. Push them first!"
$getGitUnpushedCommits;
exit 2
fi
unpushedInSubmodules="git submodule foreach --recursive --quiet ${unpushedCommitsCmd}"; # Source: https://stackoverflow.com/a/24548122
# check if there are unpushed changes in submodules
if [ -n "$($unpushedInSubmodules)" ]; then
echo "You have unpushed changes in submodules. Push them first!"
git submodule foreach --recursive ${unpushedCommitsCmd} # not "--quiet" this time, to display details
exit 2
fi
Here's my portable solution (shell script which works on Windows too without additional install) which shows the differences from origin for all branches: git-fetch-log
An example output:
==== branch [behind 1]
> commit 652b883 (origin/branch)
| Author: BimbaLaszlo <bimbalaszlo#gmail.com>
| Date: 2016-03-10 09:11:11 +0100
|
| Commit on remote
|
o commit 2304667 (branch)
Author: BimbaLaszlo <bimbalaszlo#gmail.com>
Date: 2015-08-28 13:21:13 +0200
Commit on local
==== master [ahead 1]
< commit 280ccf8 (master)
| Author: BimbaLaszlo <bimbalaszlo#gmail.com>
| Date: 2016-03-25 21:42:55 +0100
|
| Commit on local
|
o commit 2369465 (origin/master, origin/HEAD)
Author: BimbaLaszlo <bimbalaszlo#gmail.com>
Date: 2016-03-10 09:02:52 +0100
Commit on remote
==== test [ahead 1, behind 1]
< commit 83a3161 (test)
| Author: BimbaLaszlo <bimbalaszlo#gmail.com>
| Date: 2016-03-25 22:50:00 +0100
|
| Diverged from remote
|
| > commit 4aafec7 (origin/test)
|/ Author: BimbaLaszlo <bimbalaszlo#gmail.com>
| Date: 2016-03-14 10:34:28 +0100
|
| Pushed remote
|
o commit 0fccef3
Author: BimbaLaszlo <bimbalaszlo#gmail.com>
Date: 2015-09-03 10:33:39 +0200
Last common commit
Parameters passed for log, e.g. --oneline or --patch can be used.
git show
will show all the diffs in your local commits.
git show --name-only
will show the local commit id and the name of commit.
git diff origin
Assuming your branch is set up to track the origin, then that should show you the differences.
git log origin
Will give you a summary of the commits.

using date in git bash commit message

I am using a .bat file in windows 10 to push changes to an external repo, I have the following code
#!/bin/bash
cd C:\path\to\my\repo
set timestamp=$(date +"%D %T")
git add .
git commit -m "Backup at: `%timestamp%`"
git push origin master
git pull origin master
What I want to do is have the commit message be "Backup at: date" but it just makes it "Backup at: `$(date +T)\`"
Is there a way I can fix this to make it the date?
You could write your script as a regular bash shell script since Windows executes .sh files using Git Bash.
So write it like this:
Example: myBackup.sh
#!/bin/bash
cd /c/path/to/repo
timestamp=$(date +%c)
git add .
git commit -m "Backup at: $timestamp"
git push origin master
echo Press Enter...
read
$(date +%c) Will give you the locale date and time (e.g., Sun, Mar 15, 2020 12:01:53 AM) Use date --help to see full options. no need for set
Use variable like $timestamp not %timestamp% and without the backticks (``)
Use Forward slash / not Backslash \ Backslash works on Windows.

Multiple git commands in single command executed in order they are encountered by compiler

I have following list of commands that I run in respective order so that a source project can be committed and pushed to the repository on Bitbucket:
git init
git remote add origin https://[BitBucket Username]#bitbucket.org/[BitBucket Username]/[BitBucket Repository Name].git
git config user.name "[BitBucket Username]"
git config user.email "[BitBucket Email ID]"
## if email doesn't work then use below ##
git config --global user.email \<\>
git add *
git commit -m "[Comment]"
git push -u origin master
Now instead of putting each and every line at their respective time and order, I want to know, if there is a possibility that I can chain all these into single git command and maintain the same order, something like below ?
git init remote add origin https://[BitBucket Username]#bitbucket.org/[BitBucket Username]/[BitBucket Repository Name].git config user.name "[Username]" ....
Or atleast combine multiple same category params like below ?
git config user.name "[BitBucket Username]" user.email "[BitBucket Email ID]"
I need to know possibility of both scenarios with examples.
We can use list off command in single command for example:
git add . && git commit -m "updated pom file" && git push
or:
git stash pop && git add . && git commit -m "updated pom file" && git push
&& -> if 1st command successfully executes then execute next command else it won't execute next command.
& - it executes all the command
|| - execute next command if 1st one failed
If you are in a Windows Powershell:
git add . ; git commit -m "Testing one line command" ; git push
I have gitbash on Windows system and I am not as good with Win batch as with Linux shell.
You still can write a bash script (interpreted by the msys2 bash embedded with Git for Windows).
As mentioned in the comments by Lasse V. Karlsen, and as I mentioned before in "Which shell used for git '!' aliases?", you can write a shell script in a file (in your %PATH%) named git-xxx, and call it with git xxx.
That script would begin with:
#!/bin/bash
I created a file called reset.txt and in that file I have the commands
git reset --hard
git clean -d -f
[this is a newline - very important to have it]
I just copy and paste this into my terminal and it executes the commands in order.

Add new command on Windows Git Bash

I would like to add new command on my Git Bash (just now i am under Windows OS). I tryed to look on Web different solutions but I did not find anything. The command that i like to add is:
commitall -> git add --all && git commit -m "$*"
There is a way to add this command on Windows Git Bash?
Thanks
Use Git aliases, like so:
git config --global alias.commitall '!git add --all && git commit -m'
There's no need to use $* because all the arguments you specify will simply be appended to the line above, i.e. if you run:
git comitall "a message"
…the following will be executed:
git add --all && git commit -m "a message"

Git on Mac: -a command isn't working

I'm trying to commit all files in a directory, using the git commit -a "added some change" command.
I'm working on Mac OS X, Leopard.
After I submit the command, the Terminal responds with:
fatal: Paths with -a does not make sense.
Could someone please tell me if there's anything I'm doing wrong?
In git one uses the -m option to add a message. -a means all thus commits all changed files
type in your terminal:
git add -a -m "Added some change"
For future problems see
git commit -h
(... omitted output)
-m, --message <message>
commit message
(... omitted output)
Commit contents options
-a, --all commit all changed files
(... omitted output)
try git commit -am "some message"

Resources