3 minute read
How to Create a Git Repo & Push Code In One Command
A quick look at how to create, name and push your code into a github repo with a one-liner command using Github CLI.
I find myself forgetting to track changes until I‘ve spent a few hours and think ‘oh, I should really track/save this’.
This is bad practice and I need to make a concerted effort to do better.
So, i’m writing this article to remind myself, and anyone that relates, to git init sooner AND to pickup and study Github’s Command Line Interface. Github’s CLI makes creating project repos a breeze.
Prerequisites
Git installed
Github CLI installed
TL;DR:
git init
git add .
git commit -m "initial commit - a new thing"
gh repo create my-newrepo --public --source=. --remote=upstream --pushInitialising a local repo
Lets assume you have project files in a folder saved locally but you haven’t initialised git. Here are the steps to take:
Open a new terminal by typing ‘terminal’ into spotlight on mac (CMD + Space) or Windows key + R on windows
Navigate into the root of your project directory with
cd directory-nameAdd a git ignore file with the command
touch .gitignoreAdd lines to filter out things Github doesn’t need to see in your .gitignore file e.g
.DS_store&node_modulesInitialise your local repo with
git initStage your local repo with
git add .to add all your filesCommit your files with
git commit -m “initial commit, I did a thing..."
Create remote repo & push with one command
Ok, you’re ready to push your committed work into Github. At this stage you could load up a browser, navigate to Github, click around lots to create a repo, copy the command to push remotely and paste it in your command line 😅
OR you could use a one liner right from your terminal to do all that for you in one go with the Github CLI:
gh repo create my-newrepo --public --source=. --remote=upstream --pushBefore you can use this one liner, you may be asked to authenticate your Github credentials which you do quickly with the command:gh auth login. You’ll only have to do this once and the authentication process is interactive right from the command line.
Breaking down the command
The first part of the one liner:
gh repo create my-newrepocreates and names a repo in your account (note: ‘my-newrepo’ should be replaced by the repo name of your choice)The
-publicflag makes sure the repo is public (swap this for-privateif necessary)The
-source=.flag specifies the source directory to be pushedFinally, the
-remote=upstreamflag specifies the remote repository to which the local repository is going to be compared with when pushing i.e the ‘upstream’ default.
Each step in full
For the commands inputted, follow the lines that start with a tilde ‘~’. Every other line is generated by the Git or Github CLI:
~ % cd project-directory
~ project-directory % touch .gitignore
~ project-directory % git init
Initialized empty Git repository in /Users/adam/Desktop/project-directory/.git/
~ project-directory % git add .
~ project-directory % git commit -m "Initial commit"
[main (root-commit) 2428d22] Initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 .gitignore
~ project-directory % gh repo create my-newrepo --public --source=. --remote=upstream --push
✓ Created repository adamddurrant/my-newrepo on GitHub
✓ Added remote <https://github.com/adamddurrant/my-newrepo.git>
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 215 bytes | 107.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To <https://github.com/adamddurrant/my-newrepo.git>
* [new branch] HEAD -> main
branch 'main' set up to track 'upstream/main'.
✓ Pushed commits to <https://github.com/adamddurrant/my-newrepo.git>Final thoughts
Another way to avoid forgetful ways of working is not to begin work until change tracking has been initialised. Meaning you could git init right out the gate.
To do that with the CLI, you can use the following command to create, name and clone an empty repo to your local machine ready to start work:
gh repo create new-repo --public --cloneIn summary, Github CLI commands are worth remembering. They’ll make you so much more efficient and your future self will thank you.