GitHub Edit Code
GitHub lets you edit files directly in the browser: open a file, click the pencil icon (or press . to open the full web editor at github.dev), change it, and commit it with a message. That is a real Git commit, made on the server instead of on your computer.
This is handy for quick fixes such as typos in documentation. But it has a consequence: the remote now has a commit your local repository doesn't know about.
What an edit on GitHub looks like
| On the website | In Git terms |
|---|---|
| Edit a file and click Commit changes | a new commit on the chosen branch of the remote |
| "Commit directly to the main branch" | the commit goes onto main |
| "Create a new branch for this commit and start a pull request" | a new branch is pushed, and a pull request is opened |
| Add file → Upload files | a commit adding those files |
Simulating a web edit
In the examples, a second clone called website plays the part of the GitHub web editor: anything committed and pushed from it is exactly what an edit in the browser produces.
git init -q --bare ~/github/shop.git
mkdir shop && cd shop && git init -q
printf '# Ada Shop\nWe sell tea.\n' > README.md && git add . && git commit -q -m "Add README"
git remote add origin ~/github/shop.git && git push -q -u origin main
# --- someone edits README.md on github.com ---
git clone -q ~/github/shop.git ~/website && cd ~/website
sed -i "s/Ada Shop/Ada's Shop/" README.md
git commit -q -am "Fix shop name in README"
git push -q
echo "web commit: $(git log --oneline -1)"
# --- back on your computer ---
cd ~/shop
git status -sb
git log --oneline --allweb commit: f459fdf Fix shop name in README
## main...origin/main
2764c82 Add README
Nothing changed locally yet: Git never contacts the server unless you ask. Your origin/main still shows the old position.
Seeing the remote change
git fetch downloads new commits and updates origin/main, without touching your files. Then status can tell you how far behind you are, and git log/git diff show what is new:
git init -q --bare ~/github/shop.git
mkdir shop && cd shop && git init -q
printf '# Ada Shop\nWe sell tea.\n' > README.md && git add . && git commit -q -m "Add README"
git remote add origin ~/github/shop.git && git push -q -u origin main
git clone -q ~/github/shop.git ~/website && cd ~/website
sed -i "s/Ada Shop/Ada's Shop/" README.md && git commit -q -am "Fix shop name in README" && git push -q
cd ~/shop
git fetch
git status -sb
echo "--- new on the server:"
git log --oneline main..origin/main
git diff main origin/mainFrom /home/learner/github/shop
2764c82..f459fdf main -> origin/main
## main...origin/main [behind 1]
--- new on the server:
f459fdf Fix shop name in README
diff --git a/README.md b/README.md
index e781083..ea21927 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
-# Ada Shop
+# Ada's Shop
We sell tea.
main..origin/main reads "commits on origin/main that are not on main". The next lesson brings them into your branch.
Editing on GitHub and locally at the same time
If you also committed locally before fetching, the two histories have diverged: each side has a commit the other lacks. Git will have to combine them.
git init -q --bare ~/github/shop.git
mkdir shop && cd shop && git init -q
printf '# Ada Shop\nWe sell tea.\n' > README.md && git add . && git commit -q -m "Add README"
git remote add origin ~/github/shop.git && git push -q -u origin main
git clone -q ~/github/shop.git ~/website && cd ~/website
sed -i "s/Ada Shop/Ada's Shop/" README.md && git commit -q -am "Fix shop name in README" && git push -q
cd ~/shop
echo "Open daily 9-17" > hours.txt && git add . && git commit -q -m "Add opening hours"
git fetch -q
git status | head -4
git log --oneline --graph --allOn branch main
Your branch and 'origin/main' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" if you want to integrate the remote branch with yours)
* 05de6cb Add opening hours
| * f459fdf Fix shop name in README
|/
* 2764c82 Add README
Tip: Make it a habit to
git pullbefore starting work, especially if you or others sometimes edit files on the website.
Test yourself
You fix a typo on github.com and commit it. What happens to your local repository?
- It updates automatically
- Nothing, until you fetch or pull
- It gets a merge conflict immediately
- The local file is deleted
What does git fetch change?
- Your working files
- Only remote-tracking branches such as origin/main (and downloaded objects)
- The remote repository
- Your local main branch