学习笔记之Git(英)

Basic Git Workflow

A Git project can be thought of as having three parts:

A Working Directory: where you'll be doing all the work: creating, editing, deleting and organizing filesA Staging Area: where you'll list changes you make to the working directoryA Repository: where Git permanently stores those changes as different versions of the project

The Git workflow consists of editing files in the working directory, adding files to the staging area, and saving changes to a Git repository. In Git, we save changes with a commit, which we will learn more about in this lesson.

 

git init

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

$ git init
Reinitialized existing Git repository in /home/ccuser/workspace/sorcerers-code/.git/

 

git status

As you write the screenplay, you will be changing the contents of the working directory. You can check the status of those changes with it.

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

In the output, notice the file in red under untracked files. Untracked means that Git sees the file but has not started tracking changes yet.

 

git add

In order for Git to start tracking scene-1.txt, the file needs to be added to the staging area.

We can add a file to the staging area with:

git add scene-1.txt

In Git, it's common to change many files, add those files to the staging area, and commit them to a repository 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

 

git diff

Good work! Now you know how to add a file to the staging area.

Imagine that we type another line in scene-1.txt. Since the file is tracked, we can check the differences between the working directory and the staging area with:

git diff scene-1.txt

 

git commit

A commit is the last step in our Git workflow. 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

 

git log

Often with Git, you'll need to refer back to an earlier version of a project. Commits are stored chronologically in the repository and can be viewed with:

git log

学习笔记之Git(英)_第1张图片

 

 

How to Backtrack in Git

head commit

 

In Git, the commit you are currently on is known as the HEAD commit. In many cases, the most recently made commit is the HEAD commit.

 

To see the HEAD commit, enter:

git show HEAD

The output of this command will display everything the git log command displays for the HEAD commit, plus all the file changes that were committed.

 

git checkout

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.

 

 

git reset 

 

What if, before you commit, you accidentally delete an important line from scene-2.txt? Unthinkingly, you add scene-2.txt to the staging area. The file change is unrelated to the Larry/Laertes swap and you don't want to include it in the commit.

 

We can 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.

Just like retracing your steps on that hike, Git enables you to rewind to the part before you made the wrong turn. You can do this with:

git reset commit_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 is 5d692065cf51a2f50ea8e7b19b5a7ae512f633ba, use:

git reset 5d69206

HEAD is now set to that previous 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 rewound the project's history

 

Git Branching

学习笔记之Git(英)_第2张图片

The diagram to the top illustrates branching.
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.

 

git branch

 

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.

 

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_branch are valid branch names, but new branch is not.

 

git checkout

 

You can switch to the new branch with

 

git checkout branch_name

 

Once you switch branch, be now able to make commits on the branch that have no impact on master.

 

You can continue your workflow, while master stays intact!

 

git merge

 

What if you wanted include all the changes made to the new 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 new_branch.
  • new_branch is the giver branch, since it provides the changes.
  • master is the receiver branch, since it accepts those changes.

 

merge conflict 

What would happen if you made a commit on master before you merged the two branches? Furthermore, what if the commit you made on master altered the same exact text you worked on in new branch? 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.

 

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 master branch. After the branch has been integrated into master, 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.

 

Git Teamwork

 

git clone

Student 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:
In this command: 

git clone remote_location clone_name

 

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.

 

git remote -v

 

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. 
You can see a list of a Git project's remotes with the command:

 

git remote -v

 

git fetch

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.

 

 

git merge

 

Even though Student's new commits have been fetched to your local copy of the Git project, those commits are on the origin/master branch. Your local master branch has not been updated yet, so you can't view or make changes to any of the work she has added.
Git Branching we learned how to merge branches. Now we'll use the git merge command to integrate origin/master into your local master branch. The command:

 

git merge origin/master

will accomplish this for us.

 

git push

 

The command:

 

git push origin your_branch_name

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

 

你可能感兴趣的:(学习笔记之Git(英))