Git Tutorial menu

Git and GitHub

GitHub stores your repository online, so it is backed up, you can work from any computer, and others can see or join your project.

Create a GitHub Account and Repository

  1. Sign up for a free account at github.com. Use a professional username; it will appear on your portfolio.
  2. Click New repository (the + button at the top).
  3. Give it a name, like my-site, and choose Public or Private.
  4. Do not add a README if you already have a local project. Click Create repository.

GitHub then shows the address of your new repository, such as https://github.com/ayesha-dev/my-site.git.

Connect Your Project: git remote

A remote is a named link to a repository somewhere else. The usual name for the main one is origin:

Example

bash
git remote add origin https://github.com/ayesha-dev/my-site.git
git remote -v
Output
origin	https://github.com/ayesha-dev/my-site.git (fetch)
origin	https://github.com/ayesha-dev/my-site.git (push)

git push

Send your commits to GitHub:

Example

bash
git push -u origin main
Output
Enumerating objects: 9, done.
Writing objects: 100% (9/9), 812 bytes | 812.00 KiB/s, done.
To https://github.com/ayesha-dev/my-site.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

-u links your local main to origin/main. After that, a plain git push is enough.

Logging In

GitHub no longer accepts your account password on the command line. Use one of these:

  • Git Credential Manager (installed with Git for Windows): a browser window opens and you log in once
  • Personal access token: create one in GitHub Settings, Developer settings, and paste it when Git asks for a password
  • SSH key: generate a key and add it to your GitHub account

Example

bash
ssh-keygen -t ed25519 -C "ayesha@example.com"
cat ~/.ssh/id_ed25519.pub

Copy the printed public key into GitHub, Settings, SSH and GPG keys. Then use the SSH address, like git@github.com:ayesha-dev/my-site.git.

Never share your private key (the file without .pub) or your token. Treat them like passwords.

git pull

When the GitHub copy has commits you do not have (for example, a teammate pushed, or you edited on another computer), bring them down:

Example

bash
git pull
Output
Updating 7d1f3a8..b2c9e04
Fast-forward
 about.html | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

git pull is really two steps: git fetch (download new commits) and git merge (combine them with your branch).

CommandWhat it does
git fetchDownload new commits, but do not change your files
git pullFetch and merge into your current branch
git pushUpload your commits
git clone <url>Download a full copy of a repository

git clone

To get a copy of any repository, for example on a new laptop or a classmate's project:

Example

bash
git clone https://github.com/ayesha-dev/my-site.git
cd my-site
git log --oneline -2
Output
b2c9e04 (HEAD -> main, origin/main) Add about page
7d1f3a8 Merge new-heading and combine both headings

Cloning downloads the full history and sets up origin for you automatically.

Push Rejected?

If someone pushed before you, GitHub rejects your push:

Output
! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/ayesha-dev/my-site.git'

Run git pull first, fix any conflicts, then git push again.

README.md

A README.md file in the main folder is shown on your repository's GitHub page. Use it to explain what the project is and how to run it. It is the first thing a recruiter or client sees.

Exercise

Exercise

Which command uploads your local commits to GitHub?

Learn Git with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp