Git + GitHub Tutorial: Basics of Working with GitHub on a Mac đ»
This article provides a step-by-step guide of how to create a remote repository on GitHub and how to work with GitHub locally, keeping everything in sync.
Ever felt overwhelmed by the thought of using Git and GitHub? Youâre not alone. It really does look confusing and cumbersome. But Iâm here to help. Because Iâve been there myself. So letâs not drag this out and get it over with once and for all.
In just 15â20 minutes (reading time and following the steps), you will learn how to:
- â use Git in the macOS Terminal
- â set up remote repositories on GitHub
- â and most importantly â connect your local and remote repositories
âđ» This guide is for anyone who knows the purpose of Git and GitHub but havenât tried them yet.
I put together this guide from various courses and guides on Git services. I selected the best parts, rtrimmed the fat to tailor it for GitHub and Mac users. So, you learn a lot quickly.
As you read, itâs best to follow along as you read. This way, youâll understand the material better, set up accounts in the necessary systems, and get hands-on experience with Terminal and GitHub.
Getting Ready to Dive In đ€
Youâll need:
âđ» Many graphical interfaces exist for Git, including GitHubâs own. You might explore these options later, but weâll stick with Terminal here for a direct learning experience.
Setting Up Git
If youâre new to Git, start by setting up your user profile by adding your username and email. Open Terminal and run these two commands sequentially (enter your name and email without brackets):
git config --global user.name "Your Name"
git config --global user.email "your_email@whatever.com"
To verify, run these commands to see your entered details:
git config user.name
git config user.email
Keep Terminal open; youâll need it for the next steps.
Creating a Remote Repository
A remote repository on GitHub is a cloud storage where your project files and their change history are kept. Locally means itâs just a folder on your computer.
People use GitHub repositories for many things:
- Sharing code and other information (for example, a list of prompts for ChatGPT)
- Working together, discussing, and managing projects (not only related to code, for example, a skill map and development plan for team leads)
- Even organizing tasks (using the built-in task tracker)
While the actual work with code and files is done locally â in the project folders on your computer â these folders connect to your cloud repositories on GitHub.
Hereâs what you need to do:
- Create a project on github.com
- Clone it to your computer
âđ» You could start differently, but this way is easier for beginners.
Itâs smart to make a new repository on GitHub for every project. Next, Iâll explain how to set up a remote repository and a local folder for a test project named âhello-worldâ and sync them.
Creating and Setting Up
To start a new GitHub repository, click the + sign at the top right corner and choose New repository from the menu:
Youâll then see a page to set up your new repository. Hereâs what to do:
- Name your repository
- Optionally, give your project a short description
- Decide if your repository will be Public (anyone can see and contribute) or Private (only people you choose can see it)
âđ» GitHub offers Public and Private repositories. Public ones are open for everyone to see and contribute to, while Private ones are just for you and anyone you invite.
- Tick the box to Initialize this repository with a README for an instant overview of your project
âđ» Itâs a good practice to include a README in Public repositories for a quick project summary. The details you enter in the Description field get automatically added to this file, making it visible on your projectâs main page.
- Add a .gitignore file if your project has specific files you want to exclude from the repository (useful for larger projects, but we wonât dive into that here)
- Include licenses if your project uses licensed materials, like certain fonts
With these steps complete, click the green Create repository button to finish.
đ
Repository Page
On your new repositoryâs page, find everything you need under the Code tab. This includes your README file, prominently displayed.
Initially, your project includes a README.md file, a single commit marking the creation, and one branch â the primary main branch.
âđ» For product managers or analysts, the main branch usually suffices. Complex projects or team collaborations might require additional branches, a topic weâll explore later. Remember to hit Follow for updates.
Project Working Folder
Chances are, youâve got a projects folder already. If not, itâs time to make one. Next, youâll use Terminal to access this folder.
âđ» Thereâs no need to create the project folder itself; it will be cloned from GitHub.
Early on, navigating files and folders through Finder is intuitive. To quickly access your projects folder in Terminal from Finder, simply right-click on the folder and select New Terminal Tab at Folder.
This action opens a Terminal tab right in your projectâs folder.
Cloning a Remote Repository
To manage a project on your local machine and keep it in sync with the remote repository on GitHub, youâll need to clone it. Hereâs how.
- Navigate to your projectâs page on GitHub and click the Code button to copy the repositoryâs URL
- Open Terminal (the tab youâve opened via Finder) and execute the following command, replacing âURLâ with the actual URL (without brackets) you just copied:
git clone "URL"
Once the command finishes, Terminal confirms with a âdoneâ message, and youâll see the projectâs folder, now cloned, in your local projects directory. Youâre all set to modify files, add new content, and ensure everything stays updated with the remote repository.
đ TLDR
To connect a new GitHub project with a folder on your computer, follow these steps:
- Create a new project (repository) on GitHub and grab its URL
- Open Terminal and head to your general projects folder (no need to make a specific folder for this project)
- Clone the GitHub project locally with
git clone âURLâ
(replace âURLâ with the actual URL you copied)
âđ» This approach lets you bring any project from GitHub right onto your local machine.
Synchronizing Local Changes with a Remote Repository on GitHub
Changes to the Project
For a hands-on demonstration of synchronization, letâs make two updates:
- Modify the README file
- Insert an image into the project folder
Follow along with these steps to get comfortable with Git.
In the README.md, Iâve inserted âMy furst commitâ â notice the typo, which weâll fix later as we explore how to edit files directly on GitHub.
Also, Iâve added an image named project-page.png, a screenshot from this guide, to the project folder.
Git Flow
To keep your local and remote repositories in sync (updating a file and adding an image in this case), you need to add, commit, and finally push these changes.
The key steps are git commit
and git push
, with git add
serving to stage changes.
âđ» A commit acts like a save point in your project, allowing you to revert back if needed, akin to game saves. These checkpoints are synchronized between your local and remote repositories.
Push applies your committed changes to the remote project.
Now, letâs dive into each step.
Checking Git Status
Work on your project takes place within its local directory. Open this directory in Terminal for direct manipulation. If youâve navigated away, return to your projectâs folder with the command below, replacing âproject folder nameâ with your actual folder name (without brackets):
cd "project folder name"
Remember, using Finderâs right-click menu to open Terminal at your projectâs folder streamlines this process.
Git Add and Git Commit Commands
First, ensure youâre in your projectâs directory in Terminal, as all modifications occur within this folder. To see whatâs changed, run:
git status
đ A detailed list of Git and GitHub commands will be shared later in this guide.
âđ» While not essential,
git status
is a helpful command to familiarize yourself with. It lets you peek at your projectâs current state anytime.
git status
commandThis command reveals the projectâs status. Changes not yet staged for commit are marked in red, giving you a clear view of whatâs been altered but not yet committed:
- On branch master (now it called main instead of master)â signals the branch youâre on
âđ» Handy for navigating multiple branches.
- Changes not staged for commit â indicates modifications not yet prepared for a commit (e.g., updated README.md)
- Untracked files â shows new files added to the folder but not tracked (like project-page.png)
- no changes added to commit â means thereâs nothing ready to be committed just yet
Git will suggest your next steps, hinting at using git add <file>âŠ
to start staging your changes.
git add (Staging Changes)
Use the git add
command to stage changes in your projectâs folder. For substantial projects, you might stage just one file at a time using its name and extension (without brackets):
git add "file_name.extension"
To stage all modifications together, which is ideal for smaller projects, use:
git add .
Running git status
afterward lets you verify that all changes are staged for commit (though this step is optional).
git commit (Creating a Commit)
To bundle your changes into a commit with a descriptive message, execute (here, write the comment as in the example â in quotes):
git commit -m "Change README, and add screenshot"
âđ» Commits should encapsulate a cohesive set of changes, like a newly added feature or an update to existing functionality. Describing these changes in your commit message is a best practice for clear project history.
After committing, git status
will confirm there are no pending changes, indicating your commit is ready but not yet pushed to the remote repository.
git push (Pushing Changes)
To upload your local changes to the remote repository, execute git push
. To specify the branch youâre pushing to, use:
git push origin main
âđ» On your first
git push
, Git will prompt for your GitHub credentials. As a password here, you should use a Personal access token, read here how to make one. Although entering your Personal access token wonât visually alter the screen (no moving cursor or characters), just paste it and press enter. A correct entry will successfully push your changes.
After pushing, git status
will confirm there are no outstanding changes â indicating everythingâs been committed.
Your GitHub project is now current, reflecting all local changes including any new commits, file additions, or README updates.
đ TLDR
To update the remote repository with your local project changes:
- Open Terminal and go to your project directory
- Stage your changes with
git add .
- Commit those changes using
git commit -m âYour message hereâ
- Finally, push your commit with
git push origin main
âđ» Use
git status
to keep tabs on your projectâs current state and what changes are staged or committed.
Updating the Local Repository After Changes to the Project on GitHub
Sometimes, files in your project might be updated directly on GitHub or by other team members. When this happens, you need to bring those changes into your local repository.
Editing Project Files on GitHub
To demonstrate syncing Iâve updated the README file on github.com.
âđ» Remember, every change made on GitHub counts as a commit. So, when you save your edits, a new commit is instantly created in your project.
Editing files on GitHub is straightforward thanks to its built-in editor:
- Open the file to edit and type your changes;
- Provide a commit message explaining them;
- Hit the save button to commit your changes.
Once saved, this commit is instantly active, incorporating your changes into the project and your description to the commit.
GitHub keeps a log of all commits, complete with details. Viewing any commit lets you see the changes made, with deletions in red and additions in green.
Pulling Changes to Your Local Repository
To sync your local repository with the latest changes on GitHub, navigate to your projectâs folder in Terminal and run:
git pull
Executing this command pulls the latest updates from GitHub, displaying whatâs being updated in Terminal. Once git pull
completes, your local project files reflect the most current version on GitHub. Check the README in your local folder to confirm these updates.
đ TLDR
To keep your local project up-to-date after changes on GitHub:
- Open Terminal, navigate to your projectâs folder, and execute
git pull
đ List of Commands for Working in Terminal with Git and GitHub
Setting Up Git
git config â global user.name âYour Nameâ
â Set your username (without brackets)git config â global user.email âyour_email@whatever.comâ
â Set your email (without brackets)git config user.name
â Check your currently configured usernamegit config user.email
â Check your currently configured email
Managing Repositories
git clone âURLâ
â Clone a repository from GitHub to your local machine (URL without brackets)git status
â View the status of your local working directory (see whatâs changed, added, or ready to commit)git add âfile_name.extensionâ
â Stage a specific file for commit (file name without brackets)git add .
â Stage all changed files in the directory for commitgit commit -m âcommit descriptionâ
â Commit your staged changes with a descriptive messagegit push origin main
â Push your committed changes to the main branch on GitHubgit pull
â Pull the latest changes from the remote repository to your local directory
And there you have it â a comprehensive guide to getting started with Git and GitHub on your Mac. Donât hesitate to experiment with new commands, explore additional features on GitHub, and collaborate on projects to enhance your skills. Happy coding!
đđ» Follow me on X (twitter)
For more insights and tips on navigating technical topics in simple language, make sure to follow my X (twitter) or LinkedIn. I regularly share knowledge aimed at demystifying tech for non-technical specialists. See you there!
đœ