30 min git command get started to abort

Source: Internet
Author: User
Tags unique id using git git mergetool

Git is so popular now that it's widely used in large open source projects, team development, and independent developers and even students.

Beginners are very easy to be frightened by various commands and parameters to cry. But actually just getting started you don't need to know the purpose of all the commands. You can start by mastering some simple, powerful commands and gradually learn from them. (This is what this article has to say). All right, come on!

Basic understanding

A git command is a collection of command-line tools that can be used to track and record changes to a file. For example, you can save, compare, analyze, merge, and so on. This process is called version control. There are a range of version control systems, such as SVN, Mercurial, Perforce, CVS, Bitkeepe, and so on.

Git is distributed, which means that it does not rely on a central server, and any machine can have a local version of the control system, which we call the repository. If you're collaborating with multiple people, you'll need an online repository for syncing information. This is the work of GitHub, BitBucket.

1. Install git

Installing Git is straightforward:

    • linux– Open the console and then install it via package management, the commands on Ubuntu are:
      sudo apt-get install Git-all
    • Windows– recommends using Git for Windows, which includes graphical tools and a command-line emulator.
    • The simplest way of OS X – is to use the homebrew installation, the command line executes
      Brew Install Git

If you are using a graphical tool first, then it is recommended that you use GitHub desktop,sourcetree. But I recommend you use the command line, the following is the command line.

2. Configure Git

After installing Git, the first task is to configure our information, the most important is the user name and mailbox, open the terminal, execute the following command.

$ git config--global user.name "My name" $ git config--global user.email [email protected]

By configuring these two items, users will be able to know who did what, and that everything is more organized, isn't it?

3. Create a new warehouse –git Init

Git will save all the files and history in your project, create a new repository, first go to the project path, execute git init. Git then creates a hidden folder. Git, where all the information is stored.

Create a Contact folder on the desktop git_exercise, open the terminal:

$ cd desktop/git_exercise/$ git init

OK, now the project is nothing, create a new Hello.txt file to try ~

4. Check state –git Status

Git status is another very important command that tells us the current state of the vault: whether it's the latest code, what updates, and so on, to perform git status:

$ git statuson branch masterinitial commituntracked files:  (use "git add ..." to include on what'll be committed) Hel Lo.txt

Git tells us that Hello.txt has not been traced, because this file is new, Git doesn't know if it should keep track of its changes, or just ignore it. To keep track of our new files, we need to stage it.

5. Staging –git Add

Git has a concept called staging area, and you can think of it as a blank canvas wrapped around all the changes you might commit. It starts empty, and you can add content via the git add command and commit using Git commit.

There is only one file in this example:

$ git Add hello.txt

If you need to submit all of the content in the directory, you can:

$ git add-a

Use git status again to view:

$ git Statuson branch masterinitial commitchanges to BES committed:  (use "Git rm--cached ..." to unstage) new file:
   
    hello.txt
   

Our documents have been submitted. The status information will also tell us what happened to the staging area file, but here we are submitting a new file.

6. Submit –git Commit

One commit represents our warehouse to a delivery state, usually with a small piece of functionality completed. It's like a snapshot that allows us to go back to the old days like we did with a time machine.

To create a commit, we need to submit something to staging area (git add) and then:

$ git commit-m "Initial commit."

This creates a commit,-M "Initial commit." Represents a description of this submission and recommends the use of meaningful descriptive information.

Remote Warehouse

So far, our operations have been local, and it exists in the. git file. In order to be able to co-develop, we need to publish the code to the remote repository.

1. Link remote repository –git remote add

In order to be able to upload to the remote repository, we need to first establish the link, this tutorial, the address of the remote repository is: Https://github.com/tutorialzine/awesome-project, but you should yourself on GitHub, BitBucket on the building of warehouses, and step by step to try.

Add a remote repository for testing

$ git Remote add Origin https://github.com/tutorialzine/awesome-project.git

A project can have several remote warehouses at the same time in order to be able to differentiate, usually different names. Usually the main remote repository is called origin.

2. Uploading to the server –git push

Every time we commit the code to the server, Git push is used.

The git push command will have two parameters, the name of the remote repository, and the name of the branch:

$ GIT push origin mastercounting objects:3, done. Writing objects:100% (3/3), 212 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (Delta 0) to Https://github.com/tutorialzine/awesome-project.git * [New branch]      master-> ; Master

Depending on the server you are using, the push process may require you to verify your identity. If nothing goes wrong, now look at your remote branch using your browser, and Hello.txt is already waiting for you.

3. Clone Repository –git Clone

Open source projects on GitHub where people can see your code. You can use git clone to download to local.

$ git clone https://github.com/tutorialzine/awesome-project.git

A new warehouse is also created locally, and the branch on GitHub is automatically set as the remote branch.

4. Pull the replacement code –git from the server

If you update the code to the repository, others can pull your changes through the git fetch command:

$ Git pull Origin masterfrom https://github.com/tutorialzine/awesome-project * Branch            Master--     Fetch_ Headalready up-to-date.

For the moment no one else submits, all without any change

Branch


Branchs

When you're doing a new function, it's best to develop it in a separate area, often called a branch. Branches are independent of each other and have their own historical records. The reasons for this are:

    • Stable version of code will not be corrupted
    • Different functions can be developed by different developers at the same time.
    • Developers can focus on their own branches, without worrying about being destroyed by other people.
    • Prior to uncertainty, the same feature can have several versions for easy comparison
1. Create a new branch –git branch

The default branch for each warehouse is called Master, so you can create a new branch:

$ git Branch amazing_new_feature

Creates a new branch named Amazing_new_feature, which is the same starting point as the current branch

2. Switch Branch –git Checkout

Using Git branch alone, you can view the status of a branch:

$ git branch  amazing_new_feature* Master

*Indicates that the current active branch is master and uses git checkout to switch branches.

$ git checkout amazing_new_feature
3. Merge Branch –git Merge

The task of our Amazing_new_feature branch is to add a featuer.txt. We will create, add to staging area, submit.

$ git add feature.txt$ git commit-m "New feature complete."

New Branch task completed, back to master branch

$ git Checkout Master

Now go to the file and you will see that the Feature.txt file created earlier is missing because there is no feature.txt on the master branch. Use git merge to merge the Amazing_new_feature branches onto master.

$ git Merge amazing_new_feature

Ok! Then delete the Amazing_new_feature branch.

$ git branch-d amazing_new_feature
Senior

In the last section of this article, we'll take a few more advanced and used techniques.

1. Differences between two different submissions

Each commit has a unique ID that looks at all commits and their IDs, and can use git log:

$ git logcommit ba25c0ff30e1b2f0259157b42b9f8f5d174d80d7author:tutorialzinedate:   Mon May 30 17:15:28 2016 +0300    New feature Completecommit b10cc1238e355c02a044ef9f9860811ff605c9b4author:tutorialzinedate:   Mon 16:30:04 +0300    Added content to Hello.txtcommit 09bd8cc171d7084e78e4d118a2346b7487dca059author: Tutorialzinedate:   Sat 17:52:14 +0300    Initial Commit

The ID is long, but you don't need to copy the entire string, and the first part is enough.
To see what was updated with a commit, use git show:

$ git show b10cc123commit b10cc1238e355c02a044ef9f9860811ff605c9b4author:tutorialzinedate:   Mon May 30 16:30:04 +0300    Added content to Hello.txtdiff--git a/hello.txt b/hello.txtindex e69de29. B546A21 100644---a/hello.txt+++ b/hello.txt@@ -0,0 +1 @@+nice weather today, isn ' t it?

To see the difference of two commits, you can use Git diff [commit-from]. [Commit-to] Syntax:

$ git diff 09bd8cc. Ba25c0ffdiff--git a/feature.txt b/feature.txtnew file mode 100644index 0000000..e69de29diff--git a/hello.txt b/ Hello.txtindex e69de29. B546A21 100644---a/hello.txt+++ b/hello.txt@@ -0,0 +1 @@+nice weather today, isn ' t it?

Comparing the first commit and the last commit, we can see all the changes. Of course, using the git difftool command is more convenient.

2. Roll back a file to a previous version

Git allows us to roll back a particular file to a specific commit, using Git checkout as well.

In the following example, we will roll back the hello.txt to its original state, requiring the specified rollback to which commit, and the full path of the file.

$ git checkout 09bd8cc1 hello.txt
3. Roll back the submission

If you find that the latest submission is finished with a file, you can fix it by using Git commit-amend, which will return the latest commit to staging area and try to resubmit it.

If it's a more complex situation, such as not the latest commit. Then you can use git revert.

The latest commit alias is also called Head.

$ git revert HEAD

Other commits can use the ID:

$ git revert b10cc123

Conflicts occur very frequently when you roll a commit. git does not roll back correctly when the file is modified by subsequent commits.

4. Resolve Merge conflicts

Conflicts often occur in merging branches or pulling code out of others. Sometimes git can handle conflicts automatically, but most of us need to handle them manually.

For example, John and Tim wrote two parts of the code on each branch.
John likes for:

Use a For loop to console.log contents. for (var i=0; i<arr.length; i++) {console.log (arr[i]);} Tim likes foreach://use of ForEach to console.log contents. Arr.foreach (function (item) {Console.log (item);});

Suppose John is now going to pull the Tim code:

$ git merge tim_branchauto-merging print_array.jsconflict (content): merge conflict in print_array.jsautomatic merge fail Ed Fix conflicts and then commits the result.

At this point git does not know how to resolve the conflict, because he does not know John and Tim who write better.

It then inserts the tag in the code.

<<<<<<< head//use a For loop to Console.log Contents.for (var i=0; i<arr.length; i++) {    consol E.log (Arr[i]);} =======//use ForEach to Console.log contents.arr.forEach (function (item) {    Console.log (item);}); >>>>>>> Tim s commit.

Above the = = symbol is the most recent commit, and below is the code for the conflict. We need to resolve such a conflict, after the committee members discussed, unanimously decided that everyone here is rubbish! Two, not all of them. Change to the following code.

Not using a For loop or foreach.//with array.tostring () to Console.log Contents.console.log (arr.tostring ());

Well, submit it again:

$ git add-a$ git commit-m "Array printing conflict resolved."

If you are in a large project, this process can be prone to problems. You can use GUI tools to help you. Use Git mergetool.

5. Configuration. Gitignore

Most of the projects, there will be write files, folders are we do not want to submit. In order to prevent accidentally submitting, we need to Gitignore file:

    1. Create a. gitignore file in the project root directory
    2. List file names, folder names, and each row in the file that you do not need to commit
    3. . gitignore files need to be submitted, just like regular files

Files that are usually ignore are:

    • Log file
    • Task Runner Builds
    • Folders such as Node_modules
    • Ides generated files
    • Personal notes

For example:

*.logbuild/node_modules/.idea/my_notes.txt
Summarize

End of Tutorial ~ (Sprinkle Flower)
Git is a bit complicated, and there are a lot of features and tricks waiting for you to dig, this article is just to provide the tip of the iceberg, I hope you do not because too many cumbersome commands to stop the pace of progress! My bosom is quite!

30 min git command get started to abort

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.