4

Every time I run 3 commands when I need to test my team member code on local.

Like that:

git fetch remote_name branch_name 
git checkout branch_name
git pull origin master

or

git fetch remote_name branch_name && git checkout branch_name etc...

Because normally after fetch it we always checkout into it than we need pull from origin master. If we can run one command to done all those step it will faster.

Does git has a command to fix that?

Liam
  • 27,717
  • 28
  • 128
  • 190
Kakada NEANG
  • 451
  • 1
  • 6
  • 16
  • Create a bat file? Use a UI like [git extensions](https://gitextensions.github.io/)? – Liam Jul 26 '17 at 10:57
  • 1
    You can create an alias too. – Víctor López Jul 26 '17 at 10:59
  • @Liam, I use git bash on ubuntu – Kakada NEANG Jul 26 '17 at 11:02
  • 1
    @KakadaNeang make an alias as @Víctor López suggests or define a simple function in `~/.bashrc`. `function foo(){ git fetch remote_name branch_name && git checkout branch_name && git pull origin master }`, then run `foo` will do the job. If `remote_name` and `branch_name` are not constant, replace them with `$1` and `$2` in the function, and run `foo remote_name branch_name`. After editing ~/.bashrc, you need to source it by `source ~/.bashrc` or restart the shell window. – ElpieKay Jul 26 '17 at 11:41
  • It would help if you mention this in your question and/or add the relevant tags – Liam Jul 26 '17 at 12:30
  • There's another good motivation for such a combination: to get around conflicts. for example, if you switch back to an old master branch and have some locally modified files, but the current master branch would be similar to what you already have. for this reason, `git checkout` often fails and I have to do the following: `git stash && git checkout master && git pull && git stash pop` (I omit the `git fetch` because this is done by `git checkout` already) – Daniel Alder Nov 04 '20 at 09:42

1 Answers1

3

If you use this combination of commands often, you might want to add the function to your shell, as suggested by @ElpieKay. For example, if you use bash or dash, then adding the following code to your ~/.bashrc will allow you to type foo remote_name branch_name which will be equivalent to the statements in your question.

function foo {
    git fetch $1 $2 && git checkout $2 && git pull origin master
}

If you would rather type git foo remote_name branch_name, it is possible to create multi-statement git aliases as answered in this question.

Dekker1
  • 5,565
  • 25
  • 33
  • Why not just `git pull`? I never write `git pull origin master`. And good that you shorten the other commands as well. What the question does, taking the branch names as the parameters, is not needed. – questionto42 Oct 20 '22 at 13:34