3

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?

EoghanM
  • 887
  • 2
  • 10
  • 15
  • 1
    `f='path/to/some/file.txt'; git checkout --theirs "$f" && git add "$f"` – Costas Feb 11 '15 at 12:57
  • You could use a history expansion maybe? `git checkout --theirs path/to/some/file.txt && git add !#:3` (repeat the third [zero-indexed] word `:3` from the current command line `#`) – steeldriver Feb 11 '15 at 13:19
  • 1
    With _yank-last-arg_: _Alt+._ (META and DOT keys) you can insert the last argument to the previous command so after running `git diff path/to/some/file.txt` you could just type `git checkout --theirs ` hit _Alt+._ then type `&& git add ` and hit _Alt+._ again. – don_crissti Feb 11 '15 at 13:38
  • 1
    @don_crissti - `bash`, `ksh93`, and `zsh` put the same info in `$_`. – mikeserv Feb 11 '15 at 15:14

1 Answers1

5

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 -- "$@" && git add -- "$@"
}

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

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • I prefer `$_` to `!$` as the latter inserts the filename into the bash history which is not good when you want to repeat the command sequence multiple times. – EoghanM Feb 12 '15 at 16:59