layout | title | permalink |
---|---|---|
post |
GitHub Basics |
/github-basics/ |
GitHub is a Git repository hosting system that can be used as a remote Git repository.
In the Git Basics Tutorial , you learned to do version control in a local Git repository (i.e., repository in your hard drive). This allows you to go back to a previous version of your work. However, using only a local repository prevents you to collaborate with a teammate efficiently. For example, if you are working on a Java code Module1.java
and your teammate is working on Module2.java
, the typical way of consolidating your work is copying through a USB drive or sending your code as an e-mail.
In this tutorial, you will learn how to use GitHub as a remote Git repository in order for you to share your work with a teammate.
Prerequisite:
You are required to do the Git Basics Tutorial.
There are activities in this tutorial that require a teammate. Make sure that you identify another person who will be your teammate in this tutorial.
####Create a GitHub Account
-
Go to GitHub and click the
SIGN UP
button. -
Fill-up and submit the registration form.
-
Wait for a confirmation e-mail and follow the instructions in the e-mail to validate your GitHub registration.
####Explore your GitHub Account
-
Open a web browser and login to your GitHub account.
-
You will be redirected to your homepage. Your homepage lists the available repositories in your account. At this point you don't have any repository.
There are two ways to create a repository in GitHub:
- create a new repository from scratch
- fork an existing repository
You will try both approaches in this tutorial.
####Create a New Repository from Scratch
-
Click the
New repository
button. -
In the
Create a new repository
page enter the following values:Repository name myfirstrepo Type Public Initialize this repository with a README checked
-
Click the
Create repository
button. -
You will be redirected to your
myfirstrepo
repository. Currently, the repository contains a single file:README.md
.You may add new files and edit existing files using the GitHub interface. This tutorial will not cover the procedure to accomplish this.
-
Take note of the Git URL of your
myfirstrepo
. In GitHub, the Git URL follows the following format:https://github.com/<username>/<repository_name>.git
Example:
https://github.com/pong/myfirstrepo.git
IMPORTANT: The example above is the Git URL of another user. Make sure to take note the URL of your
myfirstrepo
.The Git URL is important to sync your local repository (i.e., the one in your hard drive) and your remote repository (e.g.,
myfirstrepo
).
-
Open a terminal window. Create the directory
gittemp
in the root directory. Go to the created directory.> mkdir gittemp > cd gittemp
-
Clone the git repository
https://github.com/<username>/myfirstrepo.git
and go to the created directory.> git clone https://github.com/<username>/myfirstrepo.git localrepo-one > cd localrepo-one
Example:
> git clone https://github.com/pong/myfirstrepo.git localrepo-one > cd localrepo-one
IMPORTANT: As mentioned earlier, the example above is the Git URL of another user. Make sure to use the URL of your
myfirstrepo
.Output:
Cloning into 'localrepo-one'... remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. Checking connectivity... done.
You have copied the contents of your remote repository
myfirstrepo
to a local repository (i.e., thelocalrepo-one
directory in your hard drive). In thegit clone
command, if you omit the parameterlocalrepo-one
, the directory that will be created will have the same name as the remote repository (i.e.,myfirstrepo
). In this tutorial, we intentionally specified the parameterlocalrepo-one
so that the name of the remote repository (myfirstrepo
) and local repository (localrepo-one
) are different.
-
Update
README.md
inlocalrepo-one
to the following:# myfirstrepo Hello GitHub!
-
Create a file
sample.txt
inlocalrepo-one
directory containing the following:This is just a sample text file.
-
Track and commit the changes made in the local repository.
> git add README.md sample.txt > git commit -m "updated README.md and created sample.txt"
Output:
[master 671b491] updated README.md and created sample.txt 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 sample.txt
At this point, the changes made (i.e., updated the
README.md
file and created thesample.txt
text file) are committed in your local repository. However, the local repository is not synced with the remote repositorymyfirstrepo
.
-
Go back to your web browser and refresh your page to confirm that
README.md
has not been updated andsample.txt
is not yet created in the remote repositorymyfirstrepo
. -
Verify that the Git URL of your remote repository is declared in your local repository.
> git remote -v
Output:
origin https://github.com/<username>/myfirstrepo.git (fetch) origin https://github.com/<username>/myfirstrepo.git (push)
As shown in the output above, your local repository is aware of the Git URL of your remote repository. The name
origin
is used to refer to the Git URL (e.g., instead of mentioning the very long Git URLhttps://github.com/<username>/myfirstrepo.git
, you may just refer to it asorigin
).Note that the declaration of having the name
origin
associated with the Git URLhttps://github.com/<username>/myfirstrepo.git
was automatically done when you did agit clone
earlier. The nameorigin
can be changed to something else but in this tutorial you will retain this name.
-
Sync the contents of the local repository to the remote repository.
> git push origin master
Output:
Username for 'https://github.com': <username> Password for 'https://<username>@github.com': Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (4/4), 375 bytes | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0)
The command
git push
is used because you did some modifications in the local repository (i.e., updatedREADME.md
and createdsample.txt
) and you want to push these changes to yourremote repository
.As discussed earlier,
origin
refers to the Git URLhttps://github.com/<username>/myfirstrepo.git
. The namemaster
refers to themaster
branch in your remote repositorymyfirstrepo
. A repository may have one or more branches. By default, a repository has only one branch namedmaster
. That is why your remote repositorymyfirstrepo
has amaster
branch even if you never explicitly created it. You may look at your GitHub page to verify that there is indeed a branch namedmaster
under the remote repositorymyfirstrepo
. This tutorial does not cover the creation of additional branches.In summary, the command
git push origin master
pushes the changes of your local repository to themaster
branch of the remote repository referred to by origin (which ishttps://github.com/<username>/myfirstrepo.git
).
-
Go back to your web browser and refresh your page to confirm that
README.md
has been updated andsample.txt
already exist in the remote repositorymyfirstrepo
.
####Fork an Existing Repository
As mentioned earlier, there are two ways to create a repository. Either create a new repository from scratch (which is the activity you did in the previous steps) or you fork an existing repository. In the task below, you will now create your second remote repository by forking an existing one.
-
Go back to your browser and go to the URL:
https://github.com/pong-pantola/mysecondrepo
Take note that you do not own this repository. It is owned by user
pong-pantola
. This can be verified through to the textpong-pantola/mysecondrepo
found on the webpage.
-
Click the
Fork
button.You now have your own copy of
mysecondrepo
. This can be verified through the text<username>/mysecondrepo
followed byforked from pong-pantola/mysecondrepo
.Forking allows you to make a copy of an existing repository and make it independent from the said existing repository. If you start making changes to your forked repository, it will affect only this repository.
-
Take note of the Git URL of your
mysecondrepo
.https://github.com/<username>/mysecondrepo.git
-
Open another terminal window. Go to the
gittemp
directory.> cd gittemp
-
Create the directory
localrepo-two
and go to the created directory.> mkdir localrepo-two > cd localrepo-two
Recall that you previously created the local repository
localrepo-one
by issuing the commandgit clone https://github.com/<username>/myfirstrepo.git localrepo-one
. You could have done the same thing withlocalrepo-two
. However, we want to discuss an alternative technique in syncing with a remote repository.
-
Make the
localrepo-two
a local repository.> git init
Output:
Initialized empty Git repository in /gittemp/localrepo-two/.git/
The
git init
command creates a hidden.git
subdirectory in the directorylocalrepo-two
. This makes thelocalrepo-two
a Git repository.
-
Create a file
anothersample.txt
inlocalrepo-two
directory containing the following:This is just a ANOTHER sample text file.
-
Track and commit the changes made in the local repository.
> git add anothersample.txt > git commit -m "created anothersample.txt"
Output:
[master (root-commit) e6f6dbb] created anothersample.txt 1 file changed, 1 insertion(+) create mode 100644 anothersample.txt
At this point, the changes made (i.e., created the
anothersample.txt
text file) are committed in your local repository. However, the local repository is not synced with the remote repositorymysecondrepo
.
-
Go back to your web browser and refresh your page to confirm that
anothersample.txt
is not yet created in the remote repositorymysecondrepo
. -
Verify if the Git URL of your remote repository is declared in your local repository
> git remote -v
Output:
The output of the command is blank. Recall that when you issued the same command in the
localrepo-one
repository, the output is not blank. It states that the following declaration:origin https://github.com/<username>/myfirstrepo.git (fetch) origin https://github.com/<username>/myfirstrepo.git (push)
Note that the
localrepo-one
repository was created when you issued thegit clone https://github.com/<username>/myfirstrepo.git localrepo-one
command earlier. Thegit clone
command did not only created thelocalrepo-one
repository but also did the necessary declaration to map the nameorigin
to the Git URLhttps://github.com/<username>/myfirstrepo.git
.However, in the case of
localrepo-two
, this repository was created by manually creating the directorylocalrepo-two
and issuing the commandgit init
inside it. Therefore,localrepo-two
is not even aware of the existence of the remote repositorymysecondrepo
nor its Git URLhttps://github.com/<username>/mysecondrepo.git
.You need to explicitly inform the local repository
localrepo-two
of the remote repositorymysecondrepo
.
-
Make the localrepo-two
localrepo-two
aware of the remote repositorymysecondrepo
.git remote add origin https://github.com/<username>/mysecondrepo.git
Example:
git remote add origin https://github.com/pong/mysecondrepo.git
IMPORTANT: The example above uses the Git URL of another user. Make sure to use the URL of your
mysecondrepo
.
-
Verify that the Git URL of your remote repository is already declared in your local repository.
> git remote -v
Output:
origin https://github.com/<username>/mysecondrepo.git (fetch) origin https://github.com/<username>/mysecondrepo.git (push)
-
Sync the contents of the local repository to the remote repository.
> git push origin master
Output:
Username for 'https://github.com': <username> Password for 'https://<username>@github.com': To https://github.com/<username>/mysecondrepo.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/<username>/mysecondrepo.git ' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Unlike the
git push
command you issued to sync the contents oflocalrepo-one
with the remote repositorymyfirstrepo
in an earlier task, thegit push
command above encountered an error.The intention of a
git push
command is to make the contents of the local repository the same as the remote repository. Ifgit push
detects that proceeding with the push will not result to a local and remote repositories that are synced, it will not proceed with the push. This is what happened with your attempt to push the changes inlocalrepo-two
to the remote repositorymysecondrepo
.Note that the remote repository
mysecondrepo
contains the fileREADME.md
whilelocalrepo-two
contains the fileanothersample.txt
. Agit push
command would have copiedanothersample.txt
tomysecondrepo
. This is summarized below:Repository Contents Before the Push Contents After the Push (if push will be allowed to proceed) localrepo-two
anothersample.txt
anothersample.txt
mysecondrepo
README.md
anothersample.txt
andREADME.md
Proceeding with the push will not result to synced
mysecondrepo
andlocalrepo-two
. This is the reason why thegit push
command resulted to an error.To solve this problem, a
git pull
command will be performed first.
-
Pull the contents of the remote repository to the local repository.
> git pull origin master
Output:
warning: no common commits remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/<username>/mysecondrepo * branch master -> FETCH_HEAD * [new branch] master -> origin/master Merge made by the 'recursive' strategy. README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 README.md
The
git pull
command checks files in the remote repository that are missing or different from the local repository. Those that are missing will be copied, while those that are different will be merged (or attempted to be merged) bygit pull
.The remote repository
mysecondrepo
has aREADME.md
file that is missing in the local repositorylocalrepo-two
. A pull will copy theREADME.md
file the local repository. This is summarized below:Repository Contents Before the Pull Contents After the Pull localrepo-two
anothersample.txt
anothersample.txt
andREADME.md
mysecondrepo
README.md
README.md
Note that a
git pull
command does not require that the local and remote repositories to be synced with each other. It only ensures that files in the remote repository that are missing or different from the local repository is copied/merged to the local repository.
-
Go back to your web browser and refresh your page to confirm that the remote repository
mysecondrepo
still contains only theREADME.md
file. At this point,anothersample.txt
should still not exist in the remote repository. -
Try to sync again the contents of the local repository to the remote repository.
> git push origin master
Output:
Username for 'https://github.com': <username> Password for 'https://<username>@github.com': Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 588 bytes | 0 bytes/s, done. Total 5 (delta 0), reused 0 (delta 0) To https://github.com/<username>/mysecondrepo.git 125720e..86cb740 master -> master
Repository Contents Before the Push Contents After the Push localrepo-two
anothersample.txt
andREADME.md
anothersample.txt
andREADME.md
mysecondrepo
README.md
anothersample.txt
andREADME.md
-
Go back to your web browser and refresh your page to confirm that aside from
README.md
, the remote repositorymysecondrepo
now containsanothersample.txt
.
####Sharing a Remote Repository
Syncing the contents of a local repository to a remote repository is an effective way of creating a back up of your source code. However, sharing a remote repository to teammates allow you to easily consolidate your team's work. In the next activity, you will work with another person. One of you will be Person A (the one who will share the repository) and the other one is Person B.
IMPORTANT: Each step is labeled as Person A, Person B, or Both to know who should be performing a particular step. Note that even if two steps are assigned to different persons, it does not mean that they can be performed in parallel. For example, if step X is for Person A and step Y is for Person B, Person B should wait for Person A to finish step X before Person B performs step Y.
-
Person A: On your web browser, create a new remote repository with the following specification:
Repository name mysharedrepo Type Public Initialize this repository with a README checked
-
Both: Open another terminal window. Go to the
gittemp
directory.> cd gittemp
-
Both: Clone the git repository
https://github.com/<username_of_Person_A>/mysharedrepo.git
and go to the created directory.> git clone https://github.com/<username_of_Person_A>/mysharedrepo.git localrepo-three > cd localrepo-three
Example:
> git clone https://github.com/usera/mysharedrepo.git localrepo-three > cd localrepo-three
IMPORTANT: The example above is the Git URL of another user. Make sure to use the URL of Person A's
mysharedrepo
. Even Person B should use the URL of Person A'smysharedrepo
.Output:
Cloning into 'localrepo-three'... remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. Checking connectivity... done.
-
Person A: Create a file
contribution-a.txt
inlocalrepo-three
directory containing the following:This is the contribution of Person A. This represents a source code written by Person A.
-
Person A: Track and commit the changes made in the local repository.
> git add contribution-a.txt > git commit -m "created contribution-a.txt"
Output:
[master 83d407a] created contribution-a.txt 1 file changed, 1 insertion(+) create mode 100644 contribution-a.txt
-
Person A: Sync the contents of the local repository to the remote repository.
> git push origin master
Output:
Username for 'https://github.com': <username_of_Person_A> Password for 'https://<username_of_Person_A>@github.com': Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 377 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/<username_of_Person_A>/mysharedrepo.git cb74f88..83d407a master -> master
-
Person B: Create a file
contribution-b.txt
inlocalrepo-three
directory containing the following:This is the contribution of Person B. This represents a source code written by Person B.
-
Person B: Track and commit the changes made in the local repository.
> git add contribution-b.txt > git commit -m "created contribution-b.txt"
Output:
[master d0d039d] created contribution-b.txt 1 file changed, 1 insertion(+) create mode 100644 contribution-b.txt
-
Person B: Sync the contents of the local repository to the remote repository.
> git push origin master
Output:
Username for 'https://github.com': <username_of_Person_B> Password for 'https://<username_of_Person_B>@github.com': remote: Permission to <username_of_Person_A>/mysharedrepo.git denied to <username_of_Person_B.> fatal: unable to access 'https://github.com/<username_of_Person_A>/mysharedrepo.git/': The requested URL returned error: 403
Unlike Person A, Person B encountered an
unable to access
error. Recall that both Person A and Person B cloned the remote repositorymysharedrepo
of Person A.When Person A issued the
git push
command to push its update to the remote repositorymysharedrepo
of Person A this does not cause any problem since Person A has access to its remote repository.However, when Person B issued the
git push
command to push its update to the remote repositorymysharedrepo
of Person A, an error is encountered because Person B is not given any authorization to push updates to Person A's remote repository.
-
Person A: Give permission to Person B to push updates to the remote repository
mysharedrepo
. Go to the web browser and make sure that you are in themysharedrepo
repository. -
Person A: Click the
Settings
link. Click theCollaborators
link. Confirm your password. -
Person A: In the text box, type the username of Person B and click the
Add Collaborator
button. -
Person B: Try again to sync the contents of the local repository to the remote repository.
> git push origin master
Output:
Username for 'https://github.com': <username_of_Person_B> Password for 'https://<username_of_Person_B>@github.com': To https://github.com/<username_of_Person_A>/mysharedrepo.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/<username_of_Person_A>/mysharedrepo.git ' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
The
unable to access
error does not anymore appear. This means that Person B is already given authorization to push updates to Person A's remote repository. However, the error message encountered in the previous activity appeared again. This is due to to agit push
will not sync the local repository of Person B and the remote repository of Person A. Note that the remote repository already hascontribution-a.txt
which is still missing in Person B's local repository. This is summarized below:Repository Contents Before Person B's Push Contents After Person B's Push (if push will be allowed to proceed) localrepo-three
of Person Acontribution-a.txt
andREADME.md
contribution-a.txt
andREADME.md
localrepo-three
of Person Bcontribution-b.txt
andREADME.md
contribution-b.txt
andREADME.md
mysharedrepo
of Person Acontribution-a.txt
andREADME.md
contribution-a.txt
,contribution-b.txt
, andREADME.md
Note that the push is not allowed because the local repository of Person B will not be synced with the remote repository of Person A.
This can easily be solved by Person B by doing a pull followed by a push.
-
Person B: Pull the contents of the remote repository to the local repository.
> git pull origin master
Output:
remote: Counting objects: 3, done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/<username_of_Person_A>/mysharedrepo * branch master -> FETCH_HEAD cb74f88..83d407a master -> origin/master Merge made by the 'recursive' strategy. contribution-a.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 contribution-a.txt
The repositories' contents after the pull are the following:
Repository Contents Before Person B's Pull Contents After Person B's Pull localrepo-three
of Person Acontribution-a.txt
andREADME.md
contribution-a.txt
andREADME.md
localrepo-three
of Person Bcontribution-b.txt
andREADME.md
contribution-a.txt
,contribution-b.txt
, andREADME.md
mysharedrepo
of Person Acontribution-a.txt
andREADME.md
contribution-a.txt
andREADME.md
-
Person B: Try again to sync the contents of the local repository to the remote repository.
> git push origin master
Output:
Username for 'https://github.com': <username_of_Person_B> Password for 'https://<username_of_Person_B>@github.com': Counting objects: 7, done. Delta compression using up to 4 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (5/5), 647 bytes | 0 bytes/s, done. Total 5 (delta 1), reused 0 (delta 0) To https://github.com/<username_of_Person_A>/mysharedrepo.git 83d407a..95b2e65 master -> master
The repositories' contents after the pull are the following:
Repository Contents Before Person B's Push Contents After Person B's Push localrepo-three
of Person Acontribution-a.txt
andREADME.md
contribution-a.txt
andREADME.md
localrepo-three
of Person Bcontribution-a.txt
,contribution-b.txt
, andREADME.md
contribution-a.txt
,contribution-b.txt
, andREADME.md
mysharedrepo
of Person Acontribution-a.txt
andREADME.md
contribution-a.txt
,contribution-b.txt
, andREADME.md
The local repository of Person B and the remote repository of Person A are now synced. However, Person A's local repository is not synced with the remote repository. This can easily be solved with another pull from Person A.
-
Person A: Pull the contents of the remote repository to the local repository.
> git pull origin master
Output:
remote: Counting objects: 5, done. remote: Compressing objects: 100% (4/4), done. remote: Total 5 (delta 1), reused 5 (delta 1), pack-reused 0 Unpacking objects: 100% (5/5), done. From https://github.com/<username_of_Person_A>/mysharedrepo * branch master -> FETCH_HEAD 83d407a..95b2e65 master -> origin/master Updating 83d407a..95b2e65 Fast-forward contribution-b.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 contribution-b.txt
The repositories' contents after the pull are the following:
Repository Contents Before Person A's Pull Contents After Person A's Pull localrepo-three
of Person Acontribution-a.txt
andREADME.md
contribution-a.txt
,contribution-b.txt
, andREADME.md
localrepo-three
of Person Bcontribution-a.txt
,contribution-b.txt
, andREADME.md
contribution-a.txt
,contribution-b.txt
, andREADME.md
mysharedrepo
of Person Acontribution-a.txt
andREADME.md
contribution-a.txt
,contribution-b.txt
, andREADME.md
The two local repositories and the remote repository are now synced.
####Merge Changes Made on the same file
The previous activity demonstrated how the contributions made by teammates can be consolidated together. In the next activity, both Person A and Person B will update the same file contribution-a.txt
. You will see the problem caused by such an update and the steps to resolve the problem.
-
Person A: Update the file
contribution-a.txt
inlocalrepo-three
directory so that it contains the following:This is the contribution of Person A. This represents a source code written by Person A. This is an UPDATE made by Person A.
-
Person A: Track and commit the changes made in the local repository.
> git add contribution-a.txt > git commit -m "updated contribution-a.txt"
Output:
[master d972fef] updated contribution-a.txt 1 file changed, 3 insertions(+), 1 deletion(-)
-
Person A: Sync the contents of the local repository to the remote repository.
> git push origin master
Output:
Username for 'https://github.com': <username_of_Person_A> Password for 'https://<username_of_Person_A>@github.com': Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 391 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) To https://github.com/<username_of_Person_A>/mysharedrepo.git 95b2e65..d972fef master -> master
-
Person B: Update the file
contribution-a.txt
(notcontribution-b.txt
) inlocalrepo-three
directory so that it contains the following:This is the contribution of Person A. This represents a source code written by Person A. This is an UPDATE made by Person B.
-
Person B: Track and commit the changes made in the local repository.
> git add contribution-a.txt > git commit -m "updated contribution-a.txt by Person B"
Output:
[master 8927d94] updated contribution-a.txt by Person B 1 file changed, 3 insertions(+), 1 deletion(-)
-
Person B: Sync the contents of the local repository to the remote repository.
> git push origin master
Output:
Username for 'https://github.com': <username_of_Person_B> Password for 'https://<username_of_Person_B>@github.com': To https://github.com/<username_of_Person_A>/mysharedrepo.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/<username_of_Person_A>/mysharedrepo.git ' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Similar to the error encountered by Person B earlier, Person B needs to perform a pull first before doing a push. The only difference with the error encountered in this step is that the error is caused not by a missing file but due to the varying contents of
contribution-a.txt
in the remote repository and Person B's local repository.Person B's local repository's
contribution-a.txt
:This is the contribution of Person A. This represents a source code written by Person A. This is an UPDATE made by Person B.
Person A's remote repository's
contribution-a.txt
:This is the contribution of Person A. This represents a source code written by Person A. This is an UPDATE made by Person A.
As mentioned, Person B can attempt to do a pull to fix the problem.
-
Person B: Pull the contents of the remote repository to the local repository.
> git pull origin master
Output:
remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/<username_of_Person_A>/mysharedrepo * branch master -> FETCH_HEAD 95b2e65..d972fef master -> origin/master Auto-merging contribution-a.txt CONFLICT (content): Merge conflict in contribution-a.txt Automatic merge failed; fix conflicts and then commit the result.
The
git pull
command is able to pull the changes from the remote repository (i.e., the line added incontribution-a.txt
) and include it incontribution-a.txt
in the local repository of Person B. However, since Person B did its own changes on the file, Git failed to figure out how to properly merge/consolidate the two changes.The
contribution-a.txt
in the local repository of Person B now contains the following:This is the contribution of Person A. This represents a source code written by Person A. <<<<<<< HEAD This is an UPDATE made by Person B. ======= This is an UPDATE made by Person A. >>>>>>> d972fefa5dc88f258ded0d157977600e7503293f
The
git pull
command placed a marker on the changes that it failed to merge. The reason why it failed to merge isgit pull
does not know if your intention is to retain only one of the two lines or if you want to retain both. Git is letting you to manually merge these updates.You will retain both
This is an UPDATE made by Person B.
andThis is an UPDATE made by Person A.
. This represents both Person A and Person B placing updates on the same file.
-
Person B: Fix the file
contribution-a.txt
inlocalrepo-three
directory so that the markers are removed:This is the contribution of Person A. This represents a source code written by Person A. This is an UPDATE made by Person B. This is an UPDATE made by Person A.
Take note that
contribution-a.txt
in the remote repository still does not contain the updates made by Person B. This is the current content ofcontribution-a.txt
in the remote repository:This is the contribution of Person A. This represents a source code written by Person A. This is an UPDATE made by Person A.
Person B will commit the changes made in
contribution-a.txt
and push the updates to the remote repository.
-
Person B: Track and commit the changes made in the local repository.
> git add contribution-a.txt > git commit -m "merged updates on contribution-a.txt"
Output:
[master 72c9355] merged updates on contribution-a.txt
-
Person B: Sync the contents of the local repository to the remote repository.
> git push origin master
Output:
Username for 'https://github.com': <username_of_Person_B> Password for 'https://<username_of_Person_B>@github.com': Counting objects: 10, done. Delta compression using up to 4 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 709 bytes | 0 bytes/s, done. Total 6 (delta 2), reused 0 (delta 0) To https://github.com/<username_of_Person_A>/mysharedrepo.git d972fef..72c9355 master -> master
Note that before the push,
contribution-a.txt
of the local repository of Person B and the remote repository are different. The version in the remote repository does not contain the lineThis is an UPDATE made by Person B.
. After the push, git is able to successfully merge the updates made in the local repository of Person B to the remote repository.At this point, the local repository of Person B and remote repository are synced. However,
contribution-a.txt
of the local repository of Person A is not yet updated. It does not contain the lineThis is an UPDATE made by Person B.
. This can be resolved with another pull from Person A.
-
Person A: Pull the contents of the remote repository to the local repository.
> git pull origin master
Output:
remote: Counting objects: 6, done. remote: Compressing objects: 100% (4/4), done. remote: Total 6 (delta 2), reused 6 (delta 2), pack-reused 0 Unpacking objects: 100% (6/6), done. From https://github.com/<username_of_Person_A>/mysharedrepo * branch master -> FETCH_HEAD d972fef..72c9355 master -> origin/master Updating d972fef..72c9355 Fast-forward contribution-a.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
The two local repositories and the remote repository are now synced.
####End of Tutorial
Go back to the List of Tutorials.
####What's next?