Skip to content

Using the GitHub CLI

xgreyskiesx edited this page Feb 2, 2019 · 10 revisions

Getting Started

The GitHub CLI is a command line interface which interacts with GitHub's own API, and is an essential part of version management. Installation is fast and easy.

First things first, download the executable from the official git site. Click the link for your operating system, and the file should automatically download. Open it when it's done, and start the installation process.

When you reach the window with different checkboxes for components, make sure it looks like this:

Check the "additional icons," "explorer integration," and "Git LFS" boxes

After this window, the installer should ask about your default Git editor. Ignore that and just hit next. The next page will be titled "Adjusting your PATH environment." Make sure it's set to the middle option, and hit next again.

It should say "Git from the comamand line and also from 3rd-party software"

The rest of the installation process can be clicked through with the next button again, until the button changes to say "install." Hit that, and when it finishes, you're all done! Uncheck the box saying "View Release Notes," and tap "Finish."


Creating a Remote Repository

A repository is where project files are stored. They come with many useful features, including specified releases, wikis like this one, and version control. Each repository tracks any and all changes through commits, just in case you need to backtrack to a version before an issue cropped up. "Remote" means that the repository doesn't exist on your machine, and instead is reached through server calls.

The easiest way to create a remote repository, or repo, is through the GitHub website itself. First, make sure you're logged into your account.* If you don't have one, now's the time to sign up. The options to do so are at the very top of the page and to the right.

When you're done logging into your account, you'll be met with a page that looks something like this:

Repositories are to the left

Click the shiny green "New" button to create a new repository. Give it a descriptive name (maybe "My First Repository" for now; you can always change it later) and a description (if you're feeling spicy), and make sure to check the box saying "Initialize this repository with a README." What you'll have should look something like this:

Finish the repo and you'll be met with a screen like this:

  1. Your username (or, if the owner was changed, that username).
  2. The repository's name.
  3. The repo's "toolbar." The most important buttons here are code, projects, wiki, and settings.
  4. The branch chooser. We'll talk more about that later.
  5. This is where you can interact with the repository without the CLI, and where you'll get your remote token. More on that later, too.
  6. The current file's edit button. Use this to edit whatever file you've got open at the moment.

* Best Practices: Unless you're doing this at home, do this from your browser's (preferably, Google Chrome) incognito/private browsing mode. This ensures privacy and prevents others from tampering with your account.


Initializing Local Repos

Local repositories are repos that are on your machine. If you want version control without a handy website to use, and only plan to keep your code on your computer, you'd initialize a local repo and commit only to that. You also need a local repo if you plan to push code into a remote repo, as it handles staging changes and commit messages, which is why initializing locally is required.

To get started, open the file explorer and navigate to the folder you want to initialize a repository in. For this example, I'll be doing it in Documents/projects/example-repo.

Inside the folder, right click and choose "Git bash here."

Should be under the git GUI option

That'll open a shiny cool window that looks like this:

Ooh, pretty!

In this terminal, type git init and hit enter. That'll initialize a local repo in that folder, and you should see a message stating so. If you've turned on the option to show hidden files in Windows Explorer, you'll see a brand new .git folder there. That's where Git does all the behind the scenes work.


Logging In

Before making any commits, the CLI will ask you to tell it who you are. In order to do this, we'll use two git config calls.

  1. git config user.name "myUserName" - Replace "myUserName" with your GitHub username (keep the quotes!)
  2. git config user.email "[email protected]" - Do the same here, except with your email.

Once that's done, you'll be able to start making commits!


Adding Remotes

Remotes are URLs that point to remote repositories. On GitHub, all HTTPS remotes to repos are public, because code on GitHub is open source and can be contributed to by anyone.

To find a remote URL, go to the "Clone or Download" button in a repository.

Can't miss that green button!

Clicking the little clipboard icon next to the url will copy it automatically. From there, you can add the remote to your local repository through the CLI using git remote.

git remote add [name] [URL] is the easiest way to add the remote. Usually, the name for the default remote in a local repo is origin, but any name will work.

With my example-repo, adding a remote will look like this:

Use "git remote" after to make sure it registered


Creating New Branches

Branches are another feature of version control. They're used to separate code in the same repository into different versions, or branches, of the original code. For example, the master branch might be for code that officially works, while the dev branch may be for code in development, and the testing branch might be for adding experimental features.

To create a new branch, go back to the console and type git checkout -b [branchName], like so:

Now, the new branch should show up in place of where master was, showing that it's been changed.


Making Commits

A commit is like saving all your code into a container you can open up later, even after you've made other commits. This is the main part of version control. The commands for this are git add and git commit.

If you've been following along so far, what you'll want to do now is create a new file in the directory you initialized the git repo in. You can do this by right clicking in the folder and having Windows create a new text file:

Call it README.md (with the first part in all caps; and make sure you delete the original .txt ending!). Open it up in your favorite text editor (Notepad will even do the trick) and type away, then save it.

Going back to the command line, type git add README.md. This will add the README file the commit. You can also do git add ., which adds all files in the directory (except anything mentioned in a .gitignore file; more on that later).

After that, type git commit. If you want to add a message, like "initial commit," you can type git commit -m "Message Here" instead. Make sure to wrap the message in the quotation marks! Now you're done with committing, and it's time to push changes.

The command process should look something like this:


Pushing

Pushing code refers to overwriting files in the remote repository with new or updated files from the local repository. The command for this is git push.

Normally, the protocol for pushing to repos will be git push [remote name] [branch name], but in this case, since a new branch was made, it's a little different.

Type git push -u [remote] [branch], where remote is what you made the original remote (like "origin"), and branch is the branch name you made earlier (like "example-branch"). The little -u means "upstream," and is how you create a branch in the remote repository that's linked to an existing local repo branch.

If this doesn't work, you may have to re-add the remote that was made earlier by grabbing the URL again and doing git remote set-url [remote] [URL]. If it did work, then congrats! Go to your repository on the GitHub website, navigate to the branch you made, and voila.


Pulling

Pulling code refers to overwriting files in the local repository with new or updated files from the remote repository. The command for this is git pull.

While you're still on the branch, click the README's little edit icon. Type something new, maybe add a commit message, and then save the changes on the site.

In the CLI, type git pull [remote] [branch]. This will grab your repository's README file and make it available locally with the changes. After doing this, if you open up the new file, you should see the changes you made in the web client.


That's It!

You're all done! Now, you should be able to create remote repos, initialize local ones, interact with remotes, commit changes, and push/pull to/from remote repos.

If you created a repository you don't plan to keep, you can go into the repo's settings and delete it now, unless you're keeping it for the next section of the tutorial.

Reference

git init - Initializes a local repository.

git config - Used for logging into the CLI.

git remote - Used for linking local repos to remote ones.

git checkout - Used to switch branches, or create new ones with the -b flag.

git add - Add files to commit.

git commit - Commit changes.

git push - Push changes to a remote repository.

git pull - Pull changes from a remote repository.

More info can be found on the Resources page