Learn Git, this one is enough!

Foreword

When first learning git is the article on the website to see the teacher Liao Xuefeng, a lot of content, it will be used after the completion of the basic operations.

After this long holiday, one use git's also a number of hand is enough.

I am sure you have heard the word, the more books read, the more thin, really said nothing wrong.

Learn anything, when the school a second time harvest is far greater than the first pass of learning.

Whether you are first learning git, or a second look, this article contains some of the operations you frequently used.

Know a concept, it is important! ! !

What is version control?

Look at the introduction Baidu Encyclopedia.

Version control is the software development process in a variety of program code, configuration files and documentation and other documents change management, it is software configuration management , one of the core idea.

Version control is the main function of the change tracking file . When it did it, who changed what file the information faithfully recorded. Every change file, the version number of the file will increase. In addition to record version changes, another important feature of version control is developed in parallel . Software development is often more than collaboration, version control can effectively address the development of communication problems between different versions of the synchronization and developers to improve the efficiency of collaborative development. The most common concurrent development of different versions of the software error (Bug) fix the problem can be effectively solved through version control branching and merging methods.

In simple terms is more than techniques for managing collaborative development projects.

Common version control tools

Now undoubtedly the most popular Git, SVN.

After you learn git, SVN believe you can get started very quickly.

The main difference between Git and SVN

SVN is centralized version control system, the repository is focused on the central server, and when the work, are used in their computers, so the first central server to get the latest version from then work after the completion of the work, need his own to finish the job on a central server. Centralized version control system must be networked to work, high network bandwidth requirements.

Git is a distributed version control system, no central server, each person's computer is a complete repository, when the work does not require networking, because the versions on their computers. Collaborative is this: for example it ourselves on a computer file A, the others are changed A file on the computer, then, you just need to give each other their own changes pushed between the two, we can see each other to each other's changed. Git can directly see what code updates and files!

Git is the world's most advanced distributed version control system.

Git installation

Open git official website ; download the corresponding version.

Despite its small size, but the official website to download more slowly. At this time we can go to Taobao image download.

Find the corresponding version installed.

Fool a key to install, you can not say here.

Start Git

After a successful installation in the Start menu items have Git, there are three programs under the menu: Any folder can also see a corresponding right program!
Here Insert Picture Description
** Git Bash: ** Unix and Linux-style command line, use up to a maximum recommended

** Git CMD: ** Windows style command line

The GUI Git : GUI Git, not recommended

Linux common commands

Use git, we need to master common Linux commands.

For Linux command, do not hold Bird Brother private kitchens and other heavy book or document knock, use more practice, more practice, more practice! ! !

1. cd 改变目录
    
2. cd .. 回退到上一个目录
    
3. pwd 显示当前所在的目录路径
    
 4. ls(ll):  都是列出当前目录中的所有文件,只不过ll(两个ll)列出的内容更为详细。
    
5. touch : 新建一个文件 如 touch index.js 就会在当前目录下新建一个index.js文件。
    
6.rm:  删除一个文件, rm index.js 就会把index.js文件删除。
    
7. mkdir:  新建一个目录,就是新建一个文件夹。
    
8. rm -r :  删除一个文件夹, rm -r src 删除src目录
    
rm -rf / 切勿在Linux中尝试!删除电脑中全部文件!
    
9. mv 移动文件, mv index.html src。index.html 是我们要移动的文件, src 是目标文件夹,当然, 这样写,必须保证文件和目标文件夹在同一目录下。

10. reset 重新初始化终端/清屏。

11. clear 清屏。

12. history 查看命令历史。

13. help 帮助。

14. exit 退出。

15. #表示注释

Git Configuration

All configurations are preserved locally.

View Configuration

git config -l

Here Insert Picture Description
View different level configuration file:

#查看系统config
git config --system --list  
    
#查看当前用户(global)配置
git config --global  --list

Here Insert Picture Description
Here Insert Picture Description

Git-related configuration files:

  1. Git \ etc \ gitconfig: Git installed gitconfig --system system directory level

  2. C: \ Users \ Administrator \ .gitconfig --global global configuration applies only to the currently logged in user
    Here Insert Picture Description

Of course, you can also edit this file directly here.

Set the user name and mailbox

Before you use you want to set up your user name and mailbox. Necessary, it is the user ID.

1. git config --global user.name "程梦月"  #名称
2. git config --global user.email 1127397156@qq.com   #邮箱

(-Global global configuration)

Set it once, when you git each submission will be embedded in your submission.

Git basic theory (very important)

Three regions

Local Git has three work areas: working directory (Working Directory), the staging area (Stage / Index), resource library (Repository or Git Directory). If you add a remote git repository (Remote Directory) can be divided into four work areas. File conversion relationship between these four areas as follows:img

  • Workspace: the workspace is where you usually store the project code
  • Index / Stage: staging area for temporary storage of your changes, in fact it's just a file, save the file to the list of information to be submitted
  • Repository: warehouse district (or local warehouse), where the data is secure, and there are you committed to all versions of the data. HEAD which points to the latest version into the warehouse
  • Remote: remote repository, managed code server, you can simply considered the project team in a computer for remote data exchange

The three local area precisely, it should be pointed git repository HEAD version:

img

  • Directory: Use a Git directory management, which is a warehouse that contains our work space and Git management space.
  • WorkSpace: the need for directory and file version control via Git, these directories and files in the workspace.
  • .git: Git storage directory management information, initialization is automatically created when the warehouse.
  • Index / Stage: staging area, or call the update area to be submitted, before submitting to enter repo, we can put all the updates in the staging area.
  • Local Repo: local warehouse, stored in a local repository; HEAD will be just the current development branch (branch).
  • Stash: hidden, it is a working state saving stack used to save / restore temporary state of WorkSpace.

work process

git workflow is generally like this:

1, in the working directory to add, modify files;

2, will need to be versioned file into the staging area;

3, will be submitted to the staging area file git repository.

Therefore, git managed file has three states: modified (modified), has been staging (staged), has been submitted (committed)

img

Git project to build

Use of this holiday very much.

Remember the following commonly used commands that a picture can be.

There is a remote, designed to remote repository, here is not to explain how to build a warehouse on github code or a cloud. You can refer to this blog post

img

After completion of the challenge of building a warehouse on github here before I attached a page.

How to upload local files to a remote github repository glance.
Here Insert Picture Description

  1. git init
  2. git add .
  3. git commit -m "message content"
  4. git remote add origin remote warehouse address
  5. git push -u origin master (you can also write git push)

If what you want to clone a remote repository, quite simply, one can be.

git clone [url]

Git file operations

Four states file

Version control is the version control for files that you want to modify the file, submitted to other operations, you must first know what the current state of the file, or may be submitted now do not want the documents submitted, or not submitted the documents to be submitted on.

  • Untracked: untracked, this file in the folder, but has not added to the git repository, version control is not involved by git add state to Staged..
  • Unmodify: file has been put in storage, unmodified, that is exactly the same file folder contents of the snapshot file repository for this type of files in two places, if it is modified, it becomes Modified If you use git rm removed. repository, the files become Untracked
  • Modified: file has been modified, to modify only, and no other operations that also has two destination file, accessible by git add temporary staged state, using the modified git checkout is discarded, returned to the state unmodify this git checkout. that is, remove the document from the library, covering the current modification!
  • Staged:. BSR execute git commit will be synchronized to the library, then the library file and modify local files and becomes consistent, the file is Unmodify state execution git reset HEAD filename cancel the temporary file status is Modified.

Viewing File Status

At any time to view the status of the file by the following command.

git status

Ignoring Files

Sometimes we do not want certain files under version control, such as database files, temporary files, design documents, etc.

Establish ".gitignore" file in your home directory, this file has the following rules:

  1. Ignore blank lines in the file or line with a pound sign (#) to start will be ignored.
  2. Linux can use wildcards. For example: (?) The asterisk (*) represents any number of characters, the characters represent a question mark, the square brackets ([ABC]) represents optionally character range, curly braces ({string1, string2, ...}) represent a selectable character strings and so on.
  3. If the name of the front there is an exclamation point (!), It represents an exception rule, will not be ignored.
  4. If the name of the front is a path separator (/), pledged to ignore files in this directory, and subdirectories are not ignored.
  5. If the last name is a side path separator (/), he said to ignore this directory is the subdirectory name, not the files (the default file or directory are ignored).
#为注释
*.txt        #忽略所有 .txt结尾的文件,这样的话上传就不会被选中!
!lib.txt     #但lib.txt除外
/temp        #仅忽略项目根目录下的TODO文件,不包括其它目录temp
build/       #忽略build/目录下的所有文件
doc/*.txt    #会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

git branch operation

# 列出所有本地分支
git branch

# 列出所有远程分支
git branch -r

# 新建一个分支,但依然停留在当前分支
git branch [branch-name]

# 新建一个分支,并切换到该分支
git checkout -b [branch]

# 合并指定分支到当前分支
$ git merge [branch]

# 删除分支
$ git branch -d [branch-name]

# 删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

If the same file when merging branches have been modified will cause conflict: The solution is that we can modify and resubmit conflicting files! Select to preserve his code or your code!

master the main branch should be very stable, to release a new version, are not allowed to work in the above general, work on the new dev branch work under normal circumstances, after work, for example, to publish, or dev branch code can be stable after the combined master up to the main branch.

About this operation, you can find a small partner simulate what people collaborate together.

Note: some of the figures is mad God says public number.

Published 272 original articles · won praise 19 · views 20000 +

Guess you like

Origin blog.csdn.net/hello_cmy/article/details/105014049