create a git branch with the current system date - bash

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

Related

git checkout -f staging fatal: cannot create directory at 'public/C:\xamppphp8\htdocs\caab-hcc\storage\logs': Invalid argument

I am try to checkout git checkout -f staging this way from widows PowerShell (my operating system is windows 10 )
git checkout -f staging
git checkout -q --track origin/staging
fatal: cannot create directory at 'public/C:\xamppphp8\htdocs\caab-hcc\storage\logs': Invalid argument
I can check out other branch easily from same repository but when I am going to use Staging branch i am getting this error
its Laravel project
> git for-each-ref --format %(refname:short)%00%(upstream:short) refs/heads
> git checkout -q --track origin/staging
fatal: cannot create directory at 'public/C:\xamppphp8\htdocs\caab-hcc\storage\logs': Invalid argument
> git status -z -u
> git symbolic-ref --short HEAD
> git for-each-ref --format=%(refname)%00%(upstream:short)%00%(objectname)%00%(upstream:track) refs/heads/bangla_inpout_field_with_new_api refs/remotes/bangla_inpout_field_with_new_api
> git for-each-ref --sort -committerdate --format %(refname) %(objectname) %(*objectname)
> git remote --verbose
Warning: Failed to watch ref 'c:\xamppphp8\htdocs\caab-hcc-1\.git\refs\remotes\origin\bangla_inpout_field_with_new_api', is most likely packed.
> git config --get commit.template
Without -f flag
git checkout staging
error: The following untracked working tree files would be overwritten by checkout:
bootstrap/cache/config.php
bootstrap/cache/packages.php
bootstrap/cache/routes-v7.php
bootstrap/cache/services.php
Please move or remove them before you switch branches.
Aborting
The error already tells you the problem. You have made changes in your current branch, that will be overwritten by checking out the staging branch.
There are two solutions to your problem:
Remove the changes in your current branch (git checkout .) You will lose your changes.
If you want to also remove added files and you do not care about losing progress, use: git reset --hard;
git clean -df
Use git stash to stash your changes. Your stash can be re-applied at a later point.
Afterwards, git checkout staging should work again.

Git short command for checking out, pulling, prune fetching and deleting

Is there a shortcut for:
Checking out on master
Pulling from master
Prune Fetch (check which remote branches are removed)
Delete those local branches
Scenario:
Let's say I'm on master and I checkout on the branch foo, I do some commits and publish foo to remote and push to it as well. Next I merge that branch to master on GitHub and delete the online version of foo since it's complete. Now in the offline environment, I have to do the following:
$ git checkout master
$ git pull
$ git fetch -p
$ git branch -d foo
or shorthand:
git checkout master && git pull && git fetch -p && git branch -D foo
Is there a command I can execute to make this much shorter? Like
$ git complete foo
or something along those lines..?
aliases can be used for solving this problem.
An alias can be created by running:
$ alias cpfb="git checkout master && git pull && git fetch -p && git branch -D"
Now, you can execute
$ cpfb foo
which will execute those commands specified in the alias.
Setting alias through terminal lasts for only that particular terminal instance.
Hence, save them in ~/.bashrc to make the alias permanent.

Error when creating all branches at once

I have created this bash script to create all branches at once
#!/bin/bash
git fetch -vp
for b in $(git branch -a | grep remotes | grep -v HEAD)
do
branchname=${b##*/}
remote=${b#*/}
command="git branch --track $branchname $remote"
echo "$command"
$($command)
done
but I am always having the same error:
fatal: 'master' is not a valid branch name.
If I run the same command without the script the it is executed successfully.
What am I doing wrong ?
I have created this script because I need push all my branches to another remote repo, so first I need create the all local branches from original repository...
You don't.
You can use the remote tracking branches which you already have from git fetch. That's like origin/master. git fetch has already downloaded all the commits from the remote, and branches in Git are just labels on commits. Even remote branches.
You can get a list of all your remote tracking branches from git branch -r but that's from all remotes. To get the branches for just one remote use git ls-remotes --heads <remote>. The format is a little funny, so you have to do some massaging.
$ git ls-remote --heads $REMOTE | cut -f2 | sed -E "s/refs\/heads/$REMOTE/"
origin/80_method_methods
origin/gh-pages
origin/io-all
origin/issue/217
origin/issue/255
origin/master
origin/rewrite_cmd_wrapper
Then you can push from those branches.
Though wanting to push all the branches from one repo to another is very odd. There's probably a simpler way to solve whatever problem you're trying to solve. I suspect you have an XY Problem.
I would suggest asking a question about that problem instead.
As it is well explained in #Schwern's answer, wanting to "checkout all branches" of a remote repo is probably a XY problem, and it is unneeded in practice because git fetch $REMOTE is enough to retrieve the whole history of the considered repo.
However the proposed command git ls-remote ... does not seem to be appropriate because:
it needs a network connection
it doesn't display the list of remote branches
it just queries the remote repo (without relying on fetched data) to know if the commits corresponding to local branches happen to be known in the remote.
In order to efficiently get the list of all branches from a given remote, I propose instead:
git for-each-ref --format "%(refname:strip=2)" "refs/remotes/$REMOTE/" | grep -v -e '/HEAD$'

How to reset the entire Git repository, and not only the "Master" branch, to match Remote?

What the title says.
I want to reset each and every local branch to match my Remote repository, including deleting some branches and tags which only exists locally, without having to delete everything and cloning from scratch. All I could find are instructions on how to reset a specific branch, but not the entire repository.
Even better if it can be done from the TortoiseGit Shell extension. But I'm also fine with the command line.
You can do it by following commands:
git checkout --orphan #
git fetch <Remote> refs/*:refs/* --refmap= --prune --force
where <Remote> is remote repository you want to use. You simply fetch all remote refs (refs/*:refs/*) with --prune and --force flags to remove and force update local references.
The following line will reset all local branches that have a configured upstream branch to the state of the upstream branch
git checkout #{0} && git for-each-ref refs/heads --format '%(refname:strip=2)' | xargs -ri sh -c 'git rev-parse {}#{u} >/dev/null 2>&1 && git branch -f {} $(git rev-parse {}#{u})'
You will end up with a detached HEAD due to the first command, because you cannot reset the currently checked out branch, so checkout the branch you want to have in your working directory after doing this.

Bash conditional based on whether a file has changed since last pull

I'm writing a script for automatically updating a system of ours. Basically I want to do a pull and update from a remote hg repository, and then run some update scripts. Now the problem is that these update scripts takes a while to run and most of them only has to be run if there has been changes to their configurations.
Now my script looks like following:
if hg pull -u
then
run scripts
fi
What I want is something like
if hg pull -u && 'some changes was introduces in my/configuration/directory/*'
then
run scripts
fi
Any idea how to do this?
With templated output of hg incoming your can check before pull, which files will be modified on pull (if any will be) and will act accordingly
hg incoming --template "{files % '{file}\n'}" | grep SOMESTRING
You can use hg status to get a list of files that have been changed between revisions, for example files modified between tip and it's parent(tip^) that are in my\configuration\directory:
hg status my\configuration\directory\** -m --rev "tip^:tip"
I would recommend to pull, check if those files have been altered from the current revset, update, and then run your scripts if your config has changed. To me that looks easier than trying to store which revset you started with and figure it out after the update. (Note that I'm not great with bash/grep, so this is approximate and untested):
hg pull
cfgChngd = hg status -m my\config\dir\** -m --rev tip | grep "M my\config\"
hg update
if cfgChngd
runAllTheScripts
fi
You can use the status command to get a list of changed files -- also between different revisions:
HERE=`hg log --template '{node}' -r.`
hg pull -u
if hg st --rev $HERE:. | grep -q .
then
run scripts
fi

Resources