Getting Started With Git and GitHub
At some point early in your tech journey, you’re likely to hear about Git or GitHub, whether from watching tutorials or in conversations with fellow tech enthusiasts. While these tools are essential in the software development process, they can initially seem daunting, especially since many beginners often confuse Git and GitHub as being the same thing — I know I used to! 😅 This article aims to demystify Git and GitHub, helping beginners understand and use them effectively for their needs, improving their development workflow and collaboration with other developers.
I’m sure by now you’ve inferred that Git and GitHub are not the same thing, so let’s start by explaining what each tool is and what it does. Git is a version control system. In simple terms, Git is a tool used to track changes to files and directories over time. Any changes you make to a file in your project can be tracked by Git, giving you complete control over which changes you want Git to track. On the other hand, GitHub is a platform that provides hosting for Git repositories (we’ll get to what these are later). While GitHub is widely used, other alternatives, like GitLab and Bitbucket, serve the same purpose. To summarize, Git is the tool for version control, while GitHub is a website for hosting Git repositories.
Now, let’s talk about repositories (or “repos” for short), as they play a crucial role in managing your project files. A repository is essentially a container that stores your project’s files, directories, and the changes made over time. You can choose which changes to track and which files or directories to store in the repository. When you ask Git to track a directory on your local machine, you’re essentially creating a local repository. You can later choose to host this repository on GitHub to enable easy collaboration and back up your project in case of device damage.
Get sandoh’s stories in your inbox
Join Medium for free to get updates from this writer.
In Git, there are four main locations where your code can be found at any given time: the local working directory, the staging area, the local repository, and the remote repository. Understanding how code moves between these locations is fundamental to working with Git effectively.
- The local working directory is the folder on your computer where you store and actively edit all your code files. For instance, the directory where you’ve stored your Python or JavaScript files for a tutorial would be your local working directory.
- The staging area is an intermediate space on your local machine where you gather the changes you want to commit (i.e., the changes you want Git to record). It’s useful because it allows you to control which changes Git will track. For example, if you modified both an
index.htmlfile and astyle.cssfile, but only want Git to track the changes instyle.css, the staging area enables you to do that. - When you commit your code (i.e., tell Git to record the changes you’ve staged), Git only records these changes locally in the local repository.
- Lastly, the remote repository is a storage location (like GitHub) where you can host your code and changes, providing collaboration tools and backup in case your device gets damaged.
Now that we’ve covered the concept of repositories and how code flows through these stages, let’s dive into how we can actually work with them. We’ll first walk through the essential commands used when creating a repository from scratch, followed by an overview of basic operations such as staging, committing, and pushing changes.
Working with git repositories
Typically, when starting a project, the first step is creating a remote repository on GitHub and then connecting it to a local repository on your machine. (You can also create the local repository first and then link it to GitHub). Let’s walk through this process:
- Log in to your GitHub account.
- Click the + icon at the top of the page and select New repository.
- Enter a name for your repo (ideally, the same name as your working directory) and click Create repository.
- A new page will appear, giving you instructions on how to connect your newly created GitHub repo to your local repository. Copy the set of commands under the section titled …or create a new repository on the command line.
Now, let’s break down these commands step-by-step so you fully understand what each one does:
echo "# trial" >> README.md
This command stores the string# trialin theREADME.mdfile. If the file doesn’t already exist, Git will create it.git init
This command initializes a new local repository in your current working directory. After running this command, you can verify the repository creation by runningls -ain your terminal and looking for the.gitfolder.git add README.md
Thegit addcommand adds theREADME.mdfile to the staging area, preparing it for the next commit. If you want to add all files in your working directory, you can usegit add ..git commit -m "first commit"
Thegit commitcommand saves your staged changes to your local repository. The-mflag allows you to specify a commit message.git branch -M main
This command renames the current branch tomain. The-Mflag forces Git to rename the branch, even if a branch namedmainalready exists.git remote add origin https://github.com/YourUsername/YourRepository.git
This command connects your local repository to the remote repository on GitHub.git push -u origin main
Finally, this command pushes the changes in your local repository to the remote repository on GitHub. The-uflag sets themainbranch on the remote repository as the upstream for future changes. After setting up the upstream on the initial push, you only need to rungit pushto push your changes to the same branch (Changing branches means you have to set a new upstream).
By following these steps, you’ve successfully created a repository from scratch and pushed your code to GitHub. Now you’re ready to start using Git and GitHub to manage your projects and collaborate with others.







