Git Remote
Clone a repository, make a change and push it
Description: Clone the repository server.git into a folder my-work, add a file todo.txt, commit it and push it to the remote.
Hint:
- A clone already has the remote
origin, so a plaingit pushis enough.
Solution
$ git clone server.git my-work
Cloning into 'my-work'…
done.
$ cd my-work
$ echo "buy milk" > todo.txt
$ git add todo.txt
$ git commit -m "Add todo list"
[main 9eaf666] Add todo list
1 file changed, 1 insertion(+)
create mode 100644 todo.txt
$ git push
To /home/robin/server.git
4f5618b..9eaf666 main -> main
How it works
- We used a local folder as the server. A GitHub URL works in the same way.
- The last line shows that
mainwas pushed to the remotemain.
Get new changes from the remote with fetch and pull
Description: A teammate pushed a new commit. First check the new commits without changing your branch, and then update your branch.
Hint:
git fetchdownloads without merging.git pulldownloads and merges.
Solution
$ git fetch
From /home/robin/server
0f2bd93..66c2a05 main -> origin/main
$ git log HEAD..origin/main –oneline
66c2a05 (origin/main, origin/HEAD) Mate adds a file
$ git pull
Updating 0f2bd93..66c2a05
Fast-forward
mate.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 mate.txt
$ ls
app.txt
mate.txt
How it works
- After the fetch, the commit is known as
origin/main, but our branch did not move. git pullthen merged it (a fast-forward), and the new file is in our folder.
Read Git Fetch and Pull.