Git introduction + common operation commands

Introduction to Git:

Git is a free, open source distributed version control system for agile and efficient handling of any project, small or large.
Git is an open source distributed version control system that can effectively and high-speed handle project version management from small to very large. Git is an open source version control software developed by Linus Torvalds to help manage Linux kernel development.

advantage

  1. Suitable for distributed development, emphasizing individuals;
  2. Public server pressure and data volume will not be too large;
  3. Fast and flexible;
  4. work offline;

shortcoming

  1. Code confidentiality check, all code and version information can be fully disclosed once the developer clones the entire library;
  2. Unfriendly access control;

The difference between Git and SVN

SVN is a centralized version control system, while Git is a distributed version control system. For the difference between Git and SVN, please refer to the difference between Git and SVN. The difference between Git and SVN

Git workflow

  1. Clone Git resources from remote warehouses as local warehouses;
  2. Check out the code from the local warehouse and then modify the code;
  3. Submit the code to the staging area before submitting to the local warehouse;
  4. Submit the modification and submit it to the local warehouse; save the various historical versions of the modification in the local warehouse;
  5. When you need to share code with team members, you can push the modified code to the remote warehouse.

Git workflow flow chart

insert image description here

Several core concepts of Git

Workspace, staging area, version library, remote warehouse

One difference between git and other version control systems such as svn is the concept of a staging area.
insert image description here
Workspace : The workspace is where you usually store the project code

Index / Stage : Temporary storage area, used to temporarily store your changes, in fact it is just a file, save the information about to be submitted to the file list

Repository : The warehouse area (or version library) is the place where data is safely stored. It contains the data you have submitted to all versions. Where HEAD points to the latest version put into the warehouse

Remote : The remote warehouse, the server hosting the code, can be simply considered as a computer in your project team for remote data exchange

the branch

Every commit Git strings them into a timeline, and this timeline is a branch. So far, there is only one timeline. In Git, this branch is called the main branch, that is, the master branch. Strictly speaking, the HEAD pointer does not point to the submission, but to the master, and the master points to the submission.
At the beginning, the master branch is a line. Git uses master to point to the latest submission, and then uses HEAD to point to the master, so that the current branch and the submission point of the current branch can be determined: each time a submission is made, the master branch will move forward one step
insert image description here
. In this way, with the continuous submission, the line of the master branch is getting longer and longer.

When we create a new branch, such as dev, Git creates a new pointer called dev, which points to the same commit as the master, and then points HEAD to dev, which means that the current branch is on dev: Git creates a branch very quickly, because in addition to adding
insert image description here
a Dev pointer, change the HEAD pointing, the files in the workspace have not changed!

But after switching to the dev branch, the modification and submission of the workspace is aimed at the dev branch. For example, after a new submission, the dev pointer moves forward one step, while the master pointer remains unchanged: if our work on dev is completed, we
insert image description herecan Merge dev into master. How does Git merge? The easiest way is to directly point the master to the current commit of dev, and the merge is completed:
insert image description here
so Git merges branches very quickly! Just change the pointer, and the content of the work area will not change!

You can even delete the dev branch after you're done merging the branches. Deleting the dev branch is to delete the dev pointer. After deletion, a master branch is left:
insert image description here

The local project git initializes and submits the remote library

1. First create a project in a remote warehouse (such as github) to avoid mistakes and do not initialize README, license, gitignore files.

2. Open the Terminal terminal

3. Switch to your local project directory

4. Initialize the local warehouse

$ git init 

5. Add files to the local warehouse

$ git add . 

6. Submit documents

$ git commit -m "First commit"

7. Add the remote warehouse address to the local warehouse

$ git remote add origin {
    
    远程仓库地址}

8. Push to the remote warehouse

$ git push -u origin master

clone operation command

1. Configure the account and mailbox of the local warehouse

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

2. In order to avoid the need to enter a password for each remote access, ssh will be used to log in. ssh should be bound with native info. Check whether there is a .ssh folder in the C:\Users\Administrator directory of your computer . If not, it needs to be generated.

$ ssh-keygen -t rsa -C "[email protected]"

3. clone command

$ git clone git@github.com:michaelliao/gitskills.git

4. Pull the code on the specified branch

git clone -b dev http://xxx.git

Press Enter all the way to the end.

Branch operation command

1. View the local branch

git branch

2. Create a new branch

git checkout -b new-branch  //创建并切换到分支feature-branch并切换新分支

3. View remote branches

git branch -a //列出所有分支名称

4. Push the local branch to the remote branch

git push origin my-branch:new-branch //推送本地的my-branch(冒号钱面的)分支到远程origin的new-branch(冒号后面的)分支(没有会自动创建)

5. When we git push new-branch, an error occurs, indicating that the remote warehouse cannot be associated. At this time, execute the command

git push origin HEAD:new-branch //关联远程仓库

6. Delete the remote branch

git push origin :xn  //将一个空分支推送到远程即为删除
git push origin --delete cn //功能同上

Focus: Introduction to process operation commands

git checkout -b dev origin/dev  //作用是checkout远程的dev分支,在本地起名为dev分支,并切换到本地的dev分支
git checkout -b release origin/release  //checkout远程的release分支,在本地起名为release分支,并切换到本地的release分支
git checkout dev  //切换回dev分支,并开始开发
git init //初始化当前目录为git仓库,该目录下会生成.git文件,一般情况不修改这个文件
git remote add origin git@xxx //连接github
git fetch //fetch 后可看见远程相关分支信息
//(此时,git branch  看不到任何本地分支信息,本地master分支虽然默认分支,但需第一次commit后才会真正存在)
//在当前目录下随便建一个文件 test.txt
git add test.txt //add 文件到暂存区
git commit -m"first commit for init"   //提交到本地版本库
git branch -a //此时,可以看见本地和远程所有的分支信息
git branch  dev //本地建立一个分支
git branch --set-upstream-to=origin/dev dev //将远程分支dev和本地分支dev关联
git pull //可拉取远程分支dev上的代码到本地并由本地分支dev管理 ;若远程和本地分支没有做关联,pull需要指定远程和本地分支信息
git  rm  test.txt //删除本地版本库中的test.txt文件,工作区的文件可手动删除

Guess you like

Origin blog.csdn.net/qq_42697806/article/details/119567667