ZH cheese: A concise tutorial on Git

Source: Internet
Author: User

This is the original website: HTTPS://TRY.GITHUB.IO/LEVELS/1/CHALLENGES/1

This blog post should be a note + translation.

Several noun-related

Changes: Change

Repository: Warehouse

Staging Area: Buffer area

Local: Native

Remotes: Remote

Git's workflow

Local file changes >add> buffers (staging area) >commit> local warehouses >push> remote warehouses (GITHUB)

Begin

Enter the working directory in Git bash, such as "Octobox"

1. Initialize a git repository

Git init

Then the "Octobox" directory will have an empty repository saved in/.git/, the repository is a hidden directory operated by Git.

2. Take a look at the status of the current project

git status

Because the project was just created, there is no need to submit a message:

# on Branch master## Initial commit#nothing to commit (Create"git add" to track)

3. Create a new TXT document "Octocat.txt" in "Octobox" and look at the status of the current project:

git status

The octocat.txt is displayed as "untracked files" because Git sees this as a new file:

" git add <file>, ... . " inch  "git add" to track)

4. To tell git to start tracking changes in the Octocat.txt file, we'll first add the file to the staging area:

git add octocat.txt

5. Now that Git has started tracking our octocat.txt files, let's take a look at the status of Project:

git status

Then we can see how git shows "pending changes (changes to be committed)", and the files listed here are files in the staging area that haven't been saved in our repository:

# on Branch master## Initial commit## changes to be committed:#    " git rm--cached <file> " to Unstage) #    #file:   octocat.txt#

6. To save our changes in staging area to Repository, we need to submit a message that describes the change in general:

" Add cute octocat Story "

Next you will see the message that the submission was successful:

[Master (root-commit) 20B5CCD] Add cute octocat Story 1 file 1 insertion (+100644 octocat.txt

7. How do I submit a large number of changes? Under current directory "Octobox": (1) New TXT file, (2) Create a new directory, put some TXT file in the new directory, and then add to Staging area:

' *.txt '

8. Then submit to Repository:

' Add all the octocat txt files '

At this point, we can see that all of the new txt that we just created is being submitted to repository:

[Master 3852b4d] Add all the octocat txt files 4 4 Insertions (+100644100644 octofamily/100644 octofamily/100644 red_octocat.txt

9. We have made some submissions, so how do we navigate what changes we have submitted?

git log

At this point, we can see all of our historical changes:

<[email protected]>:2020 - 0500  <[email protected]>: £2020 - 0500 Added cute octocat Story

10. To the current position of the change, submit a series of operations, we are also on the local machine, how to save on the Internet (GITHUB) it?

Start by creating a new empty repository on GitHub, and then write down the repository URL, such as:https://github.com/try-git/try_git.git

To save our local repository to the GitHub server, we want to add a remote repository (repository), Git remote Add this command needs to provide the remote repository name (custom) and repository URL (the one you created on GitHub repository):

Git remote add origin https://github.com/try-git/try_git.git

11. Push our changes to the remote repository origin (on GitHub):

The name of our remote repository is "origin", the local default branch name is "Master", and-U is telling git to remember the parameters behind it so that we can push with git push directly next time: Branch

Git push-u Origin Master

12. If, after a while, someone else has pull our repository from our GitHub and submitted, push, such as adding some files, now we want to download the latest repository to our local computer:

Git pull Origin Master

Then we can see the version of Pull:

Updating 3852b4d. 3e70b0ffast-1 +1file1 insertion (+100644 Yellow_octocat.txt

After 13.pull, we can look at the difference between the current repository and the state of our last commit, where head is a pointer to our most recent commit commit:

diff HEAD

You can see the difference in detail at this point:

diff --git a/octocat.txt b/100644---A/octocat.txt+ + +-  1 +  1 @@-A Taleof the Octocats +[ma Tale of both Octocats and an Octodog

The 14.diff command can also view files that have been staged (in staging area), staged files is what we tell git to be ready to submit, if we change the octofamily/ Octodog.txt, we add it to the stage:

git add octofamily/octodog.txt

15. Then look at what difference the staged file is:

diff --staged

Then see the message below:

diff --git a/octofamily/octodog.txt b/octofamily/file100644   0000000. CFBC74A---/dev/null+ ++ b/octofamily/-0,0 +1 @@ +[mwoof

16. We do not want to submit the Octodog.txt, to remove the octodog.txt from the staging area (unstaged):

git reset Octofamily/octodog.txt

17.git Reset helped us to unstaged the octodog.txt from the staging area, but notice that it still exists, just not in staging area, and if time turns back, Then octodog.txt back to Octodog.txt (which is the disappearance of this example), which is not very good. The file can be changed back to the state of our last commit, for example, we changed the Octocat.txt file, but wanted it back to the state of the last commit:

Git checkout--octocat.txt

18. When developers develop a new feature (a feature in a project) or fix a bug, they usually create a repository copy (branch, branch) so they can isolate the commit so that it doesn't affect the real product, and then when they're done, You can merge their branch and the primary master branch.

We want to clear some TXT files, first create a new branch clean_up:

Git branch clean_up

19. Now if you enter Git branch, you will see two local branches, one main branch master, and one new branch clean_up:

Git branch

Show:

  clean_up* Master

* Indicates the current working branch, we want to switch branches to CLEAN_UP:

git checkout clean_up

Show Toggle Success:

' clean_up '

20. Now on the CLEAN_UP branch, you can use Git rm to complete the cleanup task you just wanted to do (git rm not only clears files from the hard disk, but also puts removal into staging area)

RM ' *.txt '

Clear results:

RM ' Blue_octocat.txt ' RM ' Octocat.txt ' RM ' Octofamily/baby_octocat.txt ' RM ' Octofamily/momma_octocat.txt ' RM ' Red_octocat.txt '

21. You can use Git status to view the changes in the current stage:

git status

Show:

" git reset HEAD <file> " to Unstage) # # Deleted:    blue_octocat.txt# deleted:    octocat.txt# deleted:    octofamily/Baby_ octocat.txt# deleted:    octofamily/momma_octocat.txt# deleted:    "git add <file>, ..... " inch What'll be committed) # # octofamily/

Then submit the changes you just made:

" Remove All the Cats "

Submit Result:

[clean_up 63540fe] Remove all the Cats 5 5 Deletions (-100644100644100644 octofamily/  100644 octofamily/100644 red_octocat.txt

22. Go back to the master branch:

git checkout Master

23. Merge Clean_up to master:

git merge clean_up

Show:

Updating 3852b4d. Ec6888bfast-Forwardblue_octocat.txt|1-Octocat.txt|1-octofamily/baby_octocat.txt |1-octofamily/momma_octocat.txt |1-Red_octocat.txt|1-5Files changed,5Deletions (-) Delete Mode100644blue_octocat.txtdelete Mode100644octocat.txtdelete Mode100644octofamily/baby_octocat.txtdelete Mode100644octofamily/momma_octocat.txtdelete Mode100644Red_octocat.txt

24. Now that the cleanup has been completed, you do not need to clean_up this branch, you can XIEMOSHALV:

Git branch-d clean_up

25. The first few steps are played locally, now you can put the finished product into the remote repository:

git push

ZH cheese: A concise tutorial on Git

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.