Harness the Combinatoric Power of Command-Line Tools and Utilities

Git Basics

Lesson 5 - Sharing Your Work

Git

Commits only exist on your computer until you send them somewhere else. In this module you’ll connect your repository to a remote repository, push your commits to it, and clone it to a new location.

The sandbox starts with the repository from the previous modules. Services like GitHub host remote repositories for you. Employers often look at GitHub profiles as an indicator of skill. The commands you’ll use here work the same way with GitHub. The only difference is the address of the remote.

Creating a Remote Repository

When you create a repository on GitHub, GitHub gives you a URL that looks like https://github.com/yourname/awesomeco_site.git. Don’t let GitHub add a README, a .gitignore file, or a license when you plan to push existing code. You want the remote repository empty.

The sandbox can’t reach GitHub, so you’ll create an empty repository on disk instead. The --bare flag creates a repository with no working files, which is how hosting services store repositories:

git init --bare /tmp/remote/awesomeco_site.git

Adding a Remote

Back in your project, add a remote called origin that points to the new repository. The name origin is the convention for the primary remote:

cd /tmp/awesomeco_site
git remote add origin /tmp/remote/awesomeco_site.git
git remote -v

With GitHub, you’d use the URL from the repository page in place of the path.

Pushing Commits

Push your commits to the remote:

git push -u origin main

The -u flag tells Git to remember that your main branch tracks origin/main. From now on, git status tells you when your local branch is ahead of the remote, and you can push with a plain git push.

With GitHub, Git asks you to authenticate the first time you push.

Updating the Remote

When you make changes in the future, you add one more step to the workflow:

  1. Make changes to your code.
  2. Use git status to see what you need to do.
  3. Use git diff to review what you changed.
  4. Use git add to stage the files you changed.
  5. Use git commit -m "your message" to take a snapshot of your code.
  6. Use git push to send your commits to the remote.

Work through this process by adding a README.md file. A README tells people about the project, such as who wrote it and how to use it. The file uses Markdown, a plain text format that converts to HTML. Lines that start with # become headings:

cat > README.md <<'MD'
# AwesomeCo Website

This is a prototype website I built to learn Git.

## Author

Jane Doe

Copyright 2045 Jane Doe, all rights reserved.
MD
git status

As you can probably guess by now, you need to stage the file:

git add README.md
git status

Commit it:

git commit -m "Add README"
git status

The README now lives in your local repository, but nobody else can see it until you push. The status even says so. Your branch is ahead of origin/main by one commit. Push it:

git push
git status

That last step is crucial. Commits only happen on your computer. If you want others to see your changes, you have to push them.

Getting Code From a Remote

If you want to work on the project from another computer, use git clone to copy the repository. Clone the remote into a new folder:

cd /tmp
git clone /tmp/remote/awesomeco_site.git awesomeco_copy
cd awesomeco_copy
ls
git log --oneline

The clone contains every file and every commit anyone ever made. You have the entire history on the new computer. With GitHub, you’d clone with the repository URL instead of the path.

A clone already has origin configured, so you follow the same workflow you’ve used all along. Make your changes, stage the files, commit, and push.

Your Turn

In the clone, add a CONTRIBUTING.md file, commit it, and push it to the remote. Then look at the log of the remote repository to confirm the commit arrived:

echo "# Contributing" > CONTRIBUTING.md
git add CONTRIBUTING.md
git commit -m "Add contributing guide"
git push
git -C /tmp/remote/awesomeco_site.git log --oneline

What You Learned

  • git remote add origin connects your repository to a remote
  • git push -u origin main sends your commits and sets up tracking
  • Commits stay on your computer until you push them
  • git clone copies an entire repository, including its history

Git Cheat Sheet

CommandWhat it does
git initTurn a folder into a repository
git statusShow changed files and what to do next
git addStage files for the next commit
git commit -mTake a snapshot with a message
git commit -amStage tracked files and commit in one step
git diffShow which lines changed
git logShow the commit history
git log --onelineShow a brief commit history
git switch --detachLook at an old commit
git restoreThrow away uncommitted changes to a file
git remote add originConnect to a remote repository
git pushSend commits to the remote
git cloneCopy a repository and its history

You’ve completed the Git Basics course. Practice the workflow on your own projects. Add files and commit often, and you’ll gain the ability to go back in time whenever a change breaks something.