Git alias to checkout & pull the default branch of GitHub repositories

Short post here.

In my daily job, I switch between different projects/repositories quite often.
These projects can have different default branches (master, main, develop), and they’re all GitHub repositories.
Something I do frequently is checking out …


This content originally appeared on DEV Community and was authored by Matteo Mazzarolo

Short post here.

In my daily job, I switch between different projects/repositories quite often.

These projects can have different default branches (master, main, develop), and they’re all GitHub repositories.

Something I do frequently is checking out the repositories’ default branch and pulling down the most recent updates.

Since I don’t remember what each repo’s default branch name is, I’m using a GitHub alias that automatically detects it:

# .gitconfig
[alias]
    # checkout and pull the default branch
    m = !git rev-parse --abbrev-ref origin/HEAD | cut -c8- | xargs -n 1 git checkout && git pull origin $(git rev-parse --abbrev-ref HEAD)

I invoke it with git m.

It took me a bit to write this alias, mixing a few answers from this StackOverflow thread: git - how to get default branch?.

I went through some trial and error. I played with GitHub’s gh CLI, git symbolic-ref, and a few other options, but it looks like there’s no silver-bullet solution for this problem yet. Still, this command is working well for my use cases.

On the same topic, I’ve also found this reply to the “How to get the default for the master branch in Git?” StackOverflow question quite insightful.


This content originally appeared on DEV Community and was authored by Matteo Mazzarolo


Print Share Comment Cite Upload Translate Updates
APA

Matteo Mazzarolo | Sciencx (2021-09-04T15:48:00+00:00) Git alias to checkout & pull the default branch of GitHub repositories. Retrieved from https://www.scien.cx/2021/09/04/git-alias-to-checkout-pull-the-default-branch-of-github-repositories/

MLA
" » Git alias to checkout & pull the default branch of GitHub repositories." Matteo Mazzarolo | Sciencx - Saturday September 4, 2021, https://www.scien.cx/2021/09/04/git-alias-to-checkout-pull-the-default-branch-of-github-repositories/
HARVARD
Matteo Mazzarolo | Sciencx Saturday September 4, 2021 » Git alias to checkout & pull the default branch of GitHub repositories., viewed ,<https://www.scien.cx/2021/09/04/git-alias-to-checkout-pull-the-default-branch-of-github-repositories/>
VANCOUVER
Matteo Mazzarolo | Sciencx - » Git alias to checkout & pull the default branch of GitHub repositories. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/04/git-alias-to-checkout-pull-the-default-branch-of-github-repositories/
CHICAGO
" » Git alias to checkout & pull the default branch of GitHub repositories." Matteo Mazzarolo | Sciencx - Accessed . https://www.scien.cx/2021/09/04/git-alias-to-checkout-pull-the-default-branch-of-github-repositories/
IEEE
" » Git alias to checkout & pull the default branch of GitHub repositories." Matteo Mazzarolo | Sciencx [Online]. Available: https://www.scien.cx/2021/09/04/git-alias-to-checkout-pull-the-default-branch-of-github-repositories/. [Accessed: ]
rf:citation
» Git alias to checkout & pull the default branch of GitHub repositories | Matteo Mazzarolo | Sciencx | https://www.scien.cx/2021/09/04/git-alias-to-checkout-pull-the-default-branch-of-github-repositories/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.