DevOps.Academy
CoursesCurriculumPricing

    Loading…

    ↑ ↓ move · ↵ open · Ctrl K anywhere

    ◔My learningSign in
    DevOps.Academy

    Free, hands-on DevOps lessons. Made with care in India.

    Learn

    All coursesLinux roadmapCurriculumMy learning

    Academy

    PricingCertificates

    Legal

    PrivacyTermsContact

    devops.rajeev.pro

    Git & GitHubLesson 4 of 13
    Your progress · 0%
    On this page
    1. Git and GitHub
    2. Your account, safely
    3. Creating a repository
    4. Public or private?
    5. Fork or clone?
    ↖ Course roadmap
    Lesson 4 · GitHub essentials

    GitHub: your account, repositories, forks and clones

    Set up a GitHub account safely, keep your email private, create repositories from the web or the gh CLI, choose public or private, and know exactly when to fork and when to clone.

    25 min read · beginner · hands-on

    By the end of this lesson you'll be able to

    0 of 6 complete

    Git and GitHub

    Git is the tool on your machine: commits, branches, history. It works with no network at all. GitHub is a service that hosts Git repositories so a team can share them, and adds the work around code: pull requests and reviews, issues, Actions for CI/CD, releases and security scanning. GitLab and Bitbucket do the same job; the Git commands are identical on all three.

    Your account, safely

    Your GitHub account can push code that other people deploy, so protect it like a production login:

    1. Two-factor authentication. GitHub requires it for anyone who contributes code. Use an authenticator app or a passkey, and save the recovery codes somewhere safe, not on the same laptop.
    2. Keep your email private. Every commit records the author's email, and public commits stay public for good. In Settings → Emails, turn on Keep my email addresses private, and also Block command line pushes that expose my email. GitHub gives you an address like 12345678+rajeev@users.noreply.github.com; set it as your email for public work:
    $ git config --global user.email "12345678+rajeev@users.noreply.github.com"
    
    git config --global user.email "12345678+rajeev@users.noreply.github.com"
    • gitversion control
    • configread or set settings
    • --globalfor your user, in every repo
    1. A profile people can read. Add your name, a photo and a short bio. Create a public repository named exactly like your username, and its README appears at the top of your profile: a good place for what you work on and links to your best projects.

    Connecting your machine

    Git needs to prove who you are when you push. The simplest way is the GitHub CLI, gh:

    $ sudo apt install gh             # or brew install gh, winget install GitHub.cli
    $ gh auth login                   # choose GitHub.com, HTTPS or SSH, log in with the browser
    $ gh auth status                  # who you're logged in as, and how
    
    sudo apt install gh
    • sudorun the next command as root
    • aptDebian/Ubuntu package manager

    gh auth login stores a token and sets Git up to use it, so git push over HTTPS just works. If you prefer SSH, make a key as in the Linux course's Networking lesson (ssh-keygen -t ed25519), add the public key in Settings → SSH and GPG keys, and test it with ssh -T git@github.com. Passwords no longer work for Git over HTTPS on GitHub; it's a token or SSH.

    Creating a repository

    On the website: the + menu, then New repository. Pick a name, a description, the visibility, and optionally a README, a .gitignore template and a licence.

    From the command line, for a project that already exists on your machine:

    $ cd ~/projects/shop
    $ gh repo create shop --private --source=. --push    # create on GitHub, add origin, push
    $ git remote -v
    origin	https://github.com/rajeev/shop.git (fetch)
    origin	https://github.com/rajeev/shop.git (push)
    
    cd ~/projects/shop
    • cdchange directory
    git remote -v
    • gitversion control

    Or start a fresh one on GitHub and clone it straight away:

    $ gh repo create notes --public --add-readme --license mit --gitignore Python --clone
    
    Starter fileWhy
    README.mdthe front page: what this is, how to run it, how to contribute
    .gitignorekeeps secrets and build output out from the first commit
    LICENSEwithout one, nobody may legally reuse public code; MIT and Apache-2.0 are common

    Public or private?

    PublicPrivate
    Who can see iteveryone, and search enginesyou and the people you invite
    Who can pushonly collaborators you addonly collaborators you add
    Good foropen source, portfolio, docscompany code, anything with customers' details
    Cost on the free planfreefree, with unlimited collaborators

    Public means readable, not writable: strangers can't push to your repository, they can only fork it and ask you to pull their changes.

    Public is forever

    Bots scan every public push for passwords and API keys, often within minutes. Deleting the file in a new commit, rewriting history or switching the repository to private doesn't recall copies already made. If a secret was ever pushed, revoke it and issue a new one. The Security lesson shows how GitHub's secret scanning and push protection stop this before it happens.

    Fork or clone?

    These two words get mixed up constantly. They do different things in different places:

    Fork: a copy on GitHub. Clone: a copy on your machine. Contributors do both.© Diagram: DevOps Academy

    Fork: a copy on GitHub. Clone: a copy on your machine. Contributors do both.© Diagram: DevOps Academy
    ForkClone
    Where the copy ison GitHub, under your accounton your machine
    Made withthe Fork button, or gh repo forkgit clone <url>, or gh repo clone
    Use it whenyou can't push to the originalyou want to work on any repository locally
    • Your team's repository (you have write access): just clone it, branch, push, open a pull request.
    • Someone else's project: fork it, clone your fork, push to your fork, open a pull request to the original. The Collaboration lesson walks through it.
    $ gh repo fork owner/project --clone     # fork on GitHub and clone your fork
    $ git remote -v                          # origin = your fork, upstream = the original
    $ git fetch upstream                     # get the original's newest commits
    
    git remote -v
    • gitversion control
    At work

    Use one GitHub account for work and personal projects, and add your work email to it: organisations grant access to your account, and you keep your history when you change jobs. Your company may require SSO, which links your account to the company login without creating a second account.

    ⌘
    Mini mission

    Publish your project to GitHub

    Put the shop repository from the earlier lessons on GitHub as a private repository, using your private noreply email, then check the push from both sides: GitHub and your terminal.

    Input: A GitHub account with 2FA, and the shop repositoryOutput: github.com/<you>/shop, private, with your commits
    Reveal one safe solution
    $ gh auth login
    $ gh auth status
    $ cd ~/projects/shop
    $ git config user.email                 # make sure this is your noreply address
    $ gh repo create shop --private --source=. --push
    $ git status -sb                        # main is now tracking origin/main
    ## main...origin/main
    $ gh repo view --web                    # opens the repository in your browser
    
    cd ~/projects/shop
    • cdchange directory
    git config user.email
    • gitversion control
    • configread or set settings
    git status -sb
    • statuswhat changed

    On GitHub, check the commit count matches git log --oneline | wc -l, and that .env isn't listed. git status -sb showing main...origin/main with nothing after it means your local branch and GitHub's are identical. The Remotes lesson explains what that line can tell you.

    Try it yourself

    0 of 6 steps done

    Run each task in your own account or terminal, then tick it off.

    Knowledge check

    Quick check

    Question 1 of 5

    What is the difference between Git and GitHub?

    Go deeper

    Official reading for this lesson

    • DocsGitHub Docs: about GitHub and GitGitHub Docs ↗
    • DocsGitHub Docs: setting your commit email addressGitHub Docs ↗
    • DocsGitHub Docs: configuring two-factor authenticationGitHub Docs ↗
    • DocsGitHub Docs: fork a repositoryGitHub Docs ↗
    • ToolGitHub CLI manualGitHub ↗
    • GuideChoose an open source licenceGitHub ↗

    Or get every quiz answer right and it completes itself.

    ← PreviousBranches: work on many things without breaking mainNext lesson →Remotes: push, fetch and pull without surprises