Editing Code Online
A shared online document can be edited from any computer with a browser, even one that isn't yours. Fix a typo on your phone, and the change is there when you open the document on your laptop.
GitHub, GitLab and Bitbucket let you do the same with a repository: edit a file in the browser, write a commit message, and the website makes a real commit on the server, exactly as if you had pushed it. That is handy for small fixes, such as a typo in the README, when you aren't at your own computer. The only thing to remember is that your computer doesn't know about that commit until you fetch or pull it.
In this lesson, you will edit a file on the website, bring the change to your computer, create and protect branches on the website, and see what happens when you push to a protected branch.
Edit a File on the Website
Open the file in the repository, then:
1. Click the pencil icon (Edit this file).
2. Make the change in the editor.
3. Click Commit changes...
4. Write a commit message.
5. Choose "Commit directly to the main branch", or
"Create a new branch for this commit and start a pull request".
6. Click Commit changes.1. Click Edit, then Edit single file.
2. Make the change in the editor.
3. Write a commit message under the editor.
4. Choose the target branch (or a new branch, with a merge request).
5. Click Commit changes.1. Click Edit (top right of the file).
2. Make the change in the editor.
3. Click Commit.
4. Write a commit message, and choose whether to create
a pull request for the change.
5. Click Commit.For bigger changes over several files, the platforms have a full editor in the browser: on GitHub, press . on a repository page to open github.dev, and GitLab has the Web IDE. They commit in the same way.
Bring the Change to Your Computer
The commit is on the server now, made with your account's name and email. In this lesson, as in the previous ones, a bare repository in a folder stands in for the platform, and a separate clone called web-editor plays the website's editor: it makes the commit and pushes it, which is what the website does for you.
git init --bare --quiet fee-tracker.git
mkdir navika
cd navika
git init --quiet fee-tracker
cd fee-tracker
printf "# Fee Tracker\n\nTraks school fee payments.\n" > README.md
git add README.md
git commit --quiet -m "Add the README"
git remote add origin ../../fee-tracker.git
git push --quiet -u origin main
cd ../..
git clone --quiet fee-tracker.git web-editor
cd web-editor
sed -i.bak "s/Traks/Tracks/" README.md
rm README.md.bak
git commit --quiet -am "Fix a typo in the README"
git push --quiet
echo "Edited on the website"Edited on the websiteBack on her computer, Navika's repository doesn't know about the fix yet. A pull brings it in:
cd ../navika/fee-tracker
git pullFrom ../../fee-tracker
8344ad5..f5c2da4 main -> origin/main
Updating 8344ad5..f5c2da4
Fast-forward
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)cat README.md
git log --oneline# Fee Tracker
Tracks school fee payments.
f5c2da4 (HEAD -> main, origin/main, origin/HEAD) Fix a typo in the README
8344ad5 Add the READMEIf you did commit locally before pulling, nothing is lost: git pull --rebase puts your commits on top of the website's, as in "Git Pull".
Branches on the Website
The website can create, compare and delete branches too:
Create: open the branch menu (shows "main"), type a new name,
click "Create branch <name> from main".
Compare: Pull requests > New pull request, choose two branches.
Delete: the Branches page (repository home > branches), trash icon.Create: Code > Branches > New branch.
Compare: Code > Compare revisions, choose two branches.
Delete: Code > Branches, the delete button next to the branch.Create: Branches (left menu) > Create branch.
Compare: the Branches page, or Create pull request to see the diff.
Delete: the Branches page, ... menu > Delete.A branch created on the website appears on your computer after a fetch, like any branch a teammate pushed. Here, the web-editor clone creates docs/install and pushes it, as the website would:
cd ../../web-editor
git switch --quiet -c docs/install
echo "Install Java 21, then run the app." >> README.md
git commit --quiet -am "Add install steps"
git push --quiet -u origin docs/install
cd ../navika/fee-tracker
git fetchFrom ../../fee-tracker
* [new branch] docs/install -> origin/docs/installgit switch docs/install would now give Navika a local copy of the branch, as in "Pushing and Pulling Branches".
Protect Important Branches
Teams don't want anyone, or any accident, to push straight to main or force-push over it. A protected branch has rules that the server enforces, such as: changes only through pull requests, at least one approving review, passing automated checks, and no force pushes or deletion.
Settings > Branches (or Rules > Rulesets) > Add rule.
Branch name pattern: main
Check: Require a pull request before merging,
Require approvals, Require status checks to pass.
Force pushes and deletion are blocked by default.Settings > Repository > Protected branches.
Branch: main
Allowed to merge: Maintainers
Allowed to push and merge: No one
Allowed to force push: offRepository settings > Branch restrictions > Add a branch restriction.
Branch: main
Write access: nobody (so changes come through pull requests)
Merge via pull request: the users or groups allowed to merge
Prevent rewriting history and deleting the branch.A push that breaks a rule is refused by the server. Each platform words its message differently. To see the shape of such a refusal, the stand-in server gets a small check that refuses every push to main. It plays the part of the platform's rule here. Server-side checks like this one are called hooks, and have their own lessons later:
printf '#!/bin/sh\nwhile read old new ref; do\n if [ "$ref" = "refs/heads/main" ]; then\n echo "main is protected: open a pull request instead"\n exit 1\n fi\ndone\n' > ../../fee-tracker.git/hooks/pre-receive
chmod +x ../../fee-tracker.git/hooks/pre-receive
git merge --quiet origin/main
echo "Built by Navika." >> README.md
git commit --quiet -am "Credit the author"
git pushremote: main is protected: open a pull request instead
To ../../fee-tracker.git
! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to '../../fee-tracker.git'| Line | Meaning |
|---|---|
remote: main is protected... | A message from the server. On a real platform, it names the rule you broke. |
! [remote rejected] main -> main (pre-receive hook declined) | The server refused the update. On GitHub, GitLab or Bitbucket, the reason in brackets names their protection rule instead. |
Nothing changed on the server. The fix is to push a branch and open a pull request, the topic of the next lesson.
Common Mistakes
Editing on the website, then working locally without pulling. Your next push is rejected. Pull first.
Committing big changes in the browser. The web editor has no build, no tests and no IDE help. Use it for small fixes, and your own computer for real work.
Committing straight to main on the website when the team uses pull requests. Choose "Create a new branch" in the commit dialog.
Forgetting that a website commit uses your account email. Platforms may hide it behind a no-reply address. Check your account's email settings if commits must show a work address.
Deleting a branch on the website that a teammate still uses. Their next push recreates it, or their pull fails. Check that it is merged first.
Under the Hood
The web editor doesn't have a working tree. When you commit, the platform builds the new snapshot on the server directly: it creates a blob for the changed file, a new tree, and a commit whose parent is the branch's current commit, then moves the branch, the same objects a commit on your computer creates. That is why the commit looks exactly like any other in git log, and why the platform refuses your edit if someone pushed in the meantime, asking you to reload: just like a push, the update must be a fast-forward.
Protected-branch rules are checked at the same moment for every push, from any source, which is why they apply to the website's editor too.
Interview Corner
What happens when you edit a file on GitHub's website?
GitHub creates a real commit on the server, on the branch you choose, with your account as the author. Local clones see it only after git fetch or git pull.
What is a protected branch?
A branch with rules enforced by the hosting platform, for example: no direct pushes, changes only through pull requests with approvals and passing checks, and no force pushes or deletion. Pushes that break a rule are rejected by the server.
You edited the README on GitHub and also committed locally. How do you push?
Pull first, preferably with git pull --rebase, so your local commit is replayed on top of the website's commit, then push.
What does "[remote rejected] ... (pre-receive hook declined)" mean?
The server ran a check before accepting the push, and the check refused it. Hosting platforms use such checks for protected branches, required reviews or secret scanning. The remote: lines above it explain the reason.
Practice
Challenge: work on the website's branch. In Navika's clone, switch to the branch docs/install that was created on the website, and print the last line of README.md and the first two lines of git status.
What does Git print? Before you open the solution, decide which line ends README.md on that branch, and what the status says about its upstream.
Show solution
git switch --quiet docs/install
tail -1 README.md
git status | head -2Install Java 21, then run the app.
On branch docs/install
Your branch is up to date with 'origin/docs/install'.The last line is the install step written on the website. git switch created a local docs/install from origin/docs/install and set it as the upstream, so the status says it is up to date with it. Navika can now continue the website's work on her computer and push it back.
Checkpoint. In a repository of your own on GitHub, GitLab or Bitbucket, fix a typo in the README on the website, then pull it locally. Then protect main, try to push to it directly, and read the message the platform gives you.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git pull | Bring website edits to your computer |
git pull --rebase | Same, when you also committed locally |
git fetch | See branches created on the website |
git switch <branch> | Work on a website-created branch locally |
git push -u origin <branch> | Push a branch instead of a protected main |
Was this tutorial helpful?