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

  1. Clone the local_remote into mycopy with git clone file:///$pathToLocalRemote/.git mycopy
  2. Switch into mycopy (cd mycopy)
  3. Run git remote -v

Questions

Answers

Setup a new tracking branch

Now we will create a new branch with content and push it to the remote.

Instructions

  1. Make sure you are in mycopy
  2. Create a new branch mybranch and switch to it git switch -c mybranch
  3. Create a new file with touch newfile
  4. Stage and commit this file (git add newfile, git commit -m "mycopy commit")
  5. Push the branch with git push
  6. 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

  1. Go one directory up cd .. and clone the local_remote into mycopy2 git clone file:///$pathToLocalRemote/.git mycopy2
  2. Change into "mycopy2" with cd mycopy2
  3. Create a new branch mybranch2 and switch to it git switch -c mybranch2
  4. Create a new file with touch newfile2
  5. Stage and commit this file (git add newfile2, git commit -m "mycopy2 commit")
  6. Push it to the remote with git push -u orgin mybranch2
  7. Run git branch -a to get a list of all local and remote branches
  8. Switch into mybranch with git switch mybranch
  9. Add a file (touch anotherfile && git add anotherfile && git commit -m "another commit")
  10. 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

  1. Make sure you are in mycopy
  2. Check if you are on the branch mybranch, if not switch to it
  3. Create a new file with touch newfile3
  4. Stage and commit this file (git add newfile3, git commit -m "newfile3 commit")
  5. Run git push - what happens?
  6. Run git push --force-with-lease - what happens? (if you did run a git fetch before this will behave differently)
  7. Run git push --force - what happens?

Questions

Answers

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

  1. Switch to the remote cd ../local_remote/.git
  2. git reflog => is empty, this is a barren repository
  3. git fsck --full => shows dangling commits
  4. git 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.