Wednesday, 30 March 2016

Learn Git



     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.

***************************************************************************


git init                 creates a new Git repository

git status          inspects the contents of the working directory and staging area

git add            adds files from the working directory to the staging area

git diff          shows the difference between the working directory and the staging area

git commit   permanently stores file changes from the staging area in the repository

git log                 shows a list of all previous commits



git checkout HEAD filename    Discards changes in the working directory.   

git reset HEAD filename   Unstages file changes in the staging area.

git reset SHA  Can be used to reset to a previous commit in your commit history



git branch                                Lists all a Git project's branches.

git branch branch_name       Creates a new branch.

git checkout branch_name    Used to switch from one branch to another.

git merge branch_name     Used to join file changes from one branch to another
.
git branch -d branch_name     Deletes the branch specified.



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                            Merges origin/master into your local branch.

git push origin <branch_name>      Pushes a local branch to the origin remote.



***************************************************************************
 scene-1.txt

Harry Programmer and the Sorcers's
Code: Scene 1
------------------------------------------------------------------------------------------------------------------

1)  $  git init

The command sets up all the tools Git needs to begin tracking changes made to the project.




2)

 i)   Working Directory  :     you'll be doing all the work: creating, editing, deleting
ii)   A Staging Area       :     changes you make to the working directory
iii)  A Repository           :    permanently stores those changes as different versions of the project

-----------------------------------------------------------------------------------------------------------------



3) $  git status                 :

                                                                                                         
i)

On branch master                                                                                                
                                                                                                               
Initial commit                                                                                                  
                                                                                                               
Untracked files:                                                                                                
  (use "git add <file>..." to include in what will be committed)                                                
                                                                                                               
        scene-1.txt                                                                                            
                                                                                                               
nothing added to commit but untracked files present (use "git add" to track)

-----------------------------------------------------------------------------------------------------------


ii) $ git add scene-1.txt  
                                                                                         
git status    
                                                                                             
On branch master                                                                                                
                                                                                                               
Initial commit                                                                                                  
                                                                                                               
Changes to be committed:                                                                                        
  (use "git rm --cached <file>..." to unstage)                                                                  
                                                                                                               
        new file:   scene-1.txt

iii)  $  git rm --cached scene-1.txt                                                                                  
 
      rm 'scene-1.txt'




4)   add a file to the staging area.

  $  git add scene-1.txt                                                                                          
   $ git status                                                                                                    

 On branch master                                                                                                
                                                                                                               
Initial commit                                                                                                  
                                                                                                               
Changes to be committed:                                                                                        
  (use "git rm --cached <file>..." to unstage)                                                                  
                                                                                                               
        new file:   scene-1.txt



5)   difference between the working directory and the staging area.

-----------------------------------------------------------------------------------------------------------------
 scene-1.txt

Harry Programmer and the Sorcers's
Code: Scene 1

Dumblediff: I should've known you would be here, Professor McGonagit.

------------------------------------------------------------------------------------------------------------------




   $ git diff filename


O/p:

$ git diff scene-1.txt                              
diff --git a/scene-1.txt b/scene-1.txt              
index ac974bd..68158c9 100644                      
--- a/scene-1.txt                                  
+++ b/scene-1.txt                                  
@@ -1,2 +1,4 @@                                    
 Harry Programmer and the Sorcers's                
-Code: Scene 1                                      
\ No newline at end of file                        
+Code: Scene 1                                      
+                                                  
+Dumblediff: I should've known you would be here, Profes
\ No newline at end of file                        
(END)


--------------------------------------------
"Harry Programmer and the Sorcerer's Code: Scene 1" is in the staging area, as indicated in white.

Changes to the file are marked with a + and are indicated in green.

IMPORTANT: press q on your keyboard to exit diff mode.

--------------------------------------------


Add the changes to the staging area in Git.


$ git add scene-1.txt

-----------------------------------------------------------------------------------------------

6)   $  git commit


A commit permanently stores changes from the staging area inside the repository.



git commit is the command we'll do next. However, one more bit of code is needed for a commit:
the option -m followed by a message. Here's an example


git commit -m "Complete first line of dialogue"




Standard Conventions for Commit Messages:

Must be in quotation marks

Written in the present tense

Should be brief (50 characters or less) when using -m

-----------------------------------------------------------------------------------------------------------

7)  $ git log


In the output, notice:

A 40-character code, called a SHA, that uniquely identifies the commit. This appears in orange text.
The commit author (you!)
The date and time of the commit
The commit message





git log                                                                                                      
commit a31bce3c8de6a6c9a0b5ce13ffce8332cb5a365c                                                                
Author: codecademy <ccuser@codecademy.com>                                                                      
Date:   Wed Mar 30 04:22:33 2016 -0400                                                                          
                                                                                                               
    Complete first line of dialogue




***************************************************************************





Git offers a few eraser-like features that allow us to undo mistakes during project creation.


Ex:

---------------------------------------------------------------------------------------------
->  scene-5.txt


Hamlet: Act 1, Scene 5

Enter Ghost and Hamlet

Hamlet:
Where wilt thou lead me? speak; I'll go no further

Ghost:
Mark me.

Hamlet:
I will.


-----------------------------------------------------------------------------------

-> You are in a Git project titled hamlet-prince-of-denmark. In the code editor, you'll be working on scene-5.txt. Here, Hamlet encounters the ghost of his father. Add this text to the file:



Ghost: 
My hour is almost come,
When I to sulphurous and tormenting flames
Must render up myself.




-> From the terminal, add scene-5.txt to the staging area.



$ git add scene-5.txt    


->Commit the changes to the repository with a good commit message.                      

$ git commit -m "This is final"


--------------------------------------------------------------------------------------------------------------
8)  git show HEAD 

In Git, the commit you are currently on is known as theHEAD commit. In many cases, the most recently made commit is the HEAD commit.
To see the HEAD commit, enter:



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.



git show HEAD  
                                                                                          
commit 17af1d91fabfe6bf39715abb06662d80e7093413                                                              
Author: codecademy <ccuser@codecademy.com>                                                                    
Date:   Wed Mar 30 04:36:52 2016 -0400                                                                        
                                                                                                             
    This is final                                                                                            
                                                                                                             
diff --git a/scene-5.txt b/scene-5.txt                                                                        
index b12dd97..c3dc886 100644                                                                                
--- a/scene-5.txt                                                                                            
+++ b/scene-5.txt                                                                                            
@@ -12,3 +12,8 @@ Hamlet:                                                                                    
 I will.                                                                                                      
                                                                                                             
                                                                                                             
+                                                                                                            
+Ghost:                                                                                                      
+My hour is almost come,                                                                                      
+When I to sulphurous and tormenting flames                                                                  
+Must render up myself.                                                                                      
\ No newline at end of file

---------------------------------------------------------------------------------------------------------------

9)

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 commit.

Change the ghost's words in some way. Here's a fun suggestion:
Ghost: My hour is almost come, When I to sulphurous and tormenting balloons Must render up myself.

From the terminal, use git diff to see the difference between scene-5.txt as it appears in the working directory vs. how it appears in your last commit.

You may need to press q on your keyboard to restore the terminal.
----------------------------------------------------------------------
$  git diff scene-5.txt     
                             
diff --git a/scene-5.txt b/scene-5.txt                  
index c3dc886..0fe3858 100644                           
--- a/scene-5.txt                                       
+++ b/scene-5.txt                                       
@@ -16,4 +16,5 @@ I will.                               
 Ghost:                                                 
 My hour is almost come,                                
 When I to sulphurous and tormenting flames             
+balloons                                               
 Must render up myself.                                 
\ No newline at end of file  
------------------------------------------------------


Use the new Git command to restore the file in your working directory to look as it did when you last made a commit.
Notice that the changes you made to the ghost's line have been discarded.


git show HEAD  
                                      
commit 17af1d91fabfe6bf39715abb06662d80e7093413         
Author: codecademy <ccuser@codecademy.com>              
Date:   Wed Mar 30 04:36:52 2016 -0400                  
                                                        
    This is final                                       
                                                        
diff --git a/scene-5.txt b/scene-5.txt                  
index b12dd97..c3dc886 100644                           
--- a/scene-5.txt                                       
+++ b/scene-5.txt                                       
@@ -12,3 +12,8 @@ Hamlet:                               
 I will.                                                
                                                        
                                                        
+                                                       
+Ghost:                                                 
+My hour is almost come,                                
+When I to sulphurous and tormenting flames             
+Must render up myself.                                 
\ No newline at end of file


-----------------------------------------------------------------------------------------
more git add

**********************************************************************************************

scene-3.txt

Hamlet: Act 1, Scene 3

A room in Polonius' house.

Enter LARRY and OPHELIA

LARRY
My necessaries are embark'd: farewell:
And, sister, as the winds give benefit
And convoy is assistant, do not sleep,
But let me hear from you.

OPHELIA
Do you doubt that?

LARRY
For Hamlet and the trifling of his favour,
Hold it a fashion and a toy in blood,
A violet in the youth of primy nature,
Forward, not permanent, sweet, not lasting,
The perfume and suppliance of a minute; No more.

OPHELIA
No more but so?

---------------------------------------------------------------------------------- 

scene-7.txt


Hamlet: Act 4, Scene 7

Another room in the castle.

Enter KING CLAUDIUS and LARRY

KING CLAUDIUS
Now must your conscience my acquaintance seal,
And you must put me in your heart for friend,
Sith you have heard, and with a knowing ear,
That he which hath your noble father slain
Pursued my life.

LARRY
It well appears: but tell me
Why you proceeded not against these feats,
So crimeful and so capital in nature,
As by your safety, wisdom, all things else,
You mainly were stirr'd up.

KING CLAUDIUS
O, for two special reasons;
Which may to you, perhaps, seem much unsinew'd,
But yet to me they are strong. The queen his mother
Lives almost by his looks; and for myself--
My virtue or my plague, be it either which--
She's so conjunctive to my life and soul,
That, as the star moves not but in his sphere,
I could not but by her. The other motive,
Why to a public count I might not go,
Is the great love the general gender bear him;
Who, dipping all his faults in their affection,
Would, like the spring that turneth wood to stone,
Convert his gyves to graces; so that my arrows,
Too slightly timber'd for so loud a wind,
Would have reverted to my bow again,
And not where I had aim'd them.

LARRY
And so have I a noble father lost;
A sister driven into desperate terms,
Whose worth, if praises may go back again,
Stood challenger on mount of all the age
For her perfections: but my revenge will come.
-------------------------------------------------------------------------------------------------------------

Add the files to the staging area together using a single git command.




The hamlet we are working on contains five files. In Git, it's common to change many files, add those files to the staging area, and commit them to Git in a single commit.
For example, say you want to change the character "LARRY" to "LAERTES" in the script. The name currently appears in two files. After you change the name in both files, you could add the changed files to the staging area with:
git add filename_1 filename_2
Note the word filename above refers to the name of the file you are adding to the staging area, such asscene-3.txt.

---------------------------------------------------------------------------------------------------------

unstage that file from the staging area using


git reset HEAD filename


This command resets the file in the staging area to be the same as the HEAD commit. It does not discard file changes from the working directory, it just removes them from the staging area.




To try out the new command, let's make a mistake on purpose!

The code editor is open to scene-2.txt. Delete any line from the file and click Run.




git add scene-2.txt 
2.

From the terminal, add scene-2.txt to the Git staging area.



        $ git add scene-2.txt                        
3.

Now check the status of the Git project.

In the output, notice scene-2.txt under “Changes to be committed”.




$ git status  
                                          
On branch master                                        
Changes to be committed:                                
  (use "git reset HEAD <file>..." to unstage)           
                                                        
        modified:   scene-2.txt                         
        modified:   scene-3.txt                         
        modified:   scene-7.txt                         

4.

Use the new Git command to unstagescene-2.txt from the staging area.

Notice in the output, "Unstaged changes after reset":

M scene-2.txt

  • M is short for "modification"

$ git reset HEAD scene-2.txt         
                   
Unstaged changes after reset:                           
M       scene-2.txt    

5.

Now that changes made to scene-2.txthave been booted out of the staging area, you're ready to commit. From the terminal, make a commit to save the Larry/Laertes name swap in hamlet.





$ git commit -m "This is final"           
              
[master 6a770ed] This is final                          
 2 files changed, 6 insertions(+), 6 deletions(-) 


***********************************************************************************************
   
git reset

Git enables you to rewind to the part before you made the wrong turn and create a new destiny for the project. You can do this with:
git reset SHA
This command works by using the first 7 characters of the SHA of a previous commit. For example, if the SHA of the previous commit is5d692065cf51a2f50ea8e7b19b5a7ae512f633ba, use:

git reset 5d69206



1.

From the terminal, print out your Git commit log.

Note: If your cursor gets stuck in "git log" mode in the terminal, press "q" on your keyboard to escape.




$ git log      
                                                                                                    
commit 5d692065cf51a2f50ea8e7b19b5a7ae512f633ba                                                                     
Author: danasselin <johndoe@example.com>                                                                            
Date:   Tue Nov 3 17:14:30 2015 -0500                                                                               
                                                                                                                    
    Add first page to scene-3.txt                                                                                   
                                                                                                                    
commit 27a3bfe282808aefe69d04f4d111e7a6a0c652dd                                                                     
Author: danasselin <johndoe@example.com>                                                                            
Date:   Tue Nov 3 17:14:08 2015 -0500                                                                               
                                                                                                                    
    Add first page to scene-2.txt                                                                                   
                                                                                                                    
commit 96f1625d347d599e6f0f13f23022b3f852d5b116                                                                     
Author: danasselin <johndoe@example.com>                                                                            
Date:   Tue Nov 3 17:13:38 2015 -0500                                                                               
                                                                                                                    
    Add .gitignore   

--------------------------------------------------------





2.

From the terminal, enter the command toreset to a previous commit, using the first 7 characters of one of the past commit SHAs in your Git log.

Next, print the Git commit log again.

Notice anything interesting? The commits that came after the one you reset to are gone. TheHEAD commit has been reassigned. You just changed history.



git reset 5d69206

*******************************************************************************************************


To better understand git reset commit_SHA, notice the diagram on the right. Each circle represents a commit.
Before reset:
  • HEAD is at the most recent commit
After resetting:

  • HEAD goes to a previously made commit of your choice
  • The gray commits are no longer part of your project
  • You have in essence rewinded the project's history







************************************************************************************************


Three different ways to backtrack in Git. 

You can use these skills to undo changes made to your Git project.
Let's take a moment to review the new commands:


  • git checkout HEAD filename:    Discards changes in the working directory.                                           

  • git reset HEAD filename: Unstages file changes in the staging area.

  • git reset SHA: Can be used to reset to a previous commit in your commit history.


*********************************************************************************************


                                    GIT BRANCHING




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?”


$ git branch

--------------------------------------------------------------------------------------------------



1.

Check what branch you are currently on.

In the output, the * (asterisk) is showing you what branch you’re on. The project only has one branch at this time.

$ git branch   
                                                                                                     

* master 


--------------------------------------------------------------------------------------------------




  • The circles are commits, and together form the Git project's commit history.
  • New Branch is a different version of the Git project. It contains commits from Master but also has commits that Master does not have.




-------------------------------------------------------------------------------------------------------------



Right now, the Git project has only one branch:master.
To create a new branch, use:
git branch new_branch

Here new_branch would be the name of the new branch you create, like photos or blurb. Be sure to name your branch something that describes the purpose of the branch. Also, branch names can’t contain whitespaces: new-branch and new_branchare valid branch names, but new branch is not.


------------------------------------------------------------------------------

.

Let's create a new version of a resumé to apply for a fencing instructor role.

Create a new branch called fencing.



$ git branch fencing                                                                                                 
$ git branch                                                                                                                     fencing                                                                                                           
* master  


Remember to spell the word "fencing" correctly.
Next, view your branches as you did in the previous exercise.
Notice in the output there now appear two branches: master and fencing.

-------------------------------------------------------------------------------------------------------------


The master and fencing branches are identical: they share the same exact commit history. You can switch to the new branch with
git checkout branch_name

You will be now able to make commits on thefencing branch that have no impact on master. Let's switch to the fencing branch.

--------------------------------------------------------------------


1.

Switch to the fencing branch from themaster branch.



$ git checkout fencing                                                                                              

Switched to branch 'fencing'

2.

Use git branch to verify that you have switched branches.

In the output, notice the * is now over thefencing branch.



$ git branch                                                                                                        
                                                                                                            
* fencing                                                                                                           
  master 
---------------------------------------------------------------------------------------






You have switched to a new branch. All the commands you do on master, you can also do on this branch.
For example, to add files to the staging area, use:
git add filename
And to commit, use:
git commit -m "Commit message"



-----------------------------------------------------
ex:  resume.txt


PETE PAN
Address: No 31 Kensington Hill Park, London, England
Phone: 000-111-2222
-------------------

WORK EXPERIENCE
Leader - Lost Boys
Neverland, 5th Star to Right, Straight on 'Til Morning
Duties:
-Manage Lost Boys
-Consult with Winged Sprites
-Scheme Against Captain Hook
-------------------

VOLUNTEER EXPERIENCE



-------------------------------------------------------------------------------------

1.

Print the Git commit log.

$ git log

commit 79a1cc5e23d46fd2f4459440cc18624d874cf36c                  

Author: danasselin <johndoe@example.com>                         
Date:   Wed Oct 28 17:10:17 2015 -0400                           
                                                                 
    Add heading for volunteer experience                         
                                                                 
commit df765b18c9b767089ef649f7ece33b58c27b897d                  
Author: danasselin <johndoe@example.com>                         
Date:   Wed Oct 28 17:09:50 2015 -0400                           
                                                                 
    Add experience scheming against hook                         
                                                                 
commit 1baf497026042a0938bc353c2d5406e31860f43e                  
Author: danasselin <johndoe@example.com>                         
Date:   Wed Oct 28 15:37:58 2015 -0400                           
                                                                 
    Add tasks to Lost Boys job                                   
                                                                 
commit 6e20f0694beabd45d3d929af7114b6c22d14ce36                  
Author: danasselin <johndoe@example.com>                         
Date:   Wed Oct 28 15:37:31 2015 -0400                           
                                                                 
    Add Lost Boys work experience  
------------------------------------------------------------------
Notice the output:
  • The commits you see were all made in the masterbranch. fencing inherited them.
  • This means that every commit master has, fencingalso has.
Note: if you find that your cursor is stuck in Git log, press qto escape.


-------------------------------------------------------------------------------------------------------


In resume.txt, replace your skill at scheming against Hook with your experience in sword-fights.
Delete this line:
-Scheme against Captain Hook
and type this line in its place:

-Engage in swordfights with pirates


----------------------------------
3.

3)  Add resume.txt into the staging area.



$ git add resume.txt


4) 

Commit the changes to the repository with a commit message.

           $ git commit -m "Now in new branch"

***********************************************************************************************


                                 git merge



What if you wanted include all the changes made to the fencing branch on the master branch? We can easily accomplish this by merging the branch into master with:
git merge branch_name
In a moment, you'll merge branches. Keep in mind:

  • Your goal is to update master with changes you made to fencing.
  • fencing is the giver branch, since it provides the changes.
  • master is the receiver branch, since it accepts those changes.

----------------------------------------------------------------------------------------------

1.

1)



You are currently on the fencing branch. Switch over to the master branch.



  $ git checkout master                                                                                               

      Switched to branch 'master'


2.

2)   

Your sword-fighting experience is so impressive that it belongs on the master version of your resumé.

From the terminal, merge the fencingbranch into the master branch.

Notice the output: The merge is a "fast forward" because Git recognizes thatfencing contains the most recent commit. Git fast forwards master to be up to date with fencing.


$ git merge fencing                                                                                                 

Updating 79a1cc5..59e56ef                                                                                           

Fast-forward                                                                                                        

 resume.txt | 2 +-                                                                                                  
 1 file changed, 1 insertion(+), 1 deletion(-) 

--------------------------------------------------------------------------------------------------------


The merge was successful because master had not changed since we made a commit on fencing. Git knew to simply update master with changes onfencing.
What would happen if you made a commit on masterbefore you merged the two branches? Furthermore, what if the commit you made on master altered the same exact text you worked on in fencing? When you switch back to master and ask Git to merge the two branches, Git doesn't know which changes you want to keep. This is called a 
merge conflict.

-------------------------------------------------------------------------------


You are on the master branch. In the code editor, where you have written:

-Engage in swordfights with pirates

Add the word "professional", so the text reads:

-Engage in swordfights with professional pirates
Click Run.
2.

Add resume.txt to the staging area.



$ git add resume.txt   
3.

Commit the changes to the repository with a commit message.



$ git commit -m "Now in merge"  

                       
[master 7eedaa2] Now in merge                           
 1 file changed, 1 insertion(+), 1 deletion(-)          
4.

Imagine a few weeks have passed, and you'd like to develop your fencing resumé some more.

Switch back to the fencing branch.



$ git checkout fencing                                  
Switched to branch 'fencing'   
5.

From fencing, change the line so it reads:

-Engage in swordfights with professional pirates such as Smee.

Click Run.
6.

Once again, add resume.txt to the staging area.



$ git add resume.txt       
7.

Commit the changes to the repository with a commit message.



$ git commit -m "Now back to master"                    

[fencing e2cef0d] Now back to master                    
 1 file changed, 1 insertion(+), 1 deletion(-) 


                        


------------------------------------------------------------------------------------------

Let's say you decide you'd like to merge the changes from fencing into master.
Here's where the trouble begins!
You've made commits on separate branches that alter the same line in conflicting ways. Now, when you try to merge fencing into master, Git will not know which version of the file to keep.


---------------------------------------------------
ex:  Resume.txt

PETE PAN
Address: No 31 Kensington Hill Park, London, England
Phone: 000-111-2222
-------------------

WORK EXPERIENCE
Leader - Lost Boys
Neverland, 5th Star to Right, Straight on 'Til Morning
Duties:
-Manage Lost Boys
-Consult with Winged Sprites
<<<<<<< HEAD
-Engage in swordfights with  professional pirates
=======
-Engage in swordfights with professional pirates such as Smee.
>>>>>>> fencing
-------------------

VOLUNTEER EXPERIENCE
------------------------------------------------------------------------

1.

Switch to the master branch.



$ git checkout master                                   

Switched to branch 'master'  
2.

From the terminal, enter the command below:

git merge fencing

This will try to merge fencing into master.


$ git merge fencing                                     
Auto-merging resume.txt                                 
CONFLICT (content): Merge conflict in resume.txt        
Automatic merge failed; fix conflicts and then commit th
e result.


In the output, notice the lines:
CONFLICT (content): Merge conflict in resumé.txt Automatic merge failed; fix conflicts and then commit the result.



3.

We must fix the merge conflict.

In the code editor, look at resume.txt. Git uses markings to indicate the HEAD (master) version of the file and the fencing version of the file, like this:

<<<<<<< HEAD master version of line ======= fencing version of line >>>>>>> fencing

Git asks us which version of the file to keep: the version on master or the version on fencing. You decide you want the fencing version.
From the code editor:
Delete the content of the line as it appears in the master branch
Delete all of Git's special markings including the words HEAD and fencing. If any of Git's markings remain, for example, >>>>>>> and=======, the conflict remains.
Try reloading the page if Git's markings don't show up.

4.


Add resume.txt to the staging area.



$ git add resume.txt

5.


Now, make a commit. For your commit message, type "Resolve merge conflict" to indicate the purpose of the commit.

                           

$ git commit -m "Resolve merge conflict"                

[master 62b18d0] Resolve merge conflict 
-------------------------------------------------------------------------------------------------------------------
                                                 delete branch


In Git, branches are usually a means to an end. You create them to work on a new project feature, but the end goal is to merge that feature into the masterbranch. After the branch has been integrated intomaster, it has served its purpose and can be deleted.
The command
git branch -d branch_name
will delete the specified branch from your Git project.
Now that master contains all the file changes that were in fencing, let's delete fencing.


---------------------------------------------------------------------------

1)   Delete the fencing branch.

$ git branch -d fencing                                                                                             

Deleted branch fencing (was e2cef0d).                                                                               

$ git branch                                                                                                                                                                                                                  
* master   

Now, verify that you have indeed deletedfencing by listing all your project's branches on the terminal.
Notice in the output that only one branch,master, is shown.


********************************************************************************************


The following commands are useful in the Git branch workflow.
  • git branch: Lists all a Git project's branches.
  • git branch branch_name: Creates a new branch.
  • git checkout branch_name: Used to switch from one branch to another.
  • git merge branch_name: Used to join file changes from one branch to another.
  • git branch -d branch_name: Deletes the branch specified.


*******************************************************************************************************

2. git clone

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.

*************************************************************************************************************************
Instructions



The Git remote Sally started is called:

science-quizzes

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


$ git clone science-quizzes my-quizzes                                                                         
Cloning into 'my-quizzes'...                                                                                   
done.   

Notice the output:
cloning into 'my-quizzes'...
Git informs us that it's copying everything fromscience-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.

********************************************************************************************************
3. git remote -v


Nice work! 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 isorigin.
You can see a list of a Git project's remotes with the command:
git remote -v

*************************************************************************************************************************
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.
2.

Change directories into the my-quizzesdirectory, enter this command on the terminal:

cd my-quizzes

To learn more about cd, take a look at ourcommand line course.
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 remoteorigin, 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.


$ cd my-quizzes                                         
$ git remote -v                                         
origin  /home/ccuser/workspace/curriculum/science-quizze
s (fetch)                                               
origin  /home/ccuser/workspace/curriculum/science-quizze
s (push
********************************************************************************************************

4. 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.
*************************************************************************************************************************

Instructions
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





$ cd my-quizzes    
                                                                                          
$ 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 localmaster 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 masterbranch. The command:

*************************************************************************************************************************
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 theHEAD commit is:

Add first question to Physics quiz

From the terminal, merge withorigin/master, where Sally's most recent commits are.
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 masterbranch 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


1)  $ cd my-quizzes       

                                                                                             

                                                             
2 )  $ git merge origin/master   
                                                                                       
Updating 2fd7d9b..3a29454                                                                                           
Fast-forward                                                                                                        
 biology.txt | 4 ++++                                                                                               
 1 file changed, 4 insertions(+)                                                                                    
 create mode 100644 biology.txt  

                                                                                   
3)  $ 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 9bfc082a0e64efe5c0a2e4298e0b4f127d0a9b96                                                                     
Author: danasselin <johndoe@example.com>                                                                            
Date:   Thu Oct 29 15:42:00 2015 -0400                                                                              
                                                                                                                    
    Add gitignore file  


********************************************************************************************************

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:
  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 mergecommand. 
Step 5 involves git push, a command you will learn in the next exercise


*************************************************************************************************************************
1.

Enter this command:

cd my-quizzes

to change directories into the my-quizzesdirectory.
2.

Enter the Git command:

git branch <branch_name>

to create a branch to develop questions for the biology quiz. Name the branchbio-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 biology.txt in the code editor.

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.
6.

Commit the work to the repository with a commit message.


   1)    cd my-quizzes
   2)   git branch bio-questions
  
  3) $ git checkout bio-questions                             
  Switched to branch 'bio-questions'                      
  
 4) $ git add biology.txt      
                             
  5) $ 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 9bfc082a0e64efe5c0a2e4298e0b4f127d0a9b96         
Author: danasselin <johndoe@example.com>                
Date:   Thu Oct 29 15:42:00 2015 -0400                  
                                                        
    Add gitignore file                                  
$ git commit -m "New question added"                    
[bio-questions 8703d12] New question added              
 1 file changed, 5 insertions(+)                        
$ git log                                               
commit 8703d12041db0eec2d10c216e48c2f9421ad8b54         
Author: codecademy <ccuser@codecademy.com>              
Date:   Fri Apr 1 03:31:50 2016 -0400                   
                                                        
    New question added                                  
                                                        
commit 3a294546f4a55f02bf37233ef8988d8b9dd7ce59         
Author: danasselin <johndoe@example.com>                
Date:   Tue Nov 3 12:33:23 2015 -0500                   
                                                        
    Add heading and comment to biology quiz    

********************************************************************************************************
7. 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.

*************************************************************************************************************************
1.

Enter this command

cd my-quizzes
to change directories into the my-quizzesdirectory.
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 branchbio-questions was pushed up to the remote. Sally can now review your new work and can merge it into the remote's masterbranch.


$ cd my-quizzes                                                                                                     

                                                                   
$ 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), 392 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  

*************************************************************************************************************************

  • 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
    • 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 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.


*************************************************************************************************************************
*************************************************************************************************************************
*************************************************************************************************************************
*************************************************************************************************************************







No comments:

Post a Comment