-
Windows: Download and run the installer. Click “Next” to accept the recommended settings. This will install Git and Git Bash.
-
macOS (choose one method):
- Homebrew (Recommended): Download Homebrew from brew.sh and follow the installation instructions. Then, open Terminal and run
brew install git
. - Xcode Command Line Tools: Your Mac may have Git pre-installed. Open Terminal and run
git --version
to check. If not installed, you will be prompted to install the Xcode Command Line Tools when you run a Git command for the first time.
- Homebrew (Recommended): Download Homebrew from brew.sh and follow the installation instructions. Then, open Terminal and run
-
Linux: Open your terminal and use your package manager. For example, on Ubuntu:
sudo apt-get install git
Ensure Git is installed by running the following command in your terminal or Git Bash:
git --version
This should display the installed Git version, something like git version 2.43.1
or similar.
Git is a tool that helps developers track changes in their code and manage different versions of a project. Every developer has a full copy of the project on their own computer, so they can work independently. Git allows changes to be merged later, but on its own, it does not provide a central place for everyone to share their work.
GitHub is a web-based platform that uses Git for version control. It acts as a central place where developers can share their projects, collaborate, and review each other's code. While Git manages the versions and history of your project locally, GitHub makes it easy for teams to work together and keep everyone’s changes in sync. It is the most widely used service for hosting Git repositories.
After installing Git, you need to set up your identity so that your commits are properly attributed to you. Open your terminal or Git Bash and run the following commands, replacing the placeholders with your actual name and email address:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
This means that changes you make to repositories will be associated with this name and email. More info can be found at Github Documentation Username & Email.
- Go to GitHub and click on "Sign up" in the upper right corner.
- Follow the prompts to create a new account. Make sure to use your University Email, as this allows you to access student benefits via Github Education.
For Windows beginners, using a Personal Access Token (PAT) is simpler and avoids configuring the SSH agent. Go to GitHub Personal Access Tokens and click on "Generate new token". Select the scopes you need (for basic usage, repo
is usually sufficient). Generate the token and copy it. You will use this token as your password when pushing to GitHub from the command line. Make sure to store it securely, as you won't be able to see it again. More info can be found at Creating a personal access token.
Another way to authenticate is by using SSH keys. Go to GitHub SSH Keys and click on "New SSH key". Instructions on how to generate an SSH key can be found here. Mainly focus on "Generating a new SSH key and adding it to the ssh-agent".
-
Open your terminal or Git Bash.
-
Navigate to the directory where you want to create your project using the
cd
command -
Run the following command to initialize a new Git repository:
git init
-
This will create a hidden
.git
folder in your project directory, which Git uses to track changes. This folder contains all the necessary metadata for the repository and can be viewed by runningls -a
in your terminal, which shows dotfiles/hidden files. -
Add a new file to your project, like a txt or markdown file, write something in it, and save it.
-
To check the status of your repository and see the new file, run:
git status
Which will always show you which files are staged (will be committed), unstaged (changed but will not be committed), and untracked (new files that Git isn't tracking yet).
-
To stage the new file for commit, run:
git add filename.txt
Replace
filename.txt
with the actual name of your file. You can also stage all new files at once by runninggit add -A
. -
To commit the staged changes, run:
git commit -m "Initial commit"
Making a commit is like taking a snapshot of all the changes you have staged. The
-m
flag allows you to associate a message with the commit. Replace"Initial commit"
with a message that describes your changes. -
The next step is to push the changes to a remote repository on GitHub. We will cover that in the next section.
- Log in to your GitHub account.
- Click the "New" icon or go to in the upper right corner and select "New repository".
- Fill in the repository name, description (optional), and choose between public visibility. Do not initialize the repository with a README file, .gitignore, or license for this example.
- Click "Create repository".
- After creating the repository, you will see instructions to connect your local repository to GitHub. For this project i got:
For me i would connect via (Note that this is for my specific repository, your URL will be different):
git remote add origin https://github.com/MertzAndreas/git_intro.git
git branch -M main
git push -u origin main
The first time you clone a repository, you need to use the git clone
command. This creates a local copy of the repository on your computer. You only need to do this once per repository.
# Download repository (Note: HTTPs and SSH versions differ)
git clone <repo_url>
git status # See changes
git add <file> # Stage a file
git commit -m "msg" # Commit staged changes
git push # Send to remote
git pull # Get latest changes from remote (Note that this also does a merge if there are changes)
git config --global init.defaultBranch main
- GitHub Desktop - A user-friendly GUI for managing Git repositories. (I don't use nor understand it, but some people like it)
- W3Schools Git Tutorial - Comprehensive down to earth tutorial on Git and GitHub basics.
- GitHub Documentation - Official GitHub documentation for more complex changes.
- Git Branching - Learn git visually and interactively, great for understanding branching and merging. Might be a bit advanced for beginners, but very useful.