How to create a new Github repository from command line itself and push your first commit to the remote repo?
Before starting this article, I want to emphasize that “Git” and “Github” both are 2 different things.
Git is a version control system. It is software for tracking changes in any set of files, usually used while collaborating with team on any project. Git’s building block is called “repositories”.
Github provides a hosting service to store Git repositories and much more.
I assume that everyone has a github account and “git” installed in your system. Otherwise:
To create a new repository from our system command line, we need to do:
- Install gh (github command line tool) in our system.
- Log into our github account.
- Then, Initialize a new local repository using git.
- Create a new remote repository in github using gh.
- Push our first commit.
1. Install gh:
Like “Git” has a command line tool, “Github” also has a command line tool called “gh” .
To install “gh” in Windows and Mac OS, we need to type the commands shown under the OS specific section here.
To install “gh” in Linux distributions, we need to type the commands shown under the distribution specific section here.
2. Log into our github account:
After installing gh in our system. To log into our github account, we need to type the below command
gh auth login
Then select Github.com >>> Select HTTPS >>> Select “Login with a web browser” >>> Copy the one-time code and press enter >>> A web page will open asking the one-time code, paste it and click submit >>> Switch to your terminal and press enter.
Output:
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations? HTTPS
? How would you like to authenticate GitHub CLI? Login with a web browser! First copy your one-time code: XXXX-XXXX
- Press Enter to open github.com in your browser...
✓ Authentication complete. Press Enter to continue...- gh config set -h github.com git_protocol ssh
✓ Configured git protocol
✓ Logged in as <<GITHUB_USERNAME>>
Now you are logged into your github account. We can verify by typing the command:
Command:
gh auth statusOutput:
✓ Logged in to github.com as <<GITHUB_USERNAME>> (<<FILE_PATH_FOR_USER_DETAILS>>)
✓ Git operations for github.com configured to use https protocol.
✓ Token: *******************
3. Initialize a new local repository using git:
After logging into our github account, we need to create a directory (folder) where we can store our repository. So, for creating a new folder:
# use 'mkdir' cmd to create a directory called 'our_repo_dir'
mkdir our_repo_dir#navigate into 'our_repo_dir' directory
cd our_repo_dir
Now, we need to initialize our local repository here using ‘git’:
Command:
git initOutput:
Initialized empty Git repository in /<<PATH>>/.git/
The repository we created now is a local repository (beacause it stores in our local file system). The repository we will create in github is a remote repository.
4. Create a new remote repository in github:
We need a new name to the repository to avoid any name conflicts. So we will check the list of repositories in our github account:
Command:
gh repo list
Pick a new name that haven’t been given in the list.
Now enter the below command to create a new remote repository:
Command:
gh repo create
Give a meaningful repository name (let’s say ‘test_repo’ ) and press enter >>> Give a meaningful description to repository (or leave it blank) and press enter >>> Select visibility and press enter >>> prompt says that the location to the new remote repository will be labelled as “origin”, press ‘Y’ and enter >>> Your new repository named on github is created >>> prompt asks whether do you want to connect to the repository or not, you can choose ‘no’ >>> It will end in exit status 1 (No worries, beacause we didn’t connect to repository).
Output:
? Repository name test_repo
? Repository description
? Visibility Private
? This will add an "origin" git remote to your local repository. Continue? Yes
✓ Created repository <<GITHUB_USERNAME>>/test_repo on GitHubThe authenticity of host 'github.com (<<IP ADDRESS>>)' can't be established.
RSA key fingerprint is SHA256:VThbg6kXUpDWGl7E1IGOCspPomTxdCARLviKw6Q5SY8.
Are you sure you want to continue connecting (yes/no)? no
Host key verification failed.
Now we can check our newly created repository on github by opening our github home page.
5.Push our first commit:
Now that we have created our remote repository under the label ‘origin’. We will push our first commit to the remote repository. To push any commit, first we will add any changes to our local repository:
# we will add a README.txt file with some information in it.
echo "README file for TEST repo" >> README.md#We will add README.txt file to staging area of local repository (i.e., saying that we ready to commit these changes)
git add README.txt#Now, we will commit our changes with message "first commit".
git commit -m "first commit"
This commit is stored in local repository. Now, we will push this commit to remote repository. First, we will check whether our remote repository is stored under the label ‘origin’ or not:
git remote -vOutput:
origin https://github.com/<<GITHUB_USERNAME>>/test_repo.git (fetch)
origin https://github.com/<<GITHUB_USERNAME>>/test_repo.git (push)
If we get output like above for label ‘origin’. Then we can push our commits to remote repository. Otherwise, we have to add the remote repository link to the label ‘origin’ (‘origin’ is just a standard label, we can name it anything meaningful).
git remote add origin https://github.com/username/repo_name.git
Now, we can push our commit to the master branch of remote repository which is labelled under ‘origin’:
git push -u origin master
You can enter your github username and password (Or you can login through SSH token first, See here).
Now, your changes are pushed to the remote repository. You can check it on your github home page.