Efficient GitHub Operations: Simplifying Repository Management using Github CLI
Tags: github, git, softwaredevelopment, versioncotrol
Published: 2024-05-12 08:12:24

Often, when we set up version control initial files for a project, we tend to log onto the [Github](https://github.com/) website to create our repositories, clone, and push our projects. While this method works, it can be argued that it is not as efficient. We can manage our GitHub repositories, from creating the repo to maintaining the different versions, right from our terminal. In this article, we will go through the process of achieving this.

I will be attempting to create a repository for the Nextjs practice project I am currently working on and then push the files from my local machine to the the repository, and I will be doing all of it from my terminal.

All you need to acheive this is a basic knowledge of the command line interface, and of course a github account.

**
### Step 1 - Install Github CLI
**

**For Mac**

`brew install gh`

**For Linux (Ubuntu)**

`sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key C99B11DEB97541F0
sudo apt-add-repository https://cli.github.com/packages
sudo apt update
sudo apt install gh`


**
### Step 2 - Authenticate with Github
**

Upon installation completion, you will need to authenticate with Github. Run the following command.

`gh auth login`

This will take you through series of authentication processes. You can choose to authenticate using a web browser or by pasting an authentication token. For any option you choose, just follow the prompts and you will be all set.
I will be using the web browser (for the last time).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zwi537yj2k1j0d9amd6u.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35n24hfk5ttxz1317xze.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kc2gk0c3hi2bzwgwp0r6.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/llffm7rnk6bmqd2s77zs.png)

To verify authentication status, run...

`gh auth status`


**
### Step 3 - Create Repo
**

It's time to create the repo for our project. Do this using the command

`gh repo create <repository-name> --public`

Be sure to replace *repository name* with your choice repository name.
The public flag is usually required. It helps github decide if to make your repo public or private. Use `--private` id you want otherwise. There are other flags that you can use. Go through the [documentaion](https://cli.github.com/manual/gh_repo_create) to learn more about your options.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1m0ce9pogdq8koy247w.png)

**
### Step 3 - Clone the repository and add files locally
**

At this point, your repository is up on your github account. It can be accessed via the github url from the command output. We will have to clone the repository, and add our project files from our local machine. To do this, change directory to where you want to have your cloned repo and run the command.

`gh repo clone <repository-url>`

As usual, replace the *repository-url* with your repo url.

After cloning, if you had already started working locally ads in my case, move your project files to the cloned repo, if not, you can start working from your cloned repo.


**
### Step 4 - Pushing files to github
**

Now we will have to push our initial project files to our remote github repository. But before then, let us quickly inspect our repo on github to be sure it was created and that it is empty. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48wlowsdao9hv6rx9onr.png)

Great!

Now run the follwoing commands to get your project files in.

`
git add .
git commit -m "<commit message>"
git push
`

If everything is okay and you have a successful push, your project files should be up on github and in the repository that you created earlier.

Access github via the browser to confirm you had a successful push.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jiuarutcx4b21252rl1s.png)


**
### Conclusion
**
At first, the process of setting up your working environment for seamless repository management using just the command line may seem intimidating. However, once you give it a try, you will realize that it is actually quite easy and straightforward. As you become more comfortable at using your terminal, you will begin to appreciate the efficiency of working mostly from the command line and the conveneince it gives your workflow.

There are many other powerful features and commands that the  Github CLI offers. These features help us manage or repositories more efficiently. You can check a few other features that may fit your repository management needs on the [Github CLI Documentation](https://cli.github.com/manual/).

Happy Commits!