Reproducible Research 3 - Git: 2 devs and a central repository

Wojciech Hardy

link: https://hackmd.io/@WHardy/RR24-git2


How does it work? (quick recap)














Cheatsheet: Basic commands


Cheatsheet: Advanced commands


Read more on git reset

Other cheat sheet 1 (Atlassian)


Git commands: working with a central repository

git init --bare to create barebones for a central repository (with no initial commits)

git clone [repo_name] [clone_name] to create a linked copy of the repo_name repository.

git fetch grab information about new commits/branches from the central repository.

git pull to grab the (new) commits from the central repository to our repository.

git push to put our (newly created) commits in the central repository.

We're going to pretend we have two developers and a central repository.


Quick recap

  1. In our workspace, let's create a new folder RR_git2 and go inside

mkdir RR_git2
cd RR_git2

  1. Let's create a repository named RecapRepo and go inside

git init RecapRepo
cd RecapRepo
git status

  1. Change local user.name to "RecapRepoUser"

git config --local user.name "RecapRepoUser"


  1. Create three files: commit the first; only stage the second; do nothing to the third

echo "this file will be commited" > file1.txt
echo "this file will be staged" > file2.txt
echo "this fille will be in the working directory" > file3.txt

git add file1.txt
git commit -m "Added file1.txt"
git add file2.txt

git status


  1. In RR_git2 create a clone of the first repository; go inside

cd ..
git clone RecapRepo RecapCloned
cd RecapCloned

  1. View the contents; check the status

ls
git status

  1. View the config file in the .git folder.

(e.g. in notepad)


Exercise 5: simulating central repo with two locals

We're now going to pretend we have two people collaborating, using a central repository to manage the workflow.

  1. In your RR_git2 directory create a new, bare repository called "CentralRepo"

(Remember to use the appropriate option when initiating the repository!)

cd RR_git2
git init --bare CentralRepo


  1. In your RR_git2 directory create a clone of the CentralRepo, named Dev1

git clone CentralRepo Dev1


  1. In your RR_git2 directory create a clone of the CentralRepo, named Dev2

git clone CentralRepo Dev2


  1. Set the local user.name for Dev1 repository to "Developer_1"

cd Dev1
git config --local user.name "Developer_1"


  1. Set the local user.name for Dev2 repository to "Developer_2"

cd ../Dev2
git config --local user.name "Developer_2"

We'll be imitating this:


So far all repositories are empty. Let's imagine:

  1. Dev1 kick-starts the project by creating the branch with one commit and sending it upstream

echo "This will be the file with code" > code.R
git add .
git commit -m "Added the file with code"
git status
git push
git status

  1. Dev2 wants to get up-to-speed so grabs the changes

git pull


Exercise 6: fetching and updating before pushing

As you go, try to remember what are the states of the Dev1, the Dev2 and the Central Repositories at each step. (You can even draw them)

  1. Create a readme.txt file in your Dev1 repository.

cd ../Dev1
touch readme.txt


  1. Stage, commit and push the file.

git add .
git commit -m "Added readme.txt"
git push


  1. Go to the Dev2 repository.

cd ../Dev2


  1. Run git status

git status


  1. Fetch information about changes from the central repository. Run git status.

git fetch
git status


  1. Pull the information. Check folder contents.

git pull
ls


  1. Add a new line to readme.txt and create a readme.md file.

echo "second line of text" >> readme.txt
touch readme.md


  1. Stage, commit and push the file to the central repository.

git add .
git commit -m "Added readme.md and changed readme.txt"
git push


  1. In Dev1, create a text3.txt file with the line "A line added by Dev1". Stage, commit and push - what happens? Resolve the issue following the hints.

cd ../Dev1
echo "A line added by Dev1" > text3.txt
git add .
git commit -m "Added text3"
git push

Follow the instructions from Git to resolve merge conflicts.


Exercise 7: a merge conflict

In Exercise 6 we had a divergence, but without a conflict (commits differed, but there were no conflicting changes to files).

What if two devs make two different versions of a file?

  1. In Dev2, create a new file called text3.txt with the line "A line added by Dev2". Stage, commit and try to push. Read the hints, follow and go to step 2.

  1. There's a conflict because Dev1 committed a different text3.txt file earlier.

We're in a merging mode. We can edit the file (e.g. in Notepad) to figure out what should remain. Then stage it and commit it.

The merge becomes a new commit (try git log)


Assignment

While still in your Dev2 repository, run these three commands:
git status > git2_a.txt
git log >> git2_a.txt
git ls-files >> git2_a.txt

Send me the notepad file git2_a.txt (wojciechhardy@uw.edu.pl).


Stuck in VIM?

If you forgot about adding a message to your commit, you might have ended up in VIM. It's a free, text-editting software that sometimes feels like a trap.

Tl;dr: hit [ESC], then type *:q * and press Enter .
Repeat your commit with a helpful description.

You can also try adding the comment in VIM instead, and then exit with :wq instead, which should do the commit with the comment.

See more in this helpful Stackoverflow answer.


Useful links

Read more on the three trees with the git reset guideline

Cheat sheet 1 (Atlassian)

Git-scm in general

Atlassian in general

If you need more, just Google tutorials/blog posts/YouTube videos until you find one that makes it clear :) Lots to choose from!


Select a repo