Lesson 5 - Sharing Your Work
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.gitAdding 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 -vWith 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 mainThe -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:
- Make changes to your code.
- Use
git statusto see what you need to do. - Use
git diffto review what you changed. - Use
git addto stage the files you changed. - Use
git commit -m "your message"to take a snapshot of your code. - Use
git pushto 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 statusAs you can probably guess by now, you need to stage the file:
git add README.md
git statusCommit it:
git commit -m "Add README"
git statusThe 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 statusThat 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 --onelineThe 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 --onelineWhat You Learned
git remote add originconnects your repository to a remotegit push -u origin mainsends your commits and sets up tracking- Commits stay on your computer until you push them
git clonecopies an entire repository, including its history
Git Cheat Sheet
| Command | What it does |
|---|---|
git init | Turn a folder into a repository |
git status | Show changed files and what to do next |
git add | Stage files for the next commit |
git commit -m | Take a snapshot with a message |
git commit -am | Stage tracked files and commit in one step |
git diff | Show which lines changed |
git log | Show the commit history |
git log --oneline | Show a brief commit history |
git switch --detach | Look at an old commit |
git restore | Throw away uncommitted changes to a file |
git remote add origin | Connect to a remote repository |
git push | Send commits to the remote |
git clone | Copy 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.