Home FAQ New Contact About
Developer topics

Git and GitHub basics for beginners

Learn Git from install to pull requests in 8 short guides: setup, commits, branches, pushing to GitHub, teamwork, undoing mistakes, a command reference and learning tools. Each guide has its own colour.

Install and set up Git

7 steps

Git is the tool on your computer that records the history of your files. GitHub is a website that stores Git projects online. You set up Git first.

Git

Official site for this guide. Start here, then follow the cards below.

Free: Git is free and open source.

Visit Git

1. Install Git

Windows: download the installer from git-scm.com and keep the default options. macOS: run git --version in Terminal and accept the prompt to install the developer tools, or use brew install git. Linux: for example sudo apt install git.

Download Git

2. Check it works

Open a terminal (Git Bash on Windows) and run git --version. You should see a version number.

3. Tell Git who you are

Run git config --global user.name "Your Name" and git config --global user.email "you@example.com". Use the email on your GitHub account. These details are stamped on every commit.

4. Use main as the default branch

Run git config --global init.defaultBranch main so new repositories start on a branch called main, the same name GitHub uses.

5. Check your settings

Run git config --list to see what is set. Run git config user.name to check a single value.

6. Decide how you will sign in to GitHub

GitHub no longer accepts your account password for Git over HTTPS. Use one of: a personal access token (with Git Credential Manager, installed with Git for Windows), the GitHub CLI command gh auth login, or an SSH key.

GitHub authentication docs

7. Optional: create an SSH key

Run ssh-keygen -t ed25519 -C "you@example.com" and press Enter to accept the defaults. Copy the public key (the .pub file) and add it in GitHub under Settings, SSH and GPG keys, New SSH key. Test it with ssh -T git@github.com.

SSH key docs

Make your first repository and commit

8 steps

A repository is a project folder that Git tracks. A commit is a saved snapshot with a message.

Pro Git book

Official site for this guide. Start here, then follow the cards below.

Free: Free. Everything here works offline.

Visit Pro Git book

1. Create a project folder

Make a folder, open a terminal inside it, and run git init. Git creates a hidden .git folder where it stores history.

2. Add a file

Create a file such as index.html or README.md and write something in it.

3. See the state of things

Run git status. The new file shows as untracked, which means Git sees it but is not recording it yet.

4. Stage the file

Run git add index.html to stage one file, or git add . to stage everything changed. Staging means choosing what goes into the next commit.

5. Make a commit

Run git commit -m "Add homepage". Write short messages that say what changed and why, in the present tense.

6. Look at history

Run git log --oneline to see commits, newest first. Each has a short id you can use later.

7. See what changed

Edit the file, then run git diff to see changes not yet staged, and git diff --staged to see what is staged.

8. Ignore files you should not track

Create a file named .gitignore and list patterns such as node_modules/ and .env. Never commit passwords, API keys or secrets. Add the ignore rule before the first commit.

gitignore templates

Branches and merging

7 steps

A branch is a separate line of work. You can try an idea without touching the working version on main.

Learn Git Branching

Official site for this guide. Start here, then follow the cards below.

Free: Free. Branches are cheap, so use them often.

Visit Learn Git Branching

1. Know where you are

Run git branch to list branches. The one with a star is the branch you are on. On a new repository that is main.

2. Create and switch to a new branch

Run git switch -c add-contact-page. The -c means create. Pick short names that describe the work.

3. Commit on the branch

Edit files, then git add . and git commit -m "Add contact page". These commits belong only to this branch.

4. Go back to main

Run git switch main. Your files change to match main, and the new work is safely stored on the other branch.

5. Merge the branch

While on main, run git merge add-contact-page. If main has not moved, Git simply fast-forwards. Otherwise it creates a merge commit.

6. Fix merge conflicts

If two branches changed the same lines, Git stops and marks the file with <<<<<<<, ======= and >>>>>>>. Edit the file to keep the right version and remove the markers. Then run git add file and git commit to finish the merge. Use git merge --abort to cancel.

7. Delete the finished branch

Run git branch -d add-contact-page. Git refuses if the work is not merged, which protects you from losing it.

Put your code on GitHub

8 steps

GitHub hosts your repository online so you can back it up, share it, and deploy from it.

GitHub

Official site for this guide. Start here, then follow the cards below.

Free: GitHub Free: unlimited public and private repositories. Check the pricing page for current limits on other features.

Visit GitHub

1. Create a GitHub account

Sign up at github.com with your email and verify it.

Sign up

2. Create an empty repository

Click the plus menu, New repository. Choose a name and Public or Private. If you already have a local project, do not tick the option to add a README, so the two histories do not clash.

New repository

3. Connect your local project

Copy the repository URL from GitHub and run git remote add origin URL. Check with git remote -v. Origin is the usual name for your GitHub copy.

4. Push your commits

Run git push -u origin main. The -u links your local main to the remote main, so later you can run just git push.

5. Sign in when asked

Git asks you to authenticate. Use the browser sign-in from Git Credential Manager, a personal access token as the password, or your SSH key if you used an SSH URL.

6. Check on GitHub

Refresh the repository page. You should see your files, the latest commit message, and a History link listing every commit.

7. Clone a repository

To copy any repository to your computer, run git clone URL. This also sets origin for you.

8. Get the latest changes

Run git pull to download and merge new commits from GitHub. Run git fetch to download them without merging yet.

Pull requests and working with others

8 steps

A pull request asks the project owner to review and merge your branch. It is the normal way to contribute on GitHub.

GitHub pull request docs

Official site for this guide. Start here, then follow the cards below.

Free: Pull requests and issues are free on public and private repositories.

Visit GitHub pull request docs

1. Fork the project if you are not a collaborator

On the repository page click Fork. GitHub makes your own copy under your account. If you can write to the repository, skip the fork.

2. Clone and branch

Run git clone URL, then git switch -c fix-typo. Never work directly on main of someone else's project.

3. Commit and push the branch

Make changes, commit them, then run git push -u origin fix-typo.

4. Open the pull request

On GitHub click Compare and pull request. Write a clear title, explain what and why, and submit it.

5. Respond to review

Reviewers can comment on lines. Make fixes on the same branch and push again. The pull request updates by itself.

6. Merge it

The owner chooses Merge commit, Squash and merge (one clean commit) or Rebase and merge. After merging you can delete the branch on GitHub.

7. Update your local copy

Run git switch main, git pull, then git branch -d fix-typo. For a fork, add the original as another remote with git remote add upstream URL and pull from it.

8. Track work with issues

Use the Issues tab to list bugs and ideas. Writing Fixes #12 in a pull request description closes issue 12 when the pull request merges.

GitHub Issues docs

Fix mistakes and undo changes

8 steps

Git is forgiving if you know which command fits. Be careful with anything that rewrites history after you have pushed.

Oh Shit, Git!?!

Official site for this guide. Start here, then follow the cards below.

Free: Free. Nothing here needs a paid plan.

Visit Oh Shit, Git!?!

1. Throw away changes in a file

Run git restore file to put the file back to the last commit. This cannot be undone, so check with git diff first.

2. Unstage a file

Run git restore --staged file to remove it from the next commit but keep your edits.

3. Fix the last commit message

Run git commit --amend -m "New message". Only do this if you have not pushed the commit yet.

4. Undo the last commit but keep the work

Run git reset --soft HEAD~1. The commit disappears, and its changes stay staged.

5. Undo a commit you already pushed

Run git revert COMMIT_ID. This adds a new commit that cancels the old one, so shared history stays intact. Find the id with git log --oneline.

6. Set work aside

Run git stash to hide uncommitted changes, switch branches freely, and run git stash pop to bring them back.

7. Recover lost commits

Run git reflog to list where HEAD has been. Find the commit you want and run git switch -c rescue COMMIT_ID to get it back.

8. If you pushed a secret

Revoke or rotate the password or key immediately. Removing it from a later commit is not enough because it stays in history and may already be copied.

Command quick reference

12 items

The commands you will use every day.

git status

Show which files changed and which are staged.

git add .

Stage all changes in the current folder.

git commit -m "message"

Save a snapshot of the staged changes.

git log --oneline

Show a short history of commits.

git diff

Show changes that are not staged yet.

git branch

List branches.

git switch -c name

Create a branch and switch to it.

git merge name

Merge a branch into the one you are on.

git remote -v

Show the remote URLs, such as origin.

git push

Upload your commits to GitHub.

git pull

Download and merge new commits from GitHub.

git clone URL

Copy a repository to your computer.

Tools and places to learn more

6 items

Graphical apps, command-line helpers and practice sites.

GitHub Docs

Official site for this guide. Start here, then follow the cards below.

Free: All free to use.

Visit GitHub Docs

GitHub CLI

The gh command for logging in, cloning, creating pull requests and more from the terminal.

Open GitHub CLI

VS Code Source Control

Built into Visual Studio Code, with a panel for staging, committing and syncing.

Open VS Code docs

Pro Git book

The free official book about Git, from basics to internals.

Read Pro Git