Search code examples
gitgit-checkoutgit-fetch

How to fetch and checkout in Git at once?


I use git version 1.7.7.5.

Is that possible to do the following using one git command?

git fetch git@github.com:someone/repo.git someones_branch
git checkout FETCH_HEAD -b my_testing_branch

Solution

  • Note that you can set the upstream branch of my_testing_branch

    git branch --set-upstream my_testing_branch someone/someone_branch
    

    (here using a remote named 'someone')

    And simply git pull on it (but that would still require: git checkout my_testing_branch + git pull)

    To answer your question, one way would be to define a git alias

    git config --global alias.fetch-github '"!f() { git fetch git@github.com:$1/$2.git $3 ; git checkout FETCH_HEAD -b $4 }; f"'
    

    You would use it like so:

    git fetch-github someone repo someone_branch my_testing_branch