![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
So you’ve finally added the git command line interface to your developer workflow but you’re not sure how to make it work with your team’s GitHub repository. By using git with GitHub, you can then collaborate on code with anyone else able to access the repository.
This is a crucial feature of GitHub (and why it exists in the first place). Collaboration is at the heart of such tools and getting the git CLI and GitHub to communicate with one another is essential, especially if you have any intention of working with a team.
But getting the CLI to communicate with your team’s GitHub repository isn’t exactly the most intuitive task you’ll undertake. In fact, it can be quite confusing, which can quickly become a very frustrating issue if you’re uncertain how to make it work.
That’s where I come in. I want to walk you through the process of connecting your local Git command line with a GitHub repository. Once you see how it works, you shouldn’t have any problems in the future.
Let’s make that happen.
The requirements for this are important, so pay close attention. Here’s what you’ll need to connect git with GitHub:
Without all of those things above, this will not work. Let me address each point before we continue.
First, you must have Git installed on your local machine. This can be done on Linux, macOS or Windows. Next, you must have a valid GitHub account, which can be free or paid. You’ll also need a valid personal access token because you can no longer work with standard username/password credentials.
To create a personal access token, do the following:
Once you’ve taken care of the above, make sure to copy the token and save it in a secure location.
Note: If your team’s GitHub page doesn’t allow you to create the new token, you’ll need to request that your admin take care of it.
Finally, you’ll need a valid GitHub repository to connect to. From that repository, you want to make sure you copy the URL for the repository by clicking the green Code drop-down and then clicking the Copy icon for the address (Figure 1).
Figure 1: Don’t bother using this repository as it’s only used for testing purposes.
Once you have the link, it’s time to make the git CLI work.
The first thing we need to do is set our global username and email address for Git. This is done with the following two commands:
git config --global user.name "NAME"
git config --global user.email "EMAIL"
Where NAME is your real name and EMAIL is the email address associated with your GitHub account.
Fun times.
Next, create a directory to be used on your local machine. We’ll call this repository new-project and create the directory with:
mkdir ~/new-project
Change into that directory with:
cd ~/new-project
Initialize the repository with:
git init
We can now connect the local repository with the GitHub repository with the command:
git remote add origin URL
Where URL is the link you copied from the GitHub repository.
Next, pull the content of the remote repository with:
git pull origin master
Note: You might have to change master to the branch name you need to work with.
You should now see all of the code from the project in your local directory.
Let’s say the first thing you need to do is edit (or create) the README file. Open the file with your favorite editor or integrated development environment and make some changes to the file. Once you’ve done that, add the file to the staging area with:
git add README
Next, make your first commit with:
git commit -m "Added new information to README file"
Issue a push to send the changes to the remote repository with:
git push
You’ll be prompted for your GitHub username and password. The password is actually the personal access token you created earlier.
Upon successful authentication, you’ll see the changes were pushed to the remote repository. You can always go back to GitHub and view the file to verify the changes were made.
You can add files, edit code, and do whatever it is you need for the project, as long as you make sure to follow the usual Git workflow, which you can read about in my post, “Need To Know Git? Start Here.”
When next we visit Git, we’ll create a local repository that can be accessed by other developers on your LAN.