2016年6月15日 星期三

How to use Git

Leson1
Finding Diffs Between Larger Files
commits is a check point
Command
VSC


git log   (see the history)
git diff old new
git log --stat
git checkout id (go to the past)
git clone https://github.com/udacity/asteroids.git   ( clone a repository )
How Often to Commit
good rule of thumb is to make one commit per logical change. For example, if you fixed a typo, then fixed a bug in a separate part of the file, you should use one commit for each change since they are logically separate.
Quiz
One Commit per Logical Change Solution


You commit all the changes required to add a new feature, which you’ve been working on for a week. You haven’t committed since you started working on it.


This commit seems too big. It's easier to understand what each commit does if each only does one thing and is fairly small. Going a week without committing is not the best idea.


You found three typos in your README. You fix and commit the first.


This commit seems too small. It would be better to fix all three typos, then commit. That way, your history won't get too cluttered with typo fixes. Plus, you don’t need to worry about introducing bugs to a README, so bundling changes together is more likely to be a good idea.


You commit all the changes required to add a new feature, which you’ve been working on for an hour.


This is probably a good size for a commit. All the work is on a single feature, so the commit will have a clear logical purpose. After an hour, the diff will probably have a fair amount of content in it, but not too much to understand.


On the other hand, sometimes after working for an hour you’ll have run into more than one natural committing point, in which case you would want to break the feature up into smaller commits. Because of this, “too big” could also be a reasonable answer here.


You fix two small bugs in different functions and commit them both at once.


This commit is probably too big. It would have been better to commit after the first bug fix, since the two bug fixes aren't related to each other.


Judgment Call


Choosing when to commit is a judgment call, and it's not always cut-and-dried. When choosing whether to commit, just keep in mind that each commit should have one clear, logical purpose, and you should never do too much work without committing.



What do you think are the pros and cons of manually choosing when to create a commit, like you do in Git, vs having versions automatically saved, like Google Docs does?


no trouble,but ,may be this is not the thing you want

What is a Repository
commite with Multiple file



problem:
  1. after use git check out,how can we go back to the most recent id?


setting up the Git Workspace make it more easy to use


Changing background color

If you prefer the background color of Git Bash to be something other than black, you can change it in the "Defaults" menu under the "Colors" tab. If you like the background color as-is, you don't need to make any changes.

Downloading necessary files

  • Save this file in your home directory with the name git-completion.bash.
  • Save this file in your home directory with the name git-prompt.sh.
  • Download bash_profile_course from the Downloadables section.
  • If you already have a file in your home directory named .bash_profile, copy the content from bash_profile_course and paste it at the bottom of .bash_profile. Otherwise, move bash_profile_course to your home directory and rename it to.bash_profile. (If you're curious to learn more about how bash prompts work, see this page.)

Making Git configurations

Run the following Git configuration commands. The first one will need to be modified if you are using a text editor other than Sublime, or if Sublime is installed in another location for you. See this page for the correct command for a couple of other popular text editors. For any other editor, you'll need to enter the command you use to launch that editor from Git Bash.
git config --global core.editor "'C:/Program Files/Sublime Text 2/sublime_text.exe' -n -w"
git config --global push.default upstream
git config --global merge.conflictstyle diff3

Make sure you can start your editor from Git Bash

If you use Sublime, you can do this by adding the following line to your .bash_profile:
alias subl="C:/Program\ Files/Sublime\ Text\ 2/sublime_text.exe"

Restart Git Bash

You'll need to close and re-open Git Bash before all your changes take effect.

Supporting Materials



Lesson 2


A repositores
in a repositores
it contain a .git file,it store the data use by git
Initializing a Repository
command


git init       create a    .git file
gir status    show which file has change sice last commit


Staging Area
we can add file to the staging area




for example, i add cake.txt than add frosting.txt to the staging area,
than, a single commit,2 file together commited


code:
git add cake.txt
git add frosting.txt


remove


git reset lesson_2_reflections.txt




Writing Good Commit Messages

code:
git commit
git commit -m "Commit message"


How to write a goode Commit Messages


Commit Messages

Message Structure

A commit messages consists of three distinct parts separated by a blank line: the title, an optional body and an optional footer. The layout looks like this:
type: subject

body

footer
The title consists of the type of the message and subject.

The Type

The type is contained within the title and can be one of these types:
  • feat: a new feature
  • fix: a bug fix
  • docs: changes to documentation
  • style: formatting, missing semi colons, etc; no code change
  • refactor: refactoring production code
  • test: adding tests, refactoring test; no production code change
  • chore: updating build tasks, package manager configs, etc; no production code change

The Subject

Subjects should be no greater than 50 characters, should begin with a capital letter and do not end with a period.
Use an imperative tone to describe what a commit does, rather than what it did. For example, use change; not changed or changes.

The Body

Not all commits are complex enough to warrant a body, therefore it is optional and only used when a commit requires a bit of explanation and context. Use the body to explain the what and why of a commit, not the how.
When writing a body, the blank line between the title and the body is required and you should limit the length of each line to no more than 72 characters.

The Footer

The footer is optional and is used to reference issue tracker IDs.

Example Commit Message

feat: Summarize changes in around 50 characters or less

More detailed explanatory text, if necessary. Wrap it to about 72
characters or so. In some contexts, the first line is treated as the
subject of the commit and the rest of the text as the body. The
blank line separating the summary from the body is critical (unless
you omit the body entirely); various tools like `log`, `shortlog`
and `rebase` can get confused if you run the two together.

Explain the problem that this commit is solving. Focus on why you
are making this change as opposed to how (the code explains that).
Are there side effects or other unintuitive consequenses of this
change? Here's the place to explain them.

Further paragraphs come after blank lines.

- Bullet points are okay, too

- Typically a hyphen or asterisk is used for the bullet, preceded
  by a single space, with blank lines in between, but conventions
  vary here

If you use an issue tracker, put references to them at the bottom,
like this:

Resolves: #123
See also: #456, #789


git diff Revisited
after a file modified,git status,


it show it is modified.
add to the staging area
and commint it again




we can use git diff id1 id2 to compare two commit?
What about we want to compare the file in staging arae and working directory?

we can use


git diff


How about the difference between the staging arae and the recent commit?
git diff --staged


go back to the most recent commit for the working directory
staging area is empty
git reset --hard




Branches
not just create linear commit


for each branches,will have a branche label on each most recent commit of each branch
and the  branche label  will auto update to the most recent commit


a single commit can have multi branche label
we check out a branch(by branch label),when update on this branch (branch label),new commit, like this
another one branch label (backup) will not move forward


code
create a branch


git branch         will show current branch


git branch  branch_mame       create a new branch




from the start,we can see we currently at the master branch,although we have created the branch2 branch


git checkout branch2       go to other branch

than ,all commit will happen on this branch


Viewing the commit history


code :git log --graph --oneline master coins

How to show the log?
we use git log,how the system show which log it show?


start with the id of the commit that the branch label is currently on, then trace back to its parent, then that commit's parent, and so on until you get to a commit with no parent.  And remember to separate the commit ids with commas!  Spaces are optional.


view the master and coin brance


Detached HEAD Revisited


when you type git checkput commitID,you will see the folloe



You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

 git checkout -b <new-branch-name>

HEAD is now at 19e279b... try commit first time



Head is current commit,
use above as a example,when type git checkout 7dc, HEAD is at 7dc, than make change ,than commit,will create f36


f36 not include in any branch,so will not show in the  git log,show,if we lost the commit id,we can’t go back here


however,we can create a new bracnh for it


code:
git checkout -b new_bracng_name


we can see that the history in git is nolinear,


Combining Simple Files




how to solve the unknown?
if we have orginal file


A should not keep,because Jake want to delete it from the orginal
C,E should keep,because Rachel add C and Jake add E,they are not in the orginal file.


Merging Coins into Master


need to use 3 point
and now is merge the coins branch into the master branch so


How will be the log after merge?


code:
first checkout branch2
git merge branch2 branch3


branch 3 merge into branch2




git show commit_id
can show the different between it parent,even you don’t know it parent


after megre 3 into 2, we can delete the label of brancg 3(not the commit)


git branch -d branch 3

Merge Conflicts
not always can auto merge
example:
A B D is different line
B’ is a modified version of B
B’’ is another modified version of B

A D must keep,but how about B’ and B”?


answer is unknown




Conflict Detection
how git detect the conflict?
decide by the user


EX marge
if master into the easy-mode branch,what will happen?
tips:merge create new commit.
Answer:

Resolving Merge Conflicts
saw this video:

Asteroid = function() {  
 this.breakIntoFragments = function () {
   for (var i = 0; i < 3; i++) {
     var roid = $.extend(true, {}, this);
     roid.vel.x = Math.random() * 6 - 3;
     roid.vel.y = Math.random() * 6 - 3;
     if (Math.random() > 0.5) {
       roid.points.reverse();
     }    
     roid.vel.rot = Math.random() * 2 - 1;
     roid.move(roid.scale * 3); // give them a little push
     Game.sprites.push(roid);
   }    
 };

 this.collision = function (other) {
   SFX.explosion();
   if (other.name == "bullet") Game.score += 120 / this.scale;
   this.scale /= 3;
   if (this.scale > 0.5) {
<<<<<<< HEAD
     // break into fragments
     for (var i = 0; i < 2; i++) {
       var roid = $.extend(true, {}, this);
       roid.vel.x = Math.random() * 6 - 3;
       roid.vel.y = Math.random() * 6 - 3;
       if (Math.random() > 0.5) {
         roid.points.reverse();
       }    
       roid.vel.rot = Math.random() * 2 - 1;
       roid.move(roid.scale * 3); // give them a little push
       Game.sprites.push(roid);
     }
||||||| merged common ancestors
     // break into fragments
     for (var i = 0; i < 3; i++) {
       var roid = $.extend(true, {}, this);
       roid.vel.x = Math.random() * 6 - 3;
       roid.vel.y = Math.random() * 6 - 3;
       if (Math.random() > 0.5) {
         roid.points.reverse();
       }
       roid.vel.rot = Math.random() * 2 - 1;
       roid.move(roid.scale * 3); // give them a little push
       Game.sprites.push(roid);
     }
=======
     this.breakIntoFragments();
>>>>>>> master
   }
 };
};
merged common ancestors part is the code orginaly
master part(this.breakIntoFragments();)  is the code in master
Head part is the code in the current branch


how to solve?decide by people,the conclusion is keep 1 of the 3 part ,andy delay the special 付號 is ok

Committing the Conflict Resolution
how to give the answer to the git?
saved the file,
git status show below picture


git add


git commit





code summery lesson2


git init       
create a    .git file,create a respority
gir status    
show which file has change sice last commit
git add cake.txt
git add frosting.txt
add the file to staging area
git reset lesson_2_reflections.txt
from staging area
git commit
git commit -m "Commit message"
create commit
git diff
compare working directory and  staging area
git diff --staged
compare staging area and most recent commit
git reset --hard

go back to the most recent commit for the working directory
staging area is empty
git branch         
will show current branch
git branch  branch_mame       
create a new branch
git checkout branch2      
go to other branch
git log --graph
show the log as a picture
git log --graph --oneline master coins
show the log in branch master and branch coins as a picture
git checkout -b new_bracng_name
=f
irst
git branch  branch_mame       
than
git checkout branch2      
first checkout branch2
git merge branch2 branch3
branch 3 merge into branch2
git show commit_id
can show the different between it parent,even you don’t know it parent
git branch -d branch3
delete the branch3 label













GitHub




GitHub only contain commit hostory.


push
first ,we need to create a new repository at git Hub,a remote repository
local has a repository  too.


we can push and pull data


the data that we send and receive is commit
but not the individual commit,is a branch


EX


when push branch A,will push e53 to the GutHub first,than track back send fd2,because a3b already on the server og Github,so no send a3b

Push your branch to the github
git remote add origin git@github.com:roy989898/reflections.git
git push -u origin master

the origin is the remote name,can be any name   
the url is get from git hub should Copy the HTTPS URL, not the SSH URL!   

Editing Files on GitHub
we can create and edit file directly on the github




but the change will not appear at the local

Pulling Changes






git pull origin master


Concept Map: GitHub, Push, Pull, Remote

Forking a Repository
Traditionaly


if i share a github Repository, other people*Caroline( want to get it use clone,clocne to their local machine,
for the clone,auto create a remote  call origin
when Caroline want otpush to GitHub(her own account),need create another remote call our-version,than push the master


Use fork
little step
directly to your account


Fork the Recipes Repository
use git clone to cloe to the local


aouto create a remote


can push and pull


Add Collaborator
other people can push to your Repository too.
click setting,>Collaborator>add

EX
try this
Larry's repository on GitHub can be found here.

Collaborations Cause Conflict


EX
for this first:
Larry's repository on GitHub can be found here.


Simulate Sarah's Changes

In the Downloadables section, there is a file called sarah_changes.sh, which contains code to make it look like Sarah has modified your fork on GitHub. To run the code, download the file, then using Git Bash or your terminal, cd to the directory where you've saved it. Then type bash sarah_changes.sh followed by a space, followed by the url to your fork. For example, if Caroline were running the code, she would type bash sarah_changes.sh https://github.com/cbuckey-uda/recipes.git, but you should use the url for your fork, not Caroline's fork. If you haven't set up password caching, then you will be prompted to enter your GitHub username and password.

Updating Local Copies of Remote Branches,Fetch nad Pull
\at the begning ,we clocne the Repository  to local,aouto create a remote call orihin that point to the cloned github


show will will see a branch call master at local


actually,at the local has another branch,but we haven’t see that, it indicate the last known position of the remote, the name is remote_name/remote_branch_name(origin/master)


when we make commit on the master local branch
orihin/master at the same location,because we don’t communicate to the github yet


if we push the master bracnh



how about if github changed,and local change too?
than call fectch


will create a new commit at local,will not combined


we need combine by ourself.
merge orginal/master into master


for pull:

do:





Ex:
out of sync because no conectbetween orihin/master and the master branch

Merging the Changes Together


fiirst checkout master branch
code: git merge master origin/master


solve the conflict
git add> git commit

EX:


Fast-Forward Merges


if merge b into a,what happen?
should be like this.


howeer,
because b already have all the information of A,show actually ,just move the aA label forward


EX


Making a Pull Request,collect the feeback before update to the master branch
we can make a change on a sperate branch,call change,than push it to the github
fially,get ready,mege them


at github


create a new pull request to the different-oil brancg


choose like this,
not the people you fork for
than other people, will se the pull request,receive email.and comment it


EX


soultion


*******
Sarah accidentally says that the local master is the only thing that changes when you run git pull origin master. However, the working directory and staging area will also update when you run git pull. That's why when you run git pull, you see your files update, not just the git log output.


Updating a Pull Request
make a commit,than push to github, Pull Request auto update


Other Collaborater update the same file or same line ,Conflicting Changes
on githuib,other Collaborater  add more oil,i add different oil


on the Github wesite,we can merge the pull request on the website:
like this:


we clickthe button for more oil pull request
no,Fast-Forward Merges,github always create a new commit
and the more oil branch will delete by the github
all the thing above happen in the GitHub
than Carolin pull


we can see that the orgin/master is a the bed at lacal,it is because at last cmoounicate,the master at bed in gihub


finally ,we merge them,
Carolin  want to get her change onto the master branch,we can just direstly merge it in to the Master branch like before,However
this can not let other people, what you change,.So, we caret a h,a new commit at different,pull request auto update, to solve the conflict,, than merge the different oil into the Master
,so we see one more commit(e47).




why merge master into different _oil first?anove have the answer

EX


Simulate Sarah's changes

To simulate Sarah's changes, first download the file sarah_changes_2.sh from the Downloadables section. Then, using Git Bash or your terminal, cd to the directory where you've saved the file. Then type bash sarah_changes_2.sh followed by a space, followed by the url to your fork. For example, if Caroline were running the code, she would type bash sarah_changes_2.sh https://github.com/cbuckey-uda/recipes.git, but you should use the url for your fork, not Caroline's fork. If you haven't set up password-caching, then you will be prompted to enter your GitHub username and password.
Note: This code will not actually create a pull request. Instead, it will update the commit history on GitHub to make it look as though the pull request has already been merged. You will not need to merge a pull request. Instead, check for a commit on the master branch on GitHub with the message "Merge pull request".

Make sure you are on the master branch

You'll want to make sure you check out the master branch before following the steps in this video. You'll want to make sure you check out the master branch before following the steps in the instructions video.

Use git add on the conflicting files before committing

Just as in lesson 2, before running git commit to create the merge commit, you'll need to use git add to add any files that had conflicts to the staging area.

Supporting Materials



Keeping a Fork Up-To-Date


at the original,we fork and clone




than,the orginal has cahnge,we have make other change on the local,and push back to the fork


because of the conflicts,the fork and original can not merge.


so,we need to download the original to the local clone.
we already has a origin remote to the fork,because of the fork


we create another remote call upstream,and fectch it,create a upstream/master branch


between upstream/master and change has conflict


merge the upstream/master into master first,than merge master branch into the change branch
than push them to the fork(change branch and master branch)


finally,merged.


code summery


git remote
see remote have at the local
git remote add origin(name) HTTPS URL
add the remote repository to the local
git remote -v
show the information
git push -u origin master
push the master branch to the origin remote repository
git pull origin master
pull the commit form the remote to the local

=git fetch origin+
git merge master origin/master
git branch -a
List both remote-tracking branches and local branches.
git fech origin
updat all local copy of every branch for the origin remote
check out master

git merge master origin/master
merge the master origin/master