Front-end Xiaobai's learning path--git learning

Introduction

git is a distributed version control system

Centralized and distributed: Centralized has a central server, which requires network operation. Distributed, everyone has a repository

1. Install git

  • Download git-> find git bash after the installation is complete, open is a command line (indicating successful installation)

  • Enter the following on the command line

引号内部填自己信息
$ git config --global user.name "Your Name" 
$ git config --global user.email "[email protected]"

2. Create a repository

Repository is also known as warehouse, English namerepository

use

Git has three areas, work area, temporary area, and version library. The workspace enters the temporary storage area after add, and the files in the temporary storage area enter the repository after the commit

1. Add

Note: The following file names are suffixed

git add "文件名"

2. Submit

Note: Submit is to submit all the files in the temporary storage area to the repository

git commit -m "操作名称"

3. Check the status

git status

4. View the contents of the file

cat "文件名"

5. View git logs

git log 

Such as: Commit is followed by the version number, randomly generated

commit 980993de744a8886692ed79fc7f4cdd00ec15959 (HEAD -> master)
Author: zouyou <>
Date:   Sat Apr 11 14:35:04 2020 +0800

    update commit

commit ea799348f2b1ccc7d046f8bd61c6b193b79bdc18
Author: zouyou <>
Date:   Sat Apr 11 14:33:07 2020 +0800

    git tracks changes

-Rollback version :

  1. Use HEAD
git reset --hard HEAD^   HEAD^:上个版本  HEAD^^:上上版本
  1. Use the version number, just write the first few numbers
git reset --hard ea799348f 

If you have forgotten the version number, you can use reflog to view the history changes, regardless of whether there is a rollback

git reflog

6. View file changes

git diff

7. Undo the modification

  1. Undoing the modification of the work area is equivalent to directly operating the file and undoing the changes.
git restore 文件名
  1. Cancel the submission of the temporary storage area and return to the work area, but the file is still modified. To cancel the modification of the file, repeat 1 step
git resore --staged 文件名

8. Delete files

  1. To delete the workspace and version library at the same time, you can delete the workspace manually. . The following is to delete the repository
git rm 文件名
  1. Deleted the workspace file by mistake, but it has been uploaded to the repository, and can be restored through the repository
git checkout 文件名

9. Remote warehouse: github

  1. Create a repository on github, remember the ssh code or https address
  2. Enter the following to create a remote connection, there will be input account password
$ git remote add origin (ssh码)/(hhtps)
  1. Upload the file to github
$ git push -u origin master

Note: Since the remote library is empty, when we first pushed the masterbranch, with the added -uparameters, Git will not only masterpush the remote new masterbranch of the local branch content , but also associate the local masterbranch with the remote masterbranch. The commands can be simplified when pushing or pulling in the future.

After the upload is complete, as long as the local commit is made, you can use the following command

git push origin master //这里与上面是差不多一样的语法

Summary: To associate a remote library, first create a library remotely, remember the SSH code, and then connect git remote add origin ssh码

Upload: The first time you upload git push origin -u master, and then it do not add -uupgit push origin master

In these commands, origin is actually the name of the remote warehouse, usually named origin

  1. Clone from remote repository to local

First find a repository on github and copy its ssh code, enter the command line on git bash

git clone ssh码

Note: The git bash opened in which folder is cloned under which folder.

10. Branch management

1. Branch Introduction

The branch can be implemented similar to the development version of the system, and then pushed to the trunk after completion, which is a stable system. As follows, the two branches are merged into the trunk

learn-branches

In git, the masterbranch is the trunk. At the beginning, it masterpoints to this main line, headpointing master, so it headacts as a pointer to the current point, which can point to the trunk or to the branch.

git-br-initial

View branches, display all branches, there will be * before the current branch

git branch
2. Branch creation

Create a branch devand switch to that branch -b's role is to create and switch

git checkout -b dev
等价于
git branch dev  //创建分支dev
git checout dev // 切换到分支dev

Note: The working area, staging area, and version library of each branch are independent, and will be different after switching branches.

Git encourages heavy use of branches:

View branch:git branch

Create a branch:git branch

Switch branches: git checkout orgit switch

Create + switch branch: git checkout -b orgit switch -c

Merge a branch into the current branch:git merge

Delete the branch:git branch -d

View the branch diagram:git log --graph

- File Conflict

git-br-feature1

In this case, the two branches cannot be merged and must be resolved manually.

$ git merge feature1
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

git statusYou can also display conflicting files. You can see the mark when you open the conflicting file.

Git uses <<<<<<<, =======to >>>>>>>mark the contents of different branches

3. The bug branch

When a work is being done on the branch dev, suddenly a bug is to be fixed (such as on the trunk), so it is necessary to hide the work being done by dev first and fix the bug first. Git also provides a stashfunction to "storage" the current work site and continue working after restoring the site later:

git stash //可以将在暂存区的文件(还未提交)隐藏,使用status也看不到

You can query hidden things in stash

git stash  //=>例如:stash@{0}: WIP on dev: f52c633 add merge

After fixing the master bug, restore the dev site:

git stash aplly //对现场进行恢复,此时stash里还有,需要进一步
git stash drop //对stash里的东西删除
同时还可以从stash里一步步恢复:git stash apply stash@{0}
等价于一句话
git stash pop

Note: Since dev is originally a branch of master, then dev may also have a bug that the trunk has just fixed, so git has a function to implement bug repair and replication. This can be introduced, you can solve the bug in the branch and copy it in the trunk

git cherry-pick <版本号>
这里的版本好就是刚刚在主干修复bug后commit之后的版本号

To develop a new feature, it is best to create a new branch, similar to the bug branch;

If you want to discard a branch that has not been merged, you can git branch -D delete it by force.

4. Remote cooperation

To view the information of the remote library, use

git remote //通常远程库是origin

Can also be used git remote -v, can return the address of crawling and pushing, as follows

origin  https://github.com/zy116/first-blog.git (fetch)
origin  https://github.com/zy116/first-blog.git (push)

-Push branch

git push origin dev //推送分支
git push origin master //推送主干

However, it is not necessary to push the local branch to the remote, so which branches need to be pushed and which ones do not?

  • masterThe branch is the main branch, so it must be synchronized with the remote at all times;
  • devThe branch is a development branch, all members of the team need to work on it, so it also needs to be synchronized with the remote;
  • The bug branch is only used to fix bugs locally, so there is no need to push to the remote, unless the boss wants to see how many bugs you have fixed each week;
  • Whether the feature branch is pushed to the remote depends on whether you are working on it with your little partner.

In short, in Git, the branch can be hidden and played locally, whether it is pushed, depending on your mood!

-Grab the branch

Usually in masterand devbranches are pushing their own changes when a person put their branches are pushed up, down another clone can only see the master branch, if you want to devdevelop on a branch, we must establish a remote to a local branch

git switch -c dev origin/dev

When another person pushes something, you also have to push, and at this time there will be conflicts. Another person's latest submission conflicts with the submission you tried to push.

At this time, you will be prompted to git pulloperate, but you will be prompted for errors.

You need to specify the local devbranch and remote origin/devlink branch, set up devand origin/devlinks

 git branch --set-upstream-to=origin dev

Pull again

git pull

At this time, the pull is successful and will be marked in the conflict file, which needs to be resolved manually

Therefore, the working mode of multi-person collaboration is usually like this:

  1. First, you can try to git push origin <branch>push your own modifications;
  2. If the push fails, because the remote branch is newer than your local, you need to git pulltry to merge first ;
  3. If there is a conflict in the merger, resolve the conflict and submit it locally;
  4. After there is no conflict or the conflict is resolved, git push origin you can use push to succeed!

If git pullprompted no tracking information, the link relationship between the local branch and the remote branch has not been created. Use the command git branch --set-upstream-to origin/.

Summary :

  • View remote library information, use git remote -v;
  • If the newly created branch is not pushed to the remote, it will be invisible to others;
  • Push the branch from the local, use git push origin branch-name, if the push fails, first git pullgrab the remote new submission;
  • Create a branch corresponding to the git checkout -b branch-name origin/branch-nameremote branch locally, and use the same name for the local and remote branches;
  • Establish the relationship between the local branch and the remote branch, use git branch --set-upstream branch-name origin/branch-name;
  • Fetch the branch from the remote and use it git pull. If there is a conflict, the conflict must be dealt with first.

Thanks to Liao Xuefeng's git tutorial, some of the text is directly copied, the original tutorial is here git tutorial

Guess you like

Origin www.cnblogs.com/zy116/p/12682100.html