[Dry Goods sharing]git simple understanding and basic Operation command

Source: Internet
Author: User
Tags using git version control system

The front-end small white one, recently started using Git, so spent 2 days to see the Liaoche git tutorial (partial practice, for learning git basic operation is very helpful OH), also look at "Git version control" This book (partial theory, the content is perfect, very good), for the learning content built git warehouse test, And write a lot of git operation command notes, do a share, there are mistakes in the place you greatly also give, good to make changes ~

Git is a free, open source, distributed version control system developed with the Linux kernel.

What's the difference between git and svn?

GIT uses distributed repository management, and SVN uses centralized repository management.

Centralized repository management requires a server that hosts the repository, where developers pull up the latest version from the server at the time of development, then create/go to the branch for development, and then commit or merge the branches into the main branch after the development is complete.

Distributed repository Management allows developers to move repositories to their own computers, and during development, developers can create branches and modify code for different purposes, merging after development, which improves agility and speed of development and reduces the pressure on public servers. and conflicts between any two developers are easier to resolve.

git installation

First, you can perform a "git" command on your PC to check if Git is installed. If it is already installed, it will be listed with its options and the most commonly used subcommands. If it is not installed, the print prompt is not installed or is not an internal/external command.

Installation package: Git.download, according to the need to choose, presumably with you many years of computer switch machine experience, QQ, browser, games, such as the Ann loading and unloading experience, this step should not appear what problems.

Configuration of Git

After installing Git, we execute the "git" command to see its options and the most commonly used subcommands.
Here the orders are not listed, and we can perform their own look (let me disguise under the comments, thank you ~)

Then just configure your name and email:

git config--global user.name "Yeshou"--global user.email "[Email protected]"

We can view the configuration information through the . Gitconfig file in the root directory, where--global is added, so it is global, if other warehouse wood has a separate configuration, then the use of the global, if the other warehouses need to be configured separately, Remove--global Repeat the above operation, the same, you can see in the corresponding Warehouse folder. gitconfig file configuration information (friendly hint, this is a hidden file).

Generate Ssh-key

After we have configured the Git submitter information, we generate a local ssh-key to verify the submitter's identity.

Ssh-keygen-t rsa-c "[Email protected]"

Then locate the Id_rsa.pub file under the. ssh folder and copy it into the input box of the add process of SSH and GPG keys under GitHub's "setting" to generate the Ssh-key.

Practice on GitHub

Create a remote repository on GitHub, sign in to the GitHub site, and create a project repository called "Git-test" in the upper-right corner of "new Respository". We open the Project Warehouse home page, copy the warehouse address (since SSH is configured, then choose SSH, of course, can also be cloned via HTTP), you can use the git clone command to pull the remote repository to local.

git clone "[email Protected]:your-github-name/git-test.git]

This will pull the item to the current folder, so easy~, we can operate the local folder for the deletion and deletion of operations, listed below some of the actions.

Before you do this, you need to understand the concepts of workspaces and staging area and repository.

Workspaces : Folder contents on your computer

repository : The workspace has a hidden directory. Git, this isn't a workspace, it's a git repository. Git has a lot of stuff in its repository, the most important of which is the staging area called stage (or index), and the first branch master that git automatically creates for us, and a pointer to master called head.

Staging Area : This content exists in the hidden. Git directory, which can be simply understood as the file changes that need to be submitted are put in staging area, and then all changes to staging area are submitted at once.

Click this link for details, there is a picture of the truth Ah ~

GIT basic Operations Command

File operations

To add a file to the repository

git add test.md  // Add a test.md file

Renaming a file in the repository

git mv test.md lalala.md  // rename: TEST = Lalala

Delete a file from the repository

git rm lalala.md  //  Delete lalal.md file

View operational Records

git log  // If you feel that the information is too much, you can add the parameter "--pretty=oneline", so that you can output a short message. 

List the history operations of the master branch

List historical actions for a file

git log lalala.md

And then in the last sentence you will find that the history of the operation of the Lalala file is traced back to the past, how to do?

git log--follow lalala.md  // plus the "--follow" parameter will allow git to correlate the entire history of the content in the log

See your every action command

git reflog  // list each time you command, such as: Log, add, RM, etc.

Version fallback

git reset--hard head^  //HEAD represents the current version, plus ^ for the previous, plus ^ ^ for last ... git reset--hard commit-id  // We can get the commit ID of the specified version via Git log and return to the specified version

View Workspace Status

Git status  // can see if the file has been modified

Commit the changes to staging area

// to add a modified file // Commit the modified file and enter the modification information

Submit a file from the staging area to the remote repository

git push (Master)  // does not fill the default commit to the current branch, or to the specified branch

To view the differences between the workspace and the latest version in the Repository

git diff HEAD FileName  // when the file has not been submitted to the repository after the workspace has been modified

Discard changes made in the workspace

Git checkout--index.html   // Discard changes to index.html

To pull modifications from the remote repository to a local

git fetch (Master)  // do not fill the default to pull the latest changes to the current branch, or you can specify a branch that needs to be pulled, and the fetch does not merge and modify Git pull (master)  // the default is to pull the most recent modification of the current branch, or you can specify the branch that needs to be pulled, and then merge the changes after pull

Branching operations

Branching is the basic method of starting a separate development line in a software project, allowing development to be carried out in multiple directions simultaneously, and branching is generally mediated and merged with other branches to incorporate different developments. In layman's terms, the equivalent of a large task into a number of small tasks, divided into many people at the same time processing, and then combined together, so completed a big task.

Create a branch

Git branch dev  // creates a dev branch, but does not switch to this branch

Switch branches

git checkout dev  // switch to Dev branch

Create and switch branches

git checkout-b dev  // create a dev branch and switch to this branch

Beat up the order, and then the fart went to GitHub to refresh the branch, but no, juvenile panic, that's because you haven't submitted the branch to the remote repository ~

To place a branch in a remote repository

Git push Origin dev  // OK, submit the Dev branch to the remote repository and then run to refresh GitHub, and this time we finally see the branch .

View Branches

Git branch  // with asterisk is current branch, plus parameter "-r" to view remote minutes, "-a" view all branches

Merging branches
Note: first make clear which branch you are currently on, and you cannot merge this branch into another branch on the current branch.

git merge dev  // If I am now on the Master branch and then merge the Dev branch

Normally when merging branches, GIT uses the fast-forward mode, and we can use the parameter "--NO-FF" when merging to disable the mode

git merge--no-ff-m "" Dev  // Such a merge will create a new commit

Then the branch is merged, and then you find that the branch is still there.
Delete Branch

git branch-d dev  // Remove Dev branch git branch-d dev  // Force Delete Dev Branch

This is the problem again, go to GitHub refresh see Dev branch still exist ...
Delete Remote Branch

Git push origin--delete dev  // Delete Dev branch in remote repository

Delete a remote branch that does not exist for the corresponding local branch

Git remote prune origin

Delete local branch that does not exist for the corresponding remote branch

View the branch merge flowchart

// clearly see when to create/modify/merge branches

Label operations

A label can be simply seen as an identifying point or reference point. It is a static name that does not change over time. The branch is dynamic and moves as you commit each time. They look similar to each other, but we need to distinguish them and not confuse them.

Create a label

git tag v0.1  //  Create a label named V0.1=> version 0.1

Push tags to the remote repository

Git push Origin v0.1

Push all tags at once

Git push Origin--tags

Delete local Labels

git tag-d v0.1  // Delete a tag named v0.1

Delete a remote warehouse label

git push origin:refs/tags/v0.1

Progress staging action (similar to a game's archive)

GIT provides an "archive" feature. For example, when you are changing a function, temporarily need to modify another function, and more important, but not directly checkout discard the current changes, the GIT provides the "archive" is useful. You can save the previous changes temporarily and restore the workspace to a clean environment for you to modify another feature.

Create an "archive"

git stash  // temporarily stores the current progress of the Operation

View the list of "archives"

git stash List

View archive Content

// View archived content relative to the index, without [email protected]{index} The default is the most recent "archive"

Restore "Archive"

// Restore relative index of archived content, without [email protected]{index}, the default is the most recent "archive"

After recovery, you will find that this "archive" is still in the archive list, just like playing RPG games ~
Delete the archive

// Delete the archive content relative to the index, without [email protected]{index}, the default is the most recent "archive"

Clear All "Archive"

// after all, the drop is too tired

To configure an alias for a command

If you ask for some command trouble or too long or bad to remember, etc...

git config--global alias.cmt commit

Then you can use "Git cmt" instead of "Git commit".

Remove Alias

Open the. gitconfig file to find the contents of [alias] and delete the line contents of the corresponding alias.

. gitignore file

You can add a. gitignore file under a folder to tell Git to ignore some of the content while executing the command.

What to do: Open the. Gitignore, and add what you want to ignore on the following line.

Folders: such as "src/"//Ignore All files under the SRC folder

Files: such as "*.jpg"//Ignore files with jpg suffix

...

Ask Git for help

git help  // list of parameter lists and command lists
git help command  // Open HTML document for this command
git command--help  // Open the command's HTML document

So much for the moment ~ still continue to learn more git usage, then put out the notes if you can also share Oh ~

[Dry Goods sharing]git simple understanding and basic Operation command

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.