0

A very popular question here on SO is:

How do I check out a remote Git branch?

but the answers there either assume the branch-of-interest is already fetched, or suggest that we git fetch first.

My question: Is it possible to checkout a remote branch which has not yet been fetched, with a single git command?

Note: You may assume as new a version of git as you like, if it matters.

asked Dec 18, 2024 at 9:17
19
  • 1
    Why would you like it as a single git command? Commented Dec 18, 2024 at 9:28
  • 3
    Your "no fetch" constraint seems arbitrary. Why is it a problem? How do you expect your machine to read distant data without said data being transported over network at some point? Are you searching for alternatives to git protocol? Commented Dec 18, 2024 at 9:42
  • 2
    Your explanation about it being a single step "in english" is technically irrelevant. Compare with someone asking "Please write function X to add two variables". There is no information that I need to type characters, so my IDE shouldn't need it either. (Not trying to be the snark knight here, just desperately trying to understand your odd question setting) Commented Dec 18, 2024 at 9:46
  • 2
    Atomic commands are useful, and composite commands are also useful. But ignoring the underlying steps doesn't magically make them unexist. If your local machine needs to display some data, this data has to come accross the network at some point, in some way. If you're asking if there's a composite command to combine fetching and checking out, I don't find that absurd at all, yes it could be useful, but no it doesn't exist as such. Aliases are here to do this sort of small command grouping. Commented Dec 18, 2024 at 9:49
  • 1
    @einpoklum So far as I know, there is no shortcut command that combines git fetch + git checkout/switch yet. We can create and checkout a branch without git fetch, but the branch may not point at the right commit. I asked because I want to know your initial motivation to this question. You may think a single command could solve your problem. I want to make it clear and maybe find another solution. Commented Dec 18, 2024 at 10:14

1 Answer 1

0

If you don't have the branch fetched at all, you could create an alias. In ~/.gitconfig:

[alias]
 checkout-with-fetch = "!f() { git fetch origin 1ドル >/dev/null && git checkout -b 1ドル origin/1ドル }; f"

This will run a git fetch, but at least you get the illusion that it didn't... however I wouldn't recommend overwriting git commit as this is bound to cause you to unintentionally lose some work later on.

answered Dec 18, 2024 at 9:32
Sign up to request clarification or add additional context in comments.

4 Comments

So, that's not really a single git command, and will obviously not work on systems other than my own, but - it's a decent hack I suppose.
@einpoklum I don’t understand why it needs to be a single existing Git command. Commands are made to be composed.
@Guildenstern: 1. Because other "single" commands are already "composite", like creating a new branch + checking it out, or even create + set-remote + checkout. 2. Because it sounds like a single command in English. 3. Because composition complicates things, requiring either more typing or setting state, like the alias you offered.
I think the alias is missing at least one semicolon

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.