Supply the same filename as argument to two commands

On a git conflict, I can currently do:

git diff path/to/some/file.txt

… after reviewing the diff, usually I want to do:

git checkout --theirs path/to/some/file.txt && git add path/to/some/file.txt

It’s painstaking to edit both paths each time, so I’d like to be able to do the following:

git checkout --theirs <ref> && git add path/to/some/file.txt

Where the <ref> refers to file.txt.

Any way to do this in bash?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There are many ways.

You can use history expansion in (t)csh, bash or zsh. !$ expands to the last argument of the previous command line.

git checkout --theirs !$ && git add !$

Bash, ksh and zsh have a variable $_ which stores the last argument of the last simple command executed by the shell.

git checkout --theirs "$_" && git add "$_"

Bash and zsh have a keyboard shortcut Alt+. or ESC . which inserts the last argument of the previous command line.

git checkout --theirs Alt+. && git add Alt+.

You can stuff this in a function.

git_add_theirs () {
  if [ "$#" -eq 0 ]; then set -- "$_"; fi
  git checkout --theirs -- "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="301470">[email protected]</a>" && git add -- "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="133753">[email protected]</a>"
}

Or make that a standalone script and define an alias add-theirs = git_add_theirs in your ~/.gitconfig.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x