Remotes
The setup script is located in the git.zip file
in the exercsises folder
.
Run ./remotes.sh
for setup!
Introduction
In this exercise we will work with a local remote. The setup script will print the location and also the needed clone command.
Cloning
Instructions
- Clone the
local_remote
intomycopy
withgit clone file:///$pathToLocalRemote/.git mycopy
- Switch into
mycopy
(cd mycopy
) - Run
git remote -v
Questions
- What is the remote called?
- Can the remote have another name?
Answers
- origin
- yes, origin is a convention
Setup a new tracking branch
Now we will create a new branch with content and push it to the remote.
Instructions
- Make sure you are in
mycopy
- Create a new branch
mybranch
and switch to itgit switch -c mybranch
- Create a new file with
touch newfile
- Stage and commit this file (
git add newfile
,git commit -m "mycopy commit"
) - Push the branch with
git push
- This fails - follow the instructions of git to create a tracking branch and push
Preparations for doing force pushes
To create a state where the remote was pushed to from a different source we need to trick a bit. We will create a second local clone of the remote and work there.
Instructions
- Go one directory up
cd ..
and clone the local_remote into mycopy2git clone file:///$pathToLocalRemote/.git mycopy2
- Change into "mycopy2" with
cd mycopy2
- Create a new branch
mybranch2
and switch to itgit switch -c mybranch2
- Create a new file with
touch newfile2
- Stage and commit this file (
git add newfile2
,git commit -m "mycopy2 commit"
) - Push it to the remote with
git push -u orgin mybranch2
- Run
git branch -a
to get a list of all local and remote branches - Switch into
mybranch
withgit switch mybranch
- Add a file
(
touch anotherfile && git add anotherfile && git commit -m "another commit"
) - Push this change (
git push
)
Now we have simulated someone else working on the same remote. We now
can switch back with cd ../mycopy
Force Push
Now we perform force pushes.
Instructions
- Make sure you are in
mycopy
- Check if you are on the branch
mybranch
, if not switch to it - Create a new file with
touch newfile3
- Stage and commit this file (
git add newfile3
,git commit -m "newfile3 commit"
) - Run
git push
- what happens? - Run
git push --force-with-lease
- what happens? (if you did run agit fetch
before this will behave differently) - Run
git push --force
- what happens?
Questions
- Run
git log
, is there a commit ofmycopy2
? - What could be a consequence of force pushing?
- What would you need to to do to incorporate the changes on the remote into you local copy?
Answers
- No.
- Annoying co-workers, loosing commits, loosing data
git pull
will either merge the remote or rebase your changes on it
Bonus: Finding a lost commit
When doing a force push, the old commits are not removed from the remote. But can we recover them?
Instructions
- Switch to the remote
cd ../local_remote/.git
git reflog
=> is empty, this is a barren repositorygit fsck --full
=> shows dangling commitsgit show commitHash
=> shows the lost commit
Dangling commits
Dangling objects (commits, trees, blobs) are without a connection to
the commit graph or the reflog - they are not reachable. Object which
are reachable from a dangling commit are not dangling but unreachable.
Dangling objects are removed after a configurable time automatically.
Check man git gc
for details.