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
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.
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 clone science-quizzes/ my-quizzes
Cloning into 'my-quizzes'...
done.3.git remote -vWe 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
3.1.Using
the file navigator, examine the contents of the cloned Git project.
There are a few quiz files here, which we will be working with during
this lesson.
Open a file of your choice in the code editor.
3.2.Change directories into the my-quizzes directory, enter this command on the terminal:
Open a file of your choice in the code editor.
3.2.Change directories into the my-quizzes directory, enter this command on the terminal:
cd my-quizzes
3.3.list the remotes.
$ git remote -v
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)
.
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.4.1.go into the my-quizzes directory.
$ cd my-quizzes
4.2.Fetch any new changes Sally may have made to the remote.
$ git fetch
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
From /home/ccuser/workspace/curriculum-a/science-quizzes
* [new branch] master -> origin/master 5.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 Lesson III, 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. 5.1.go into the my-quizzes directory
cd my-quizzes
You are on your local
master
branch. In your commit history, the commit message of the HEAD
commit is:
Add first question to Physics quiz
From the terminal, merge with origin/master
, where Sally's most recent commits are. Notice the output:
$ git merge origin/master
Updating 2fd7d9b..3a29454
Fast-forward
biology.txt | 4 ++++
1 file changed, 4 insertions(+)
create mode 100644 biology.txt 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
$ git log
commit 3a294546f4a55f02bf37233ef8988d8b9dd7ce59
Author: danasselin <johndoe@example.com>
Date: Tue Nov 3 12:33:23 2015 -0500
Add heading and comment to biology quiz
commit 6aa7704a31d05541141fbb529abf946bd2fd416b
Author: danasselin <johndoe@example.com>
Date: Thu Oct 29 17:04:04 2015 -0400
Add biology quiz
commit 2fd7d9b248e0b4a3b531b9af3bb61916d42ad45f
Author: danasselin <johndoe@example.com>
Date: Thu Oct 29 15:42:55 2015 -0400
Add first question to physics quiz
commit 2c4e484e0f5bda111a164e6580f4a4a603cea48f
Author: danasselin <johndoe@example.com>
Date: Thu Oct 29 15:42:36 2015 -0400
Add physics quiz
commit 65be0d3f24fb34363bdac6949325f35ad3cdd98a
Author: danasselin <johndoe@example.com>
Date: Thu Oct 29 15:42:21 2015 -0400
Add chemistry quiz
commit 9bfc082a0e64efe5c0a2e4298e0b4f127d0a9b96Git has performed a "fast-forward" merge, bringing your local master
branch up to speed with Sally's most recent commit on the remote. 6.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:- Fetch and merge changes from the remote
- Create a branch to work on a new project feature
- Develop the feature on your branch and commit your work
- Fetch and merge from the remote again (in case new commits were made while you were working)
- Push your branch up to the remote for review
git merge
command. Step 5 involves git push.
6.1.change directories into the my-quizzes directory.
6.2.Create a branch to develop questions for the biology quiz. Name the branch
bio-questions
.
$ git branch bio-questions
6.3.Switch to your new branch
Add a biology question to the file and some sample answers.
6.5.Add biology.txt to the staging area.
$ git add biology.txt
6.6.Commit the work to the repository with a commit message.
6.3.Switch to your new branch
$ git checkout bio-questions
Switched to branch 'bio-questions'6.4.On your branch, open biology.txt in the code editor. Add a biology question to the file and some sample answers.
6.5.Add biology.txt to the staging area.
$ git add biology.txt
6.6.Commit the work to the repository with a commit message.
$ git commit -m "First Commit"
[bio-questions fcc7550] First Commit
1 file changed, 5 insertions(+)
7.git push
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.7.1.change directories into the my-quizzes directory.
$ cd my-quizzes/
7.2.Push your branch up to the remote.
$ git push origin bio-questions
Counting objects: 5, done.
Delta compression using up to 3 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 388 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To /home/ccuser/workspace/curriculum-a/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. 8.generalizations
- 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.
git clone
: Creates a local copy of a remote.git remote -v
: Lists a Git project's remotes.git fetch
: Fetches work from the remote into the local copy.git merge origin/master
: Mergesorigin/master
into your local branch.git push origin <branch_name>
: Pushes a local branch to theorigin
remote.
No comments :
Post a Comment