30-minute git command "from getting started to giving up"

Source: Internet
Author: User

git is now very popular, it is widely used in large open-source projects, but beginners very easy "from the start to give up", various commands various parameters, God, the baby will be frightened to cry. In fact, the novice does not need to understand the purpose of all the commands, learning is required a gradual process, you can start from a powerful command. This is a tutorial for novice or veteran veterans who are familiar with graphical tools, "from getting started to giving up"

first, 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 it's a multi-person collaboration, you need an online repository to sync the information, which is what GitHub, BitBucket, is doing.

1. Install git

Installing Git is very straightforward, Linux-open the console and then install it via package management, the commands on Ubuntu are:

Sudo?apt-get?install?git-all

Windows-it is recommended to use Git for Windows, which includes graphical tools and a command-line emulator.

OS X-The simplest way is to use homebrew installation, command line execution

>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 "[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 repository-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 Status-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?status  on?branch?master  initial?commit  untracked?files:  (use? ") Git?add?... "? to?include?in?what?will?be?committed)  Hello.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?status on?branch?master initial?commit changes?to?be?committed: (use? ") GIT?RM?--Cached?... "? to?unstage)  new?file:?hello.txt<br></span>

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

second, the 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?master Counting?objects:done. writing?objects:?100%? (3/3),? 212?bytes?|? 0?bytes/s,?done. Total?3? (delta?0), reused?0? (delta?0) to *? [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 from the server-git pulls

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

$?git?pull?origin?master  from  *?branch?master?->? Fetch_head  already?up-to-date.

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

Third, Branch

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 branches-git checkout

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

$?git?branch amazing_new_feature *?master

The * number indicates that the active branch is the master, using git checkout to switch branches.

$?git?checkout?amazing_new_feature
3. Merge branches-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
Iv. Advanced

Here are some of the 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?log commit?ba25c0ff30e1b2f0259157b42b9f8f5d174d80d7  Author:? Tutorialzine Date:? Mon? may?30?17:15:28?2016?+0300 new?feature?complete commit?b10cc1238e355c02a044ef9f9860811ff605c9b4 Author:? Tutorialzine Date:? Mon? may?30?16:30:04?2016?+0300 added?content?to?hello.txt commit?09bd8cc171d7084e78e4d118a2346b7487dca059 Author:? Tutorialzine<br>date:? Sat? may?28?17:52:14?2016?+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?b10cc123 \\commit?b10cc1238e355c02a044ef9f9860811ff605c9b4 Author:? Tutorialzine Date:? Mon? may?30?16:30:04?2016?+0300 added?content?to?hello.txt diff?--Git?a/hello.txt?b/hello.txt Index?e69de29. b546a21?100644---? a/hello.txt +++?b/hello.txt @@?-0,[email protected]@ +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. BA25C0FF  diff?--Git?a/feature.txt?b/feature.txt new?file?mode?100644  index?0000000..e69de29  diff?-- Git?a/hello.txt?b/hello.txt  index?e69de29. b546a21?100644  ---? a/hello.txt  +++?b/hello.txt @@-0,[email protected]@<br>+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?foreach?to?console.log?contents.  Arr.foreach (the 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?failed;? Fix?conflicts?and?then?commit?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++)? {console.log (arr[i]);} =======//? Use?foreach?to?console.log?contents.arr.foreach (the 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 found that everyone here is rubbish! Two don't. Change to the following code.

//? Not?using?for?loop?or?foreach.  //? Use? 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:

Create a. gitignore file in the project root directory

List file names, folder names, and each row in the file that you do not need to commit

. gitignore files need to be submitted, just like regular files

Files that are usually ignore are:

Log file Task Runner Buildsnode_modules folder Ides generated files personal notes

For example:

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

This article reprinted address: http://www.linuxprobe.com/in-30min-gitcli-ok.html

Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/

30-minute git command "from getting started to giving up"

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.