Create and configure your GitHub or GitLab account

Create and configure your GitHub or GitLab account #

Git works perfectly well with no account on anything. You can commit for years on your own laptop and never talk to a server. But sooner or later you want a copy of your work somewhere that isn’t your laptop, and you want other people to be able to read it — and that means an account on a hosting service.

This chapter is the boring administrative part. Fifteen minutes of clicking, and then we are done with it forever. We will do GitHub and GitLab side by side, because they ask you for very nearly the same things.

ℹ️ If you want to know which one to pick, and what else exists, that is a real discussion and it belongs in Git hosting, not here. For now: pick either. Nothing in this course depends on the choice.

Create the account #

Both want an email address, a password, and a username.

⚠️ Think about the username for thirty seconds. It goes into the URL of every repository you will ever own — github.com/<username>/<project> — into every clone command anyone runs against your code, and into the noreply email address the service generates for you. Both services will let you rename an account, and both will warn you that links to the old name break. Renaming is genuinely painful. xX_dark_coder_1997_Xx felt right at the time; it is going on your CV.

Then check your mail and click the verification link. Neither service will let you do much of anything until the email address is verified — on GitLab an unverified account cannot even push.

Turn on two-factor authentication now #

Do this before anything else, while you still have the patience for it.

Your Git hosting account is, for most developers, the single most damaging account to lose. It is not just your code: it is your name on commits, your published packages, your deploy keys, and — if you use it as an identity provider — a pile of other accounts.

Both services have moved from “please” to “you must”:

  • GitHub has required two-factor authentication since March 2023 for every user who contributes code — the criteria include publishing an app or an action, creating a release, being an organization owner, or contributing to a repository GitHub considers high-importance. It was rolled out in cohorts, each with a 45-day enrollment window, and the last cohort’s deadline was 19 January 2024. It is not (as of writing) required of every single account, but if you are following this course you are on your way into the group it applies to. Turn it on.

  • GitLab.com announced mandatory multi-factor authentication in January 2026, with enforcement starting 27 April 2026 and rolling out in cohorts through the year. It applies to any sign-in or API request made with a username and password. Git over SSH and Git over HTTPS with a token are unaffected.

    ℹ️ Fast-moving: GitLab’s MFA enforcement schedule evolves. Check GitLab’s current documentation for the latest dates.

You have a choice of second factor. In rough order of how much we like them:

  1. A passkey or a hardware security key (a YubiKey, or the passkey your phone or laptop already offers). Phishing-resistant, because the key checks which site is asking. On GitHub a passkey can replace the password entirely.
  2. A TOTP app — the six digits that change every thirty seconds. Any of them will do. This is the sane default and it works offline.
  3. SMS, which GitHub supports but actively discourages: SIM swapping is a real attack, delivery is unreliable, and it is not available everywhere. Use it as a last resort, not a first choice.

⚠️ Save the recovery codes. When you enable 2FA, GitHub hands you a list of sixteen single-use recovery codes as a file called github-recovery-codes.txt. Download it. Put it in your password manager, or print it and put it in a drawer. If you lose your second factor and your recovery codes, GitHub Support cannot get your account back for you — they say so explicitly. This is not a threat, it is arithmetic: if support could restore access, so could anyone who convincingly impersonated you.

Profile hygiene, briefly #

Two things are worth five minutes.

Your commit email is public. Every commit you push carries the email address from your user.email — see Installing and configuring Git — and anyone who clones the repository has it forever. If you would rather not publish your personal address, GitHub gives you a noreply alias of the form ID+USERNAME@users.noreply.github.com (accounts from before July 2017 have the older USERNAME@users.noreply.github.com form), and a “Keep my email addresses private” setting that makes web-based commits use it. Then set that address locally:

git config --global user.email "1234567+yourname@users.noreply.github.com"

GitHub will still attribute the commits to you, because it knows the alias belongs to you.

Your public repositories are a portfolio whether you meant them to be or not. As someone who reads a lot of candidate code: the first thing I look at is not the cleverest repository, it is the commit messages in whatever you last touched. That is not a reason to be paranoid — it is a reason to write the commit message.

Add your SSH key #

Generate the key and understand what it is in Configure Git with an SSH key; we will not repeat it here. Where the public key goes:

One wrinkle worth knowing, because it confuses people the first time. Both services distinguish between a key used to authenticate (prove it is you pushing) and a key used to sign (prove a commit is really yours). They handle it differently:

  • GitHub asks you to choose a type when you upload — “Authentication key” or “Signing key” — and if you want to use one key for both, you upload the same public key twice, once as each type. This surprises everyone.
  • GitLab lets a single key entry carry the usage “Authentication & Signing”, so one upload is enough.

Commit signing itself is in Configure Git with an SSH key.

Personal access tokens #

A token is a long random string that stands in for your password when something needs to talk to the service over HTTPS — a script, a CI job, a tool. GitHub stopped accepting account passwords for Git operations on 13 August 2021, so if you push over HTTPS, a token is what you are actually typing.

GitHub has two kinds:

  • Fine-grained tokens — scoped to specific repositories and to specific permissions, generally available since March 2025. Default expiry is 30 days; the maximum is 366 days (or no expiry, if policy allows it).
  • Classic tokens — one flat list of coarse scopes (repo gives access to all your repositories, public and private). Not formally deprecated, still needed for a few features, but GitHub recommends fine-grained tokens where they work and has said it eventually intends to disable the classic ones.

GitLab’s equivalent lives under Access tokens in your user settings, with its own scope list (read_repository, write_repository, api, …) and a required expiry date.

Three rules:

  1. Scope narrowly. A token that only needs to read one repository should be able to read one repository. The failure mode of a broad token is not “someone reads my code”, it is “someone pushes to all of it”.
  2. Set an expiry. A token that expires is a leak with a deadline.
  3. Never put a token in a file inside the repository. Not in config.py, not in .env, not “temporarily”. Tokens go in a credential helper or an environment variable — see the HTTPS section of Configure Git with an SSH key. Both services scan public pushes for things that look like their own tokens and will revoke them, which is kind of them, but do not make it their job.

Your first repository, connected to the one from Part 1 #

On the web interface, create a repository. Name it my_first_git_project, to match the one we built in First steps, first Git commands.

⚠️ Do not tick “Add a README” (or GitLab’s “Initialize repository with a README”). We already have commits locally, and a README created on the server is a commit we do not have. That is the single most common first-push failure and we show it below. Create the repository completely empty.

The forge then shows you the two lines you need. From inside your local repository:

cd ~/projects/my_first_git_project
git remote add origin git@github.com:yourname/my_first_git_project.git
git branch -M main
git push -u origin main

Which answers:

To github.com:yourname/my_first_git_project.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

That’s it. Your work is in two places. What remote, push and -u actually mean is the subject of Retrieve and send code.

The four ways that first push goes wrong #

“src refspec main does not match any”

error: src refspec main does not match any
error: failed to push some refs to 'github.com:yourname/my_first_git_project.git'

You asked Git to push a branch called main and you do not have one — because git init still creates master unless you told it otherwise, while GitHub and GitLab name their default main. Check with git branch --show-current. Rename with git branch -M main and push again. Then set init.defaultBranch so it never happens again.

This is also what you get if you have made no commits at all: an empty branch is not a branch.

“Updates were rejected because the remote contains work that you do not have locally”

To github.com:yourname/my_first_git_project.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:yourname/my_first_git_project.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

This is the README you were told not to tick. The server has a commit; you have a commit; neither is an ancestor of the other. Bring theirs underneath yours:

git pull --rebase origin main
git push -u origin main

--rebase replays your commits on top of theirs, and unlike a merge it does not object to the two histories being unrelated. If you would rather not think about it at all, and the server-side repository contains nothing you care about, delete it on the web interface and start again with an empty one.

“Repository not found” (GitHub) or “The project you were looking for could not be found” (GitLab)

This one lies to you. GitHub deliberately returns the same message for “this repository does not exist” and “this repository exists and you may not see it”, because telling you which would leak the existence of private repositories. So the checklist is:

  • Is the spelling of owner and repository name exactly right? Including case.
  • Are you the account you think you are? ssh -T git@github.com will tell you which user your key authenticates as.
  • If it is someone else’s repository — do you have push access, or are you supposed to fork it?

“Permission denied (publickey)”

Your key never got in. That whole diagnosis lives in Configure Git with an SSH key.

The half-dozen settings that matter on day one #

Private or public. You can flip either way later, but understand what “later” means:

⚠️ Making a private repository public exposes its entire history, not its current state. Every commit, every branch, every file you ever committed and then deleted. The credentials you committed in week one and removed in week two are in the history, and going public publishes them. Deleting a file does not remove it from Git — that is the whole point of Git. If a repository has ever held a secret, treat “make public” as “publish that secret”, and rotate it first.

The default branch name. Both services default to main for new repositories, and both let you change the default for your account. Match whatever you set in init.defaultBranch locally and you save yourself the master/main dance every single time.

Branch protection on main, even alone. It sounds like ceremony for a solo project, and it is the cheapest insurance you will ever buy: a rule that forbids force-pushing to main means the afternoon you mistype a git push --force costs you a swear word instead of a weekend. On GitHub this is Settings → Branches (or the newer Rulesets); on GitLab, Settings → Repository → Protected branches. Tick “do not allow force pushes” and stop there. Requiring pull requests when you are the only developer is where it stops being worth it.

A README. Not for other people. For you, in eighteen months, when you have forgotten how to run your own project.

A licence — and this one really matters.

⚠️ A repository with no licence file is “all rights reserved” by default. Not public domain, not “do what you like”. Copyright is automatic; putting code on the internet does not grant anyone permission to use it. So if you want people to be able to use your code, you have to say so, in a file, on purpose. Both services offer a licence picker at creation time; https://choosealicense.com/ will walk you through the choice in about ninety seconds. MIT if you want “do anything, just keep my name on it”. GPL if you want changes given back.

A .gitignore. Both forges offer language templates at creation time (Python, Node, Go…) drawn from https://github.com/github/gitignore, and they are a good starting point. What .gitignore is for, and how to write your own, is in A little structure please.

GitHub or GitLab? #

Honestly: for learning Git, it does not matter, and you will end up with accounts on both anyway because other people’s projects live on both.

GitHub is where the open-source world is, which makes it where the network effect is. GitLab ships more of the surrounding machinery in the box and is much more pleasant to self-host. Both are excellent Git servers, which is the only part this course is about.

And they are not the only two. Codeberg (running Forgejo) is a non-profit alternative, Forgejo and Gitea are easy to self-host, and a bare repository over SSH on any machine you control is already a perfectly real Git server — that is the subject of Git hosting. Your history is yours: whatever you choose, git clone gets all of it back out.

Doing all of it from the terminal: gh and glab #

If you would rather not click, both services have an official command-line client — gh for GitHub, glab for GitLab. Install them from your package manager — brew install gh, or your distribution’s package, or winget on Windows.

gh auth login

This walks you through a browser login, stores the token in your system credential store, and offers to set up your SSH key and configure Git for you. It replaces most of this chapter.

Then, from inside the local repository we made in Part 1:

gh repo create my_first_git_project --private --source=. --push

That creates the repository on GitHub, adds it as origin, and pushes — the whole “first repository” section in one line. gh repo create with no arguments is interactive and will also offer --gitignore and --license templates. gh auth setup-git configures gh as Git’s credential helper so HTTPS pushes stop asking for anything.

glab auth login and glab repo create do the same job on GitLab.

ℹ️ These CLIs are also the sane way to script anything against a forge — opening pull requests, listing issues, downloading CI logs — without hand-rolling API calls. More on the surrounding tooling in Making the command line yours.

Summary git remote add git push -u #

  • An account on a hosting service is not required to use Git, but it is how you get a copy of your work off your laptop and in front of other people.
  • Your username ends up in every URL of every repository you own. Choose it deliberately.
  • Turn on two-factor authentication immediately — GitHub has required it of code contributors since March 2023, GitLab.com began enforcing MFA for password sign-ins in April 2026 — and save the recovery codes somewhere that is not the laptop.
  • Your commit email is public forever. GitHub’s ID+USERNAME@users.noreply.github.com alias exists if you would rather it not be your real one.
  • Forges distinguish authentication keys from signing keys; GitHub wants the same key uploaded twice if you use it for both.
  • Personal access tokens stand in for a password over HTTPS (passwords stopped working on GitHub in August 2021). Scope them narrowly, give them an expiry, and never commit one.
  • git remote add origin <url> names a remote; git push -u origin main sends a branch and remembers where it went.
  • The classic first-push failures are src refspec main does not match any (your branch is called master), a rejected non-fast-forward (the forge created a README), and Repository not found (usually permissions, not a missing repository).
  • A repository with no licence is all rights reserved. If you want your code used, say so in a file.
  • Making a private repository public exposes its entire history, not just its current contents.
  • gh auth login and gh repo create --source=. --push do most of this chapter from the command line.