spenibus.net
2015-05-20 12:04:26 GMT

Git bash - Create remote repositories quickly on github and gitlab

Preamble -- I'm a fairly casual user of Git. I use on average 4 commands, all aliased for speed. For reference, full list below. File: .gitconfig -- ```` [alias] s = status aa = add -A cm = commit -m pam = push all master paa = push --all all ```` My usual git console input tends to look like this: ```` git s git aa git cm "blergh" git paa ```` And of course, there is the occasional ```` git checkout -f" aka "fuck it, not worth it ```` I don't make a very advanced use of Git and I try not to waste time using it. Purpose -- This brings us to the point of this post: optimizing the creation of the project's remote repositories. I mirror code between github.com and gitlab.com and using the web interface on each website to setup my new projects is an annoying and repetitive task. Annoying and repetitive might very well be adjectives that have spawned countless lines of code and today will be no exception. We are going to speed up the entire process and neatly encapsulate it into a single keyword, using a bash function and websites API. Setup -- 1. Generating tokens github: <https://github.com/settings/tokens> gitlab: <https://gitlab.com/profile/account> 2. Token storage Since I use an encrypted volume for sensitive data, it will be used here. Where you keep your data is up to you. Tokens being as important as passwords, be careful. Each token goes into its own file: >github.token.txt gitlab.token.txt Make sure there are no extra characters in those files, such as whitespaces. 3. Find or create the file `.bashrc` in your user profile. It should be alongside `.gitconfig`. Add the following code: ```` function gitnew() { # token path tokenPath='z:/path/to/token/files' # name of remote remoteName='origin' # github user user_github='username' # gitlab user user_gitlab='username' ##### no user edits below this point # repo name repoName="$(basename "$PWD")" # check if path exists if [ ! -d "$tokenPath" ] # no path then echo 'error: token path not available' # path available else echo 'config: github' git remote add "$remoteName" 'git@github.com:'"$user_github"'/'"$repoName"'.git' echo 'config: gitlab' git remote set-url "$remoteName" --add 'git@gitlab.com:'"$user_gitlab"'/'"$repoName"'.git' echo 'querying github' curl \ -u "$user_github":"$(cat "$tokenPath"'github.token.txt')" \ 'https://api.github.com/user/repos' \ -d '{"name":"'"$repoName"'"}' echo 'querying gitlab' curl \ -H 'PRIVATE-TOKEN: '"$(cat "$tokenPath"'gitlab.token.txt')" \ 'https://gitlab.com/api/v3/projects' \ --data-urlencode 'name='"$repoName" \ --data-urlencode 'public=true' fi } ```` Make sure to edit `tokenPath`, `remoteName`, `user_github` and `user_gitlab`. And that's it really. All you have to do now is create a new folder, name it properly and open a bash console from it. Then type: ````txt git init gitnew ```` `gitnew` will automatically use the folder's name to create the remote repositories and add both remotes to the config under the same name. Full setup ready in less than 10 seconds. We're done here. Go away.