Skip to main content

Git Teamwork

  • So far, we've learned how to work on Git as a single user. Git offers a suite of collaboration tools to make working with others on a project easier.

Imagine that you're a science teacher, developing some quizzes with Sally, another teacher in the school. You are using Git to manage the project.
In order to collaborate, you and Sally need:
A complete replica of the project on your own computers
A way to keep track of and review each other's work
Access to a definitive project version

You can accomplish all of this by using remotes. A remote is a shared Git repository that allows multiple collaborators to work on the same Git project from different locations. Collaborators work on the project independently, and merge changes together when they are ready to do so.


  • Sally has created the remote repository, science-quizzes in the directory curriculum, which teachers on the school's shared network have access to. In order to get your own replica of science-quizzes, you'll need to clone it with:
    git clone remote_location clone_name

In this command:

remote_location tells Git where to go to find the remote. This could be a web address, or a filepath, such as:
/Users/teachers/Documents/some-remote

clone_name is the name you give to the directory in which Git will clone the repository.


  • Exercise:

The Git remote Sally started is called:
science-quizzes

Enter the command to clone this remote. Name your clone:
my-quizzes

Notice the output:
cloning into 'my-quizzes'...

Git informs us that it's copying everything from science-quizzes into the my-quizzes directory.

my-quizzes is your local copy of the science-quizzes Git project. If you commit changes to the project here, Sally will not know about them.


  • git remote -v:

We have a clone of Sally's remote on our computer. One thing that Git does behind the scenes when you clone science-quizzes is give the remote address the name origin, so that you can refer to it more conveniently. In this case, Sally's remote is origin.

You can see a list of a Git project's remotes with the command: git remote -v

Exercise:

1.
Create couple of .txt file on /my-quizzes.

2.
Change directories into the my-quizzes directory, enter this command on the terminal:
cd my-quizzes

3.
Enter git remote -v to list the remotes.

Notice the output:
origin    /home/ccuser/workspace/curriculum/science-quizzes (fetch)
origin    /home/ccuser/workspace/curriculum/science-quizzes (push)

Git lists the name of the remote, origin, as well as its location.
Git automatically names this remote origin, because it refers to the remote repository of origin. However, it is possible to safely change its name.

The remote is listed twice: once for (fetch) and once for (push). We'll learn about these later in the lesson.


  • git fetch:


After you cloned science-quizzes, you had to run off to teach a class. Now that you're back at your computer, there's a problem: what if, while you were teaching, Sally changed the science-quizzes Git project in some way. If so, your clone will no longer be up-to-date.

An easy way to see if changes have been made to the remote and bring the changes down to your local copy is with:
git fetch


This command will not merge changes from the remote into your local repository. It brings those changes onto what's called a remote branch. Learn more about how this works below.

Exercise:

1.
Enter this command: cd my-quizzes
to go into the my-quizzes directory.
2.
Fetch any new changes Sally may have made to the remote: git fetch

Output:













  • Git merge:

Even though Sally's new commits have been fetched to your local copy of the Git project, those commits are on the origin/master branch. Your local master branch has not been updated yet, so you can't view or make changes to any of the work she has added.

In other lesson, Git Branching, we learned how to merge braches. Now we'll use the git merge command to integrate origin/master into your local master branch. The command:
git merge origin/master

will accomplish this for us.

Exercise:

1.
Enter this command:
cd my-quizzes
to go into the my-quizzes directory.

2.
You are on your local master branch. In your commit history, the commit message of the HEAD commit is something like:
Add first question to Physics quiz (marked with red arrow in the below image)

From the terminal, merge with origin/master, where Sally's most recent commits are:
git merge origin/master

Notice the output:

Updating a2ba090..bc87a1a
Fast-forward
 biology.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Git has performed a "fast-forward" merge, bringing your local master branch up to speed with Sally's most recent commit on the remote.

























3.
Print the commit history.

In the output, notice that the HEAD commit has changed. The commit message now reads:

 Add heading and comment to biology quiz (marked with green arrow in the above image)


  • Git workflow:
    Now that you've merged origin/master into your local master branch, you're ready to contribute some work of your own. The workflow for Git collaborations typically follows this order:


  1. Fetch and merge changes from the remote
  2. Create a branch to work on a new project feature
  3. Develop the feature on your branch and commit your work
  4. Fetch and merge from the remote again (in case new commits were made while you were working)
  5. Push your branch up to the remote for review

Steps 1 and 4 are a safeguard against merge conflicts, which occur when two branches contain file changes that cannot be merged with the git merge command. Step 5 involves git push, a command you will learn in the next exercise.

Exercise:

1.
Enter this command:
cd my-quizzes
to change directories into the my-quizzes directory.

2.
Enter the Git command:
git branch <branch_name>
to create a branch to develop questions for the biology quiz. Name the branch bio-questions.
Note: be careful to spell the name "bio-questions" exactly as it appears.

3.
Switch to your new branch with the command:
git checkout <branch_name>
replacing <branch_name> with the name of the new branch.

4.
On your branch, open the file you want to work with (for example, biology.txt) and make changes as follows:
Add a biology question to the file and some sample answers. For example:

  What is an animal that hunts and eats other animals called?
  a) herbivore
  b) prey
  c) ecosystem
  d) predator

5.
Add biology.txt to the staging area: git add biology.txt
6.
Commit the work to the repository with a commit message: git commit -m "test commit message"

Git Push:

Now it's time to share our work with Sally.

The command:
git push origin your_branch_name


will push your branch up to the remote, origin. From there, Sally can review your branch and merge your work into the master branch, making it part of the definitive project version.

Exercise:
1.
Enter this command
cd my-quizzes
to change directories into the my-quizzes directory.

2.
Push your branch up to the remote.

In the output, notice the line:
To /home/ccuser/workspace/curriculum/science-quizzes
 * [new branch]      bio-questions -> bio-questions
Git informs us that the branch bio-questions was pushed up to the remote. Sally can now review your new work and can merge it into the remote's master branch.












  • Generalization:

Congratulations, you now know enough to start collaborating on Git projects! Let's review.

A remote is a Git repository that lives outside your Git project folder. Remotes can live on the web, on a shared network or even in a separate folder on your local computer.
The Git Collaborative Workflow are steps that enable smooth project development when multiple collaborators are working on the same Git project.

We also learned the following commands:

  1. git clone: Creates a local copy of a remote.
  2. git remote -v: Lists a Git project's remotes.
  3. git fetch: Fetches work from the remote into the local copy.
  4. git merge origin/master: Merges origin/master into your local branch.
  5. git push origin <branch_name>: Pushes a local branch to the origin remote.

Git projects are usually managed on Github, a website that hosts Git projects for millions of users. With Github you can access your projects from anywhere in the world by using the basic workflow you learned here.











Comments

Popular posts from this blog

Git Branching- Learn how to manage multiple versions of a project with Branching.

Git Branch: Up to this point, you've worked in a single Git branch called  master . Git allows us to create  branches  to experiment with versions of a project. Imagine you want to create version of a story with a happy ending. You can create a new branch and make the happy ending changes to that branch only. It will have no effect on the  master branch until you're ready to merge the happy ending to the master branch. In this lesson, we'll be using Git branching to develop multiple versions of a resumé. You can use the command below to answer the question: “which branch am I on?” Command : git branch Check what branch you are currently on.  In the output, the * (asterisk) is showing you what branch you’re on. The diagram above illustrates branching. 1. The circles are commits, and together form the Git project's commit history. 2. New Branch  is a different  version  of the Git project. It contains commits...

Basic Git Workflow- how it's work

Basic Git Workflow: Git is a software that allows you to keep track of changes made to a project over time. Git works by recording the changes you make to a project, storing those changes, then allowing you to reference them as needed. Turning a directory into a project>>  command: git init The word  init  means  initialize . The command sets up all the tools Git needs to begin tracking changes made to the project. Output: would be like-  Initalized an empty g it repository in /home/ccuser/workspace/sorcerers-code/.git/ By the above output, we could say that  The Git project was created. Git workflow: A Git project can be thought of as having three parts: 1. A  Working Directory : where you'll be doing all the work: creating, editing, deleting and organizing files 2. A  Staging Area : where you'll list changes you make to the working directory 3. A  Repository : where Git permanently stores those chan...

How to Backtrack- Learn different ways to undo changes made to a Git project and when to use them.

Head commit : In Git, the commit you are currently on is known as the  HEAD commit. In many cases, the most recently made commit is the HEAD  commit. To see the  HEAD  commit, enter: git show HEAD The output of this command will display everything the  git log command  displays for the  HEAD  commit, plus all the file changes that were committed. For example:  Enter the command to show the  HEAD   commit. Notice the output. The ghost's most recently added line is in green text. Git Checkout: What if you decide to change the ghost's line in the working directory, but then decide you wanted to discard that change? You  could  rewrite the line how it was originally, but what if you forgot the exact wording? The command git checkout HEAD filename will restore the file in your working directory to look exactly as it did when you last made a c...