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.
Related
I use Vscode, and I want to know Where my HEAD is pointing branch,
How Can I show up the current branch name like Bash?
I use WSL(ubuntu)termimal in my Vscode and OS is Windows 10
Thank you
Note that, from microsoft/vscode issue 67670, the current branch name is already visible in the status bar of VSCode.
Or, with Git 2.22+ (Q2 2019)
git branch --show-current
It is true the prompt in a git bash in VSCode does not display the Git branch.
You need to configure the $SHELL
For example, to enable running bash as a login shell (which runs .bash_profile), pass in the -l argument (with double quotes):
// Linux
"terminal.integrated.shellArgs.linux": ["-l"]
Then in your ~/.bashrc can include a special prompt.
I got it configured by modifying the .bashrc file in the /home/ on the WSL session. You can do vim ~/.bashrc to edit the file.
Find and replace the code block in the .bashrc with this;
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\$(`git branch --show-current 2>/dev/null`)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u#\h:\w\$(`git branch --show-current 2>/dev/null`)\$ '
fi
I found #Diganto Paul's answer does not show the current directory (as it was by default). I used this instead:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[33m\]$(parse_git_branch)\[\033[00m\]\$ '
Copied from: https://hinty.io/ivictbor/show-git-branch-in-bash-on-linux-windows-wsl-2-cygwin-prompt/
I run my game on my Github pages.
and recently started using unity cloud builds.
I managed to get the post-build script running.
but have no clue on how to get it to pull the repository, apply the new build, and push it to GitHub.
So far I have tried this script, without much luck
#!/bin/sh
set -x
ssh-agent $(ssh-add ./id_rsa; git clone git#github.com:triktron/Road-Rage.git tmpResp; cp -r ./WebGL/Build ./tmpResp/;cd ./tmpResp/; git add Build; git commit -m "auto build"; git push)
the logs responded with this
2838: Executing Post-Build Script at upload.sh
2839: ###########################################################
2840: # WARNING: UNPROTECTED PRIVATE KEY FILE! #
2841: ###########################################################
2842: It is required that your private key files are NOT accessible by others.
2843: This private key will be ignored.
2844: ++ git clone git#github.com:triktron/Road-Rage.git tmpResp
2845: Cloning into 'tmpResp'...
2846: ++ cp -r ./WebGL/Build ./tmpResp/
2847: cp: ./WebGL/Build: No such file or directory
2848: ++ cd ./tmpResp/
2849: ++ git add Build
2850: ++ git commit -m 'auto build'
2851: *** Please tell me who you are.
2852: Run
2853: to set your account's default identity.
2854: Omit --global to set the identity only in this repository.
2855: ++ git push
2856: Everything up-to-date
2857: + ssh-agent
2858: SSH_AGENT_PID=5387; export SSH_AGENT_PID;
2859: WORKSPACESIZE | ARTIFACTSSIZE
2860: --------------|--------------
2861: 154.10 MiB | 12.79 MiB
thanks already for your time!
[Update 23 march 2020]
so i've spent more time on this then i'm willing to say but i figured it out.
here is my solution if anyone ever wants to do the same.
i placed this script in my files
#!/bin/sh
set -x
export buildfolder="$(find . -regex '.\/temp[^\/]*\/WebGL\/Build' -print -quit)"
if [ -z "$buildfolder" ]; then
echo "Could not find build folder"
exit 1
fi
if [ ! -d ./tmp ]; then
git clone "https://${nickname}:${githubkey}#github.com/${nickname}/${repositorie}" ./tmp
fi
cp -r "$buildfolder" ./tmp
cd ./tmp
git add Build
git config --global user.email "$githubemail"
git config --global user.name "$nickname"
git commit -m "unity cloud build"
git push --force
and added these envirement varibles in my unity cloud build console
picture of envirement vars
i will be running a automated deployment on every day.
so i need to create a child branch from develop, is there any way to create a branch from develop with the current system time. just like below
git checkout -b 'date +"%d-%m-%y"-dev-release-"%I"'
git checkout -b `date +"%d-%m-%y"-dev-release-"%I"`
or
git checkout -b $(date +"%d-%m-%y"-dev-release-"%I")
Bacticks and $() means: execute the command and replace the command with its output.
See http://mywiki.wooledge.org/CommandSubstitution
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.
I use git-svn to interact with an existing SVN repository that contains some C++ projects. subwcrev.exe is used as a pre-build event to update some strings in a C++ header (svnversion.h). These strings get hardcompiled to form some version information for the resulting binary.
Since subwcrev requires .svn metadata to work, the pre-build event is going to fail when used on the git-svn working copy. So I came up with the following bash script which I use as post-commit and post-checkout hooks for my git repository. The script tries to do the same thing as subwcrev based on the output of git svn info (cached in a local file).
#!/bin/sh
if [ ! -f svninfo ] ; then
git svn info > svninfo
fi
revision=`sed -e "/Revision/!d" -e "s/Revision: \(.*\)/\1/" svninfo`
lastchange=`sed -e "/Last Changed Rev/!d" -e "s/Last Changed Rev: \(.*\)/\1/" svninfo`
# Get the last changed date, extract timestamp, replaces dashes with slashes
changedate=`sed -e "/Last Changed Date/!d" -e "s/Last Changed Date: \(.\{19\}\).*/\1/" -e "s!-!\\\\\\/!g" svninfo`
now=`date "+%Y\/%m\/%d %H:%M:%S"`
gitcommit=`git show --abbrev-commit | sed -n -e "s/commit //p"`
for entry in $( find -name svnversion_template.h ); do
newname=`echo $entry|sed -e "s/_template//"`
sed -e "s/\\\$WCRANGE\\\$/${revision}/" \
-e "s/\\\$WCREV\\\$/${lastchange}-${gitcommit}/" \
-e "s/\\\$WCDATE\\\$/${changedate}/" \
-e "s/\\\$WCNOW\\\$/${now}/" \
-e "s/\\\$WCURL\\\$/local git repo/" \
-e "s/\\\$WCMODS.*\\\$/(true)/" \
-e "s/\\\$WCMIXED.*\\\$/(false)/" \
$entry > `echo $entry|sed -e "s/_template//"`
done
What I cannot really emulate so far is the automatic detection of a local uncommitted changes (based on the last checked out SVN revision) that makes subwcrev so useful.
I am replacing $WCREV$ with the revision number of the SVN repository (as subwcrev would do) but this time I add my abbreviated git commit hash to identify the code I compiled. My question now is: Is there a way to distinguish in a shell script whether my current HEAD differs from the last fetched SVN revision so that I could omit adding the -${gitcommit} part and set $WCMODS$ to false?
If there were some thing like a post-"git svn dcommit" hook, my problem would be solved, too, since then that special hook would create the svnversion.h differently. Can such hook be added somehow?
I don't really get your points, but start to improve your script first.
revision=$(grep -Po "(?<=Revision: ).*" svninfo)
lastchange=$(grep -Po "(?<=Last Changed Rev: ).*" svninfo)
# Get the last changed date, extract timestamp, replaces dashes with slashes
changedate=$(grep -Po "(?<=Last Changed Date: ).{19}" svninfo)
changedate=${changedate//-//}
now=$(date "+%Y\/%m\/%d %H:%M:%S")
Then, in for loop, could you please explain detail, what result you need?
Can you show one sample of svnversion_template.h ?
So it looks like you might have to parse the contents of the git svn info query yourself to get what is normally stored in WCREV. The example results look like this for me:
git svn info
Path: .
URL: http://myurl.com/trunk/myrepo
Repository Root: http://myurl.com
Repository UUID: 15fed3e9-81ce-ef4a-a7da-fc36e3df1edc
Revision: 14106
Node Kind: directory
Schedule: normal
Last Changed Author: myusername
Last Changed Rev: 14106
Last Changed Date: 2015-05-29 10:23:10 -0400 (Fri, 29 May 2015)
Now for the second part of your question, whether or not you can tell if your git HEAD matches the latest svn checkout, you'll need to use the command git diff git-svn command. "git-svn" here is the name of the branch that the git-svn program is maintaining, and if everything is up to date, the results will be empty.