GitHub essentials · Lesson 2 of 4
Module 3 · The command line · GitHub essentials
Install git, sign in to GitHub from your computer, clone a repository and learn the everyday loop of pull, edit, add, commit and push, with fixes for the errors you are most likely to meet.
The website is fine for small edits. Real work happens on your own computer, in the terminal you set up in The terminal and where to run it, and git keeps the two copies in step. This lesson connects your computer to GitHub, then walks through the loop you will use every day: change, commit, push, pull.
You already use the terminal in this module, so the terminal route will feel familiar. GitHub Desktop is there if you prefer buttons.
Two ways to work
| GitHub Desktop | The command line | |
|---|---|---|
| What it is | A free app from GitHub for Windows and macOS | Typing git commands in a terminal |
| Good for | Getting started, seeing changes visually | Servers and clusters, scripts, every operating system |
| Sign-in | Through the browser, once | With the GitHub CLI (gh), a token or an SSH key |
| Same result? | Yes: both make the same commits | Yes |
Install git and tell it who you are
- Windows: install Git for Windows from git-scm.com. It includes Git Bash, a terminal where the commands in this lesson work as shown.
- macOS: open Terminal and type git --version. If git is missing, macOS offers to install the command line tools.
- Linux: install it with your package manager, for example sudo apt install git.
Then, once per computer, tell git the name and email address to put on your commits. Use the same email as your GitHub account so GitHub links the commits to you.
git --version git config --global user.name "Alex Smith" git config --global user.email "alex.smith@example.org" git config --global init.defaultBranch main git config --list --global
The version number on your computer will differ; any recent version is fine. --global means the setting applies to every repository on this computer. init.defaultBranch main makes new repositories start with a branch called main, the same name GitHub uses.
Sign in to GitHub from your computer
GitHub no longer accepts your account password for git commands. The easiest way to sign in is the GitHub CLI, a small official tool called gh (install it from cli.github.com). It signs you in through your browser and sets git up to use that sign-in.
gh auth login
- Answer as shown: GitHub.com, HTTPS, yes, then sign in with the browser. Your one-time code will be different.
- You only do this once per computer. From then on, git push and git pull work without asking for a password.
Other ways to sign in
- Personal access token: a long password-like string you create under Settings › Developer settings › Personal access tokens. GitHub recommends fine-grained tokens, limited to the repositories and permissions you choose. When git asks for a password, paste the token. Treat it exactly like a password and give it an expiry date.
- SSH key: a pair of files on your computer that prove who you are, common on servers and clusters. Create one with ssh-keygen -t ed25519 -C "your email", then add the public key (the file ending in .pub) under Settings › SSH and GPG keys. You then use the SSH address of a repository, which starts with git@github.com:.
- GitHub Desktop: choose Sign in to GitHub.com when the app first opens and finish in the browser.
Get your repository onto your computer: clone
Cloning makes a full copy of a repository, with all its history, in a new folder on your computer, and remembers where it came from. On the repository page, choose the green Code button and copy the HTTPS address.
cd ~/projects # the folder where you keep your work git clone https://github.com/your-username/lsma-practice.git cd lsma-practice git status
- git clone creates the folder lsma-practice and downloads every commit.
- origin is the name git gives the GitHub copy you cloned from. origin/main is the main branch on GitHub.
- Working tree clean means nothing has changed since the last commit.
- In GitHub Desktop the same step is File › Clone repository, then choose the repository from the list.
The everyday loop
Every working session follows the same five steps: pull to get the latest version, edit your files, add the changes you want to save, commit them with a message, and push them to GitHub. Create a small file, then run the loop:
git pull # 1 · get anything new from GitHub first echo "First notes on the project" > notes.md # 2 · edit (here: make a file) git status # see what changed git add notes.md # 3 · choose what goes into the next commit git commit -m "Add project notes" # 4 · save the snapshot with a message git push # 5 · send it to GitHub
- Untracked means git can see the file but is not recording it yet. After git add it is staged: included in the next commit.
- git add . stages every change in the folder at once. It is quick, but check git status first so you do not include something by mistake.
- The short codes such as 7d2c1e4 identify each commit. Yours will differ.
- main -> main confirms your local main branch was copied to the main branch on GitHub. Refresh the repository page and notes.md is there.
- Other useful commands: git diff shows exactly what changed before you add it; git log --oneline lists the history; git restore file.py throws away uncommitted changes to one file.
Good commit habits
Commit small and often: one commit per meaningful change, such as Add quality-control plot or Fix sample names in metadata. A message should say what the commit does, in a few words, so that you can find it again months later.
Pull before you start and push when you stop. Most problems with git come from two copies drifting apart for days.
.gitignore: what git should never track
Some files should stay on your computer only: large data, caches, temporary files and anything secret. List them in a file called .gitignore in the top folder of the repository, one pattern per line:
# data stays out of git data/ *.fastq.gz *.bam # secrets .env # caches and temporary files __pycache__/ .ipynb_checkpoints/ .Rhistory .RData .DS_Store
A line can be a file name, a folder name ending in /, or a pattern with * meaning anything. Lines starting with # are comments. Note that .gitignore only affects files git is not tracking yet; if you already committed a file, remove it from tracking with git rm --cached file and commit.
When something goes wrong
| You see | What it means | What to do |
|---|---|---|
| Authentication failed or Support for password authentication was removed | git tried to use your account password | Sign in with gh auth login, or paste a personal access token when asked for a password |
| ! [rejected] main -> main (fetch first) | GitHub has commits you do not have yet, for example an edit you made on the website | Run git pull, then git push again |
| CONFLICT (content): Merge conflict in notes.md | The same lines were changed in both copies | Open the file, choose the right version between the markers, then add and commit (Lesson 3) |
| File ... is 120.00 MB; this exceeds GitHub's file size limit of 100.00 MB | A data file is too big for GitHub | Remove it from the commit, add it to .gitignore, and keep the data elsewhere (Lesson 4) |
| fatal: not a git repository | You are in the wrong folder | Use cd to move into the repository folder, then try again |
| You committed something by mistake and have not pushed | The commit exists only on your computer | git reset --soft HEAD~1 undoes the last commit and keeps your changes staged |
Connect and clone
This lesson
Install git, set your name and email, sign in with gh auth login (or GitHub Desktop), and clone lsma-practice onto your computer.
Check your answer
git status inside the folder says On branch main, Your branch is up to date with 'origin/main', and the folder contains the files you saw on GitHub.
Run the loop twice
This lesson
Add a .gitignore line for a data folder, create notes.md, then pull, add, commit and push. Edit the README on the GitHub website, then bring that change down to your computer.
Check your answer
After the website edit, git pull on your computer prints the README change, and git log --oneline lists the website commit above your own. If you edited the same lines on both sides, you met your first conflict: Lesson 3 shows how to resolve it.
Check your understanding
1. In the everyday loop, what does git add do?
2. git push is rejected with "fetch first". What is the usual fix?
3. Which file stops git from tracking data folders and secrets such as a .env file?
Up next
GitHub 3
GitHub: branches, pull requests and issues