9 Collaborating (git fetch & merge (or pull))

Key terms/commands:

  • git fetch: fetches the changes that is in your remote repository GitHub. This command download the changes into the .git/ folder so you can compare changes to your local files, but it does not actually modify your files (i.e., files outside of the .git/ folder).

  • git merge: implement the changes you have fetched into your local files.

  • git pull: git fetch + git merge. However, it is recommended that you fetch first, check that you want to implement the changes, and then merge.

9.1 Setup a fake collaborator, yourself in another directory :)

Clone the workdir repo into your local computer but a different folder as if you have a collaborator

Let’s pretend you are the collaborator, and you will make some changes and push it to GitHub. Here we go into the collab directory, make a new file foofoo.txt, then add & commit it.

## [master 07bbe41] Add foofoo.txt
##  1 file changed, 0 insertions(+), 0 deletions(-)
##  create mode 100644 foofoo.txt

Push this change to GitHub

## Counting objects: 2, done.
## Delta compression using up to 8 threads.
## Compressing objects: 100% (2/2), done.
## Writing objects: 100% (2/2), 245 bytes | 245.00 KiB/s, done.
## Total 2 (delta 1), reused 0 (delta 0)
## remote: Resolving deltas: 100% (1/1), completed with 1 local object.
## To github.com:your_username/workdir.git
##    f9bed0e..7e740d1  master -> master

Lets check what is in the current folder

## foofoo.txt
## foo.txt
## ignore_this.txt

Check what is on the workdir repository on GitHub. It should contain foofoo.txt

But what about the original workdir folder?

## foo.txt
## ignore_this.txt

foofoo.txt should be missing. Now let’s say you want to check and incorporate the changes your collaborator made in your original folder.

9.2 Implement the changes your fake collaborator made

fetch the changes your fake collaborator pushed to GitHub onto your local computer.

Here is an example output from running git fetch, exact details will vary.

## remote: Enumerating objects: 3, done.
## remote: Counting objects: 100% (3/3), done.
## remote: Compressing objects: 100% (1/1), done.
## remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
## Unpacking objects: 100% (2/2), done.
## From github.com:your_username/workdir
##    7e740d1..5e3ff23  master     -> origin/master

NOTE that this does not change the files in your directory. But now you can use check to see what your collaborator has changed

## diff --git a/foofoo.txt b/foofoo.txt
## new file mode 100644
## index 0000000..e69de29

If you decided these are changes you want in your local directory (the original workdir)

## Updating 7e740d1..5e3ff23
## Fast-forward
##  foofoo.txt | 0
##  1 file changed, 0 insertions(+), 0 deletions(-)
##  create mode 100644 foofoo.txt

FAQ

  • What if git merge returns message “Already up to date.”
    • git merge implement the changes that were fetched by git fetch, so fetch first.
  • Will git fetch change my stuff?
    • Fetching does not change your local file until your pull.

BONUS - What is my remote?

What is your remote?

Adding a different remote

Changing the URL of the remote (e.g., you change the remote repository)

9.3 Really collaborating on GitHub



1

  • Using pull request (advance), useful for collaborating with anyone, including strangers!
    • fork a repository you want to make changes
    • git clone it to your local computer
    • Make changes, git add, git commit and git push it to your forked remote repo
    • Make a Pull Request in the original repository so the owner can review and decide whether they want to incorporate the changes you made.
  • Adding collaborators in your GitHub repository
    • Click Settings
    • Click Collaborators and add them (they need to be on GitHub)

You have now fetch and merge changes from different people (or yourself on a different computer/folder)!


  1. Icons made by Smashicons, Dave Gandy, Iconnice from Flaticon