WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Git · Advanced · question 49 of 100

How do you use ’git refspec’ to configure advanced push and fetch behaviors?

📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

In Git, a refspec is a string that defines the mapping between local and remote references. It is used to specify the source and destination references for Git push and fetch operations. A refspec can be used to configure advanced push and fetch behaviors, such as pushing a local branch to a remote branch with a different name or fetching only specific branches from a remote repository.

The basic syntax of a refspec is:

    <source>:<destination>

where <source> and <destination> are references to commits or branches. Here are some examples:

Push a local branch to a remote branch with a different name:

    git push origin local-branch:remote-branch

Delete a remote branch:

    git push origin :remote-branch

Fetch only specific branches from a remote repository:

    git fetch origin branch1 branch2

Fetch all remote branches and tags:

    git fetch --all --tags

Fetch a single tag:

    git fetch origin tagname

Fetch a specific commit:

    git fetch origin <commit-hash>:<destination-branch>

It’s important to note that refspecs can be quite powerful but can also be dangerous if not used carefully. It’s always a good idea to double-check the refspecs before executing any push or fetch command. Additionally, some hosting services such as GitHub allow repository administrators to restrict the types of refspecs that can be used in order to prevent accidental data loss or corruption.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Git interview — then scores it.
📞 Practice Git — free 15 min
📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

All 100 Git questions · All topics