Engineering Toolkit, lesson 5
Rebasing, remotes and pull requests
Share a branch, keep it current, and get it reviewed and merged.
"Branches, merges and conflicts": branches as labels, fast-forwards and merge commits.
Part 1. Remotes: the same history somewhere else
So far the repository lives only on your laptop. To work with anyone else, or to have a backup, you need a copy somewhere you can both reach. That other copy is a remote. It's usually on GitHub or GitLab, but any folder or server git can reach will do.
To keep this lesson runnable with no account, a folder plays the server. git init --bare makes a repository with no working files, which is what a server keeps.
$ git init --bare ~/server/textstats.git
Initialized empty Git repository in /home/ana/server/textstats.git/
$ git remote add origin ~/server/textstats.git
$ git push -u origin main
To /home/ana/server/textstats.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
$ git remote -v
origin /home/ana/server/textstats.git (fetch)
origin /home/ana/server/textstats.git (push)
$ git clone ~/server/textstats.git ~/sam-textstats
Cloning into '/home/ana/sam-textstats'...
done.With GitHub, you'd make an empty repository on the website and use the address it gives you, such as https://github.com/ana/textstats.git, in place of the folder. -u links your main to the server's, so later a plain git push or git pull knows where to go. The clone is Sam's copy, for the rest of the lesson.
Remote
Another copy of the repository that you push to and fetch from, known by a short name. origin is the usual name for the one you cloned from or first pushed to.
Sam adds a line to the README, commits and pushes. Ana's copy doesn't change by itself. She has to ask.
Your turn
Ana runs git fetch, and it reports a new commit on origin/main. What has changed in her project folder?
fetch only downloads commits and moves the remote-tracking labels, such as origin/main. Your branches and files don't change until you merge. git pull is a fetch followed by that merge, and here the merge is a fast-forward.$ git fetch
From /home/ana/server/textstats
a24cc41..3c1be36 main -> origin/main
$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
$ git pull
Updating a24cc41..3c1be36
Fast-forward
README.md | 2 ++
1 file changed, 2 insertions(+)Remote-tracking branch
A label like origin/main that records where a branch on the remote was the last time you fetched. Only fetch, pull and push move it.
Part 2. Rebasing: replay your commits on top
Keep going with Pro
You've done the first part. Pro opens the rest of this lesson, and every other one.
- Rebasing: replay your commits on top
- Rebasing safely
- Pull requests
Engineering Toolkit overviewSpotted a mistake? Tell us.