Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 4Name in the course: Working with remotes

Share work with remote repositories

A remote is another copy of the repository — usually on GitHub. You push your commits to it and fetch or pull the commits of others. This chapter covers clone, remotes, fetch, pull, push and tracking branches.

  • 5 Lessons
  • 2 Commands

$ run as a normal user# run as root (or with sudo)

4.1

Clone a repository

git clone copies a whole repository with its history and sets the remote origin.

git clone git@github.com:s0sta/infra-shop.git
git clone --depth 1 https://github.com/s0sta/it-notes.git
cd infra-shop && git remote -v
origin  git@github.com:s0sta/infra-shop.git (fetch)
origin  git@github.com:s0sta/infra-shop.git (push)

Remember

Use SSH URLs (git@github.com:…) with a key, or HTTPS with a token. Passwords do not work on GitHub any more.

Commands in this lesson

4.2

Manage remotes

A repository can have several remotes — for example origin (your fork) and upstream (the original project).

git remote add upstream https://github.com/hashicorp/terraform-provider-azurerm.git
git remote -v
git remote set-url origin git@github.com:s0sta/infra-shop.git
git remote rename upstream source
git remote remove source

Commands in this lesson

4.3

Fetch and pull

fetch downloads new commits but changes nothing in your branch. pull = fetch + merge (or rebase) into your branch.

git fetch origin
git log --oneline main..origin/main
git pull
git pull --rebase
git fetch --prune
CommandChanges your branch?
git fetchNo, only origin/…
git pullYes, merges origin/BRANCH
git pull --rebaseYes, puts your commits on top of the new ones

At work

git fetch + look + then merge is safer than a blind git pull on important branches.

Commands in this lesson

4.4

Push your branch

git push sends your commits. The first push of a new branch sets the upstream with -u.

git push -u origin feature/web-network
git push
git push origin --delete feature/old
git push --tags
git push --force-with-lease

Watch out

--force overwrites the remote branch and can delete others' commits. If you must rewrite a pushed branch (your own feature branch), use --force-with-lease — it refuses when someone else pushed meanwhile.

Commands in this lesson

4.5

Understand tracking branches

A local branch can track a remote branch. Then git status tells you if you are ahead or behind.

git branch -vv
* feature/web-network 3f2a9c1 [origin/feature/web-network: ahead 2] Add NSG rules
git status
Your branch is ahead of 'origin/feature/web-network' by 2 commits.
git branch -u origin/main main

Commands in this lesson

All commands in this chapter