Git commit --amend replace word in message script - bash

I'm trying to write a bash script that runs
git commit --amend
and my default editor is Vim. In the commit message I want to replace
Reviewed: notreviewed
to
Reviewed: $myarg
How does one do this with a script?

This isn't the best option (it relies on myarg not containing any / characters, or anything that could terminate the sed command prematurely), but a single filter-branch command can be used in place of git commit --amend.
git filter-branch --msg-filter "sed '/Reviewed:/s/notreviewed/$myarg/'" HEAD

Related

How to use a line of hyphen to separate git command results within git alias in git-bash

I've already created a alias called info to display status, branch and log in git-bash. However, all the information was bunched up and somewhat annoying to read, so i wanted to make it easier on myself when reading it by adding a line of hyphens with new line above and below it. After trying many times, I can't get it to work. Therefore i'm seeking help here.
Here's what I tried before, trying to get one to work before copying it in between git branch and git log.
$ git config --global alias.info '!git status && echo && !printf -- '-%.0s' {1..80}; echo "" && echo && git branch && git log'
Below is what the result should look like.
git status result
-----------------------
git branch result
-----------------------
git log result
If the ! syntax doe not give the expected result, try and embed your commands in a shell function within your Git alias:
alias.info !f() { echo begin arg=$1/$2/end; }; f
Or even in a separate script:
git config --global alias.info '!sh info.sh'
The point is: if you can make it work in a regular shell script, you will be able to call it from an alias.
Or you can even name your script git-info (executable, no extension), and simply type:
git info
No alias needed there: any git-xxx script can be called as git xxx (if git-xxx is in your PATH)

Always prompt for a stash message in git

I tend to stash changes without remembering why I stash them.
I do make it a point to git stash push -m most of the time, but if there's a fire drill or something else that knocks me out of flow, I may forget and lose time trying to recover.
Is there a way to imitate the behavior of git commit (minus the -m) for git stash where vim pops up and abandons the operation if the message is empty?
AFAIK there's no config option for this. You'll have to write an alias in your .gitconfig and train yourself to use it.
For example, I have two stash aliases git pop and git save. (You can see I didn't get the memo about git stash save being deprecated). These are both for convenience, and to change the default behavior to something I find more useful.
save = stash save -k -u
pop = stash pop
Unfortunately git stash push -m doesn't bring up an editor, if you need to write more than a few words to describe what you were doing consider a branch instead. We can fix this by writing a little shell function and passing the argument to -m using "$#" to ensure messages with spaces are a single argument.
savem = "!f() { git save -m \"$#\"; }; f"
Now you can write git savem 'remember to remember what this was'.
$ git savem 'remember to remember what this was'
Saved working directory and index state On issue/45: remember to remember what this was
And if you forget, you'll get the normal git-stash usage message. You can snazz up the alias to provide a custom usage message if you like.
$ git savem
usage: git stash list [<options>]
or: git stash show [<stash>]
...
To me it makes sense to consider using a branch for this. It seems like you want to keep the changes. Branches can be named so it’s easier to recall what was being worked on. These can be local or pushed to remote in case it wasn’t a drill.
$ git branch topic/wip
If you want continue work on master yo can do a
$ git checkout master
Not pretty but could be achieved using bash + vipe in moreutils
msg="$(< /dev/null vipe)";
[[ -z "$msg" ]] || git stash -m "$msg"

Git checkout by date gives error on Windows

I want to checkout from git and using commands from this SO-link. I am not sure what I am doing wrong here.
I have tried following commands.
git checkout `git rev-list -n 1 --before="2015-3-3 13:37:00" master`
git checkout `git rev-list 1 --before="2015-3-3 13:37:00" master`
git checkout `git rev-list -1 --before="2015-3-3 13:37:00" master`
and get the following error.
C:\Users\junaid\Documents\GitHub\bucardo [master]> git checkout `git rev-list -n 1 --before="2015-3-3 13:37:00" master`
error: unknown switch `n'
usage: git checkout [options] <branch>
or: git checkout [options] [<branch>] -- <file>...
-q, --quiet suppress progress reporting
-b <branch> create and checkout a new branch
-B <branch> create/reset and checkout a branch
-l create reflog for new branch
--detach detach the HEAD at named commit
-t, --track set upstream info for new branch
--orphan <new branch>
new unparented branch
-2, --ours checkout our version for unmerged files
-3, --theirs checkout their version for unmerged files
-f, --force force checkout (throw away local modifications)
-m, --merge perform a 3-way merge with the new branch
--overwrite-ignore update ignored files (default)
--conflict <style> conflict style (merge or diff3)
-p, --patch select hunks interactively
--ignore-skip-worktree-bits
do not limit pathspecs to sparse entries only
But following command works but I think, it can go back to 90 days only.
C:\Users\junaid\Documents\GitHub\bucardo [master]> git checkout 'master#{2015-3-27 18:30:00}'
warning: Log for 'master' only goes back to Thu, 17 Mar 2015 18:08:03 -0500.
Note: checking out 'master#{2015-07-27 18:30:00}'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at cb4f95a... Start the cleanup of delete_rows()
C:\Users\junaid\Documents\GitHub\bucardo [(cb4f95a...)]>
In Bash (and many other Unixy shells) backticks are used for command substitution, e.g. see this description from The Linux Documentation Project:
Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed like this:
$(command)
or like this using backticks:
`command`
Bash performs the expansion by executing COMMAND and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.
franky ~> echo `date`
Thu Feb 6 10:06:20 CET 2003
I don't think that cmd.exe supports real command substitution at all. It certainly doesn't treat backticks this way.
There are some workarounds that you can try, e.g. using for /f or temporary files, but these are awkward to use.
Depending on how you installed Git you might have access to a Bash shell on Windows that should work the way you want.
If you have access to PowerShell it looks like the $(...) syntax should work, which is also supported on Bash. posh-git has a good reputation as an environment for Git on PowerShell, though I've never used it personally.

Is there a way to allow spaces in bash bang pattern (repeating last command)

I was wondering if there is a way to repeat a bash command with a subcommand separated by space. For example, if I enter several commands,
git add a.txt
git status
... other commands starting with git
git commit -m ""
and do:
!git
I will run the last git commit command again. My questions, is there a way to repeat the last command that contains a space, e.g. to repeat the last git status command?
I tried,
!git\ s
!"git s"
, but none works.
Try:
!?git s
The "!?string" event designator searches for the most recent command preceding the current position in the history list containing string.
Also you could use this to refer to the command n lines back:
!-n
In this case, you could hit CtrlR , type "git s" then hit Enter
Though related, but not exactly answering your question ...
Try adding the following two lines to your ~/.bashrc
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
FUNCTIONALITY (after you source ~/.bashrc or open a new terminal):
when you start typing i.e. "git" and hit the up or down arrow, it will search through your history completion to complete what's already on the line.
Reference
You can also use the fc bash builtin command:
$ git status
fatal: Not a git repository (or any of the parent directories): .git
$ git diff
usage: git diff [--no-index] <path> <path>
$ fc -s 'git s'
git status
fatal: Not a git repository (or any of the parent directories): .git
$

Add line break to git commit -m from command line on Windows

My company has a policy that all checkins to a particular project must follow a specific multi-line template for git commits. How can I most simply create a single commit message that has multiple lines from the command line in Windows?
This is almost exactly a duplicate of "Add line break to git commit -m from command line" except that this question is specific to Windows/cmd.exe, while that question is related to bash.
Either create a file that contains your commit message in the right format and use git commit -F <message_file>, after which you can continually edit and reuse that same file, or create a template file and use git commit -t <template_file> to open your editor with the pre-cooked template to be filled in. The second option can be made permanent by setting the commit.template configuration variable, so you don't need to use the -t ... bit on every commit. See the git commit manual page (git help commit, if your git is installed correctly, or search online) for more information.
You can create multiline commit message like this:
C:\> git commit -m "Line 1"^
More?
More? "Line 2"^
More?
More? "Line 3"
Note, that the circumflex character is only on odd lines.
git commit -m "Subject" -m "Description..."
You can also use interactive rebase and then reword for editing the commit's message.
Type git commit -m "doesnt really matter whats here" and then git rebase -i HEAD~1, replace pick with r or reword, save and then edit the message.

Resources