basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Git from scratch: the first repository and a push to GitHub over SSH

    Install git, introduce yourself to it, create a repository with a .gitignore, make the first commit and push it to GitHub with an SSH key. Then the daily loop: edit, status, diff, commit, push. You need an SSH key (SSH keys) and a GitHub account.

    What you end up with

    ~/notes/                         the working directory
      .git/                          history, branches, repository settings — don't touch by hand
      .gitignore                     what git doesn't see: .env, node_modules/, logs
      README.md
    github.com/example-user/notes      the remote repository "origin", accessed with an SSH key

    Setup

    1. Install git

      Where How Version
      Ubuntu 24.04 sudo apt install git 2.43
      Ubuntu 26.04 sudo apt install git 2.53
      Windows Git for Windows from git-scm.com, or winget install Git.Git 2.55 as of October 2026
      macOS xcode-select --install or brew install git Apple’s lags behind, Homebrew’s is current

      The Git for Windows installer asks about line endings. The default answer (“Checkout Windows-style, commit Unix-style”) turns on core.autocrlf=true — a trap for scripts headed to a server (Line endings in .gitattributes).

      git --version
      # git version 2.55.0.windows.5
    2. Introduce yourself

      git config --global user.name "example-user"
      git config --global user.email "[email protected]"
      git config --global init.defaultBranch main

      Name and email go into every commit and are visible to anyone who can see the repository. To keep your personal email private, GitHub gives you an …@users.noreply.github.com address: it’s in the account settings under “Emails”, and commits with it are still linked to your account.

      init.defaultBranch main — without this line git 2.x names the first branch master and shows a 13-line hint; in git 3.0 the default becomes main. Other settings — ~/.gitconfig block by block.

    3. Create the repository

      mkdir ~/notes && cd ~/notes
      git init
      # Initialized empty Git repository in /home/me/notes/.git/

      Everything git knows about this directory is in .git/. Without .git there’s no history: delete it and the files stay, but the history is gone.

    4. First of all — .gitignore

      Before the first git add, decide what must never get into the repository:

      # .gitignore
      .env
      *.key
      node_modules/
      dist/
      *.log
      .DS_Store
      Thumbs.db

      .gitignore only affects files git doesn’t track yet. A .env that made it into a commit won’t be removed from history by a line in .gitignore (.gitignore and a tracked file), and if it holds a password, you’ll have to change it (A secret in a commit). Ready-made lists for each language and framework are at github.com/github/gitignore.

    5. The first commit

      echo "# Notes" > README.md
      git status
      # On branch main
      #
      # No commits yet
      #
      # Untracked files:
      #   (use "git add <file>..." to include in what will be committed)
      # 	.gitignore
      # 	README.md

      Untracked means git sees the files but doesn’t track them. Put them in the index and commit:

      git add .gitignore README.md
      git commit -m "README and .gitignore"
      # [main (root-commit) 6f203cf] README and .gitignore
      #  2 files changed, 9 insertions(+)
      #  create mode 100644 .gitignore
      #  create mode 100644 README.md

      6f203cf is the start of the commit’s hash; any command can refer to the commit by it.

    6. The SSH key — into GitHub

      The key from SSH keys will do. Add the public half, ~/.ssh/id_ed25519.pub, to GitHub: Settings, SSH and GPG keys, New SSH key, type Authentication Key. Check without git:

      ssh -T [email protected]
      # Hi example-user! You've successfully authenticated, but GitHub does not provide shell access.

      On the first connection ssh asks about the github.com key: the ED25519 fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU, published in GitHub’s documentation (Server fingerprint).

    7. An empty repository on GitHub, and push

      On GitHub, create a repository called notes — empty: no README, .gitignore or license. Otherwise it has its own history, and the first push gets refused (see “If it doesn’t work”). Take the SSH URL, not HTTPS:

      git remote add origin [email protected]:example-user/notes.git
      git push -u origin main
      # To github.com:example-user/notes.git
      #  * [new branch]      main -> main
      # branch 'main' set up to track 'origin/main'.

      origin is the conventional name for the remote repository. -u links the local main to origin/main: from then on, plain git push and git pull are enough.

    8. The daily loop

      git status                 # what changed
      git diff                   # line by line: what exactly
      git add -p                 # into the commit piece by piece, with a prompt for each
      git commit -m "traefik: readTimeout 300s — uploads over a minute were cut off"
      git push

      git diff before committing is your last chance to notice something extra riding along: a password, a debug line, someone else’s file. The message says what and why; “fix” and “changes” will say nothing in a month (Everyday git).

    9. A second computer

      git clone [email protected]:example-user/notes.git
      cd notes
      git log --oneline -3
      # 6f203cf (HEAD -> main, origin/main) README and .gitignore

      Before working, git pull; after, git push. Forgot pull and already committed — Branches in git.

    If it doesn’t work

    error: src refspec main does not match any

    Cause
    The repository has no commits yet, or the branch is called master while you push main.
    Fix
    git branch --show-current shows the name; no commits — git commit first. To rename: git branch -m main.

    fatal: refusing to merge unrelated histories

    Cause
    The GitHub repository was created with a README or a license — it has its own history, unrelated to yours.
    Fix
    Once: git pull origin main --allow-unrelated-histories, resolve the conflicts, git push. Next time, create an empty repository on GitHub.

    remote: Support for password authentication was removed on August 13, 2021.

    Cause
    The repository URL is https://…, and git asks for your account password, which GitHub won’t accept.
    Fix
    Switch to SSH: git remote set-url origin [email protected]:example-user/notes.git.

    error: remote origin already exists.

    Cause
    git remote add for a name that already exists — for example, you repeated the command with a corrected URL.
    Fix
    git remote set-url origin <url>, and git remote -v to check.

    nothing added to commit but untracked files present

    Cause
    The files are new and weren’t added to the index: git commit commits only the index.
    Fix
    git add <file> or git add ., check with git status, then git commit.

    How to check

    The local branch is linked to GitHub and nothing is behind:

    git status -sb
    # ## main...origin/main
    git remote -v
    # origin  [email protected]:example-user/notes.git (fetch)
    # origin  [email protected]:example-user/notes.git (push)

    ## main...origin/main without [ahead 1] or [behind 2] means you and GitHub have the same thing. A URL starting with git@ means you go over SSH, and no password will be asked.