DEV Community

Cover image for How to write Bash one-liners for cloning and managing GitHub and GitLab repositories

How to write Bash one-liners for cloning and managing GitHub and GitLab repositories

Victoria Drake on August 06, 2019

Edit: An earlier version of this post's title seemed to suggest I encouraged moving away from GitHub. This was not, in fact, the intention, so the ...
Collapse
 
flrichar profile image
Fred Richards • Edited

This reminds me of something I perform all of the time... not pure bash, but helpful nonetheless.

I have about ~50 git repos I'm pulling down and tracking (others' projects, not mine), so this gnu parallel command says "enter each git repo in the current dir and perform a git pull".

cd path-to-git-repos
parallel cd {}\; git pull ::: `ls`

Best part ... it runs in parallel. So I have 4 cpus and the git repos have nothing to do with each other... it runs four pulls at once.

Collapse
 
victoria profile image
Victoria Drake

That’s awesome!

Would you explain the ::: part? I haven’t seen this syntax before. All I can find is that : means true or something like that.

Collapse
 
flrichar profile image
Fred Richards • Edited

It uses the output of the command as an input source. $(ls) would have done the same thing... I like to think of it as performing the operation on a current set.

It doesn't look familiar because its a specific parallel command option, not a general bash option.

Another silly example of a clock-counter ... the -k flag says keep the order (so things don't happen out of order). Perform an echo operation on three sets.

parallel -k echo {1} {2} {3} ::: {0..12} ::: {0..5} ::: {0..9}
Collapse
 
dmitriz profile image
Dmitri Zaitsev

GitLab, unlike GitHub, lets us do this nifty thing where we don’t have to use the website to make a new repository first.

Wow, that really illuminates how Github forces us into suboptimal routines. Another example is updating a fork, that should really have been a single button click, not the local pull-push wrestlings.

Collapse
 
tamouse profile image
Tamara Temple

Github has a utility called hub which provides the ability to create repos, fork them, create pull-requests, and few other things. It's meant to be a substituted for the git command since it implements all the git subcommands as well. github.com/github/hub

It's still not as cool as gitlab's "push-to-create"

Collapse
 
dmitriz profile image
Dmitri Zaitsev

I have been using hub but didn't know that was possible.
But I have now tried and failed to get it working:

hub create
Error creating repository: Unauthorized (HTTP 401)
Bad credentials

Don't know if that is my hub version 2.2.9, and I can't get their latest version work, probably due to my old OSX, even though none of the errors said that.

No comparison with Gitlab in both convenience and usability, where there is no need to download anything.

Thread Thread
 
tamouse profile image
Tamara Temple

This is something I set up so long ago, I don't even remember what I did.

All props to Gitlab for making it painless

Collapse
 
vinayhegde1990 profile image
Vinay Hegde • Edited

Nifty aliases to simplify daily Git tasks, Victoria! Adding a few more I made that might help:

# Setting upstream on creating a new branch in any GIT repo
function git-new-upstream () {
git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD)
}

# Check if branch on local exists on remote, provide branch name as $1
function git-remote-check () {
git ls-remote --heads origin $1
}
Enter fullscreen mode Exit fullscreen mode

Note: These should be added in .bashrc & then reloaded either via by source .bashrc or opening a new terminal session

Collapse
 
csmyujinkim profile image
csm-yujinkim

I learned about the xargs command. Thanks a bunch!

Collapse
 
victoria profile image
Victoria Drake

I’m so glad! You’re welcome!

Collapse
 
shostarsson profile image
Rémi Lavedrine

Very interesting.
I made something similar for Bitbucket : github.com/Shosta/DLBitbucketRepos
It clones all the repositories you own on Bitbucket.