[git] study notes

git uses

1 installation (omitted)

2 configuration

Configure name and email

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

3 Use git:

View the status of the current warehouse

git status

Initialize warehouse

git init

file status:

  1. not tracked
  2. tracked
  3. Temporary storage
  4. unmodified
  5. already edited

Untracked → Staging

git add <filename> 将文件切换到暂存的状态
git add * 将所有已修改(未跟踪)的文件暂存

Staging → Unmodified

git commit -m "xxxx" 将暂存的文件存储到仓库中
git commit -a -m "xxxx" 提交所有已修改的文件(未跟踪的文件不会提交)

Unmodified → Modified

After modifying the code, the file will become modified

4 Commonly used commands

reset file

git restore <filename> # 恢复文件
git restore --staged <filename> # 取消暂存状态

Delete Files

git rm <filename> # 删除文件
git rm <filename> -f # 强制删除

move files

git mv from to # 移动文件 重命名文件

5 branches

When git stores files, each code submission will create a corresponding node, and git records the status of the code through nodes one by one. The nodes will form a tree structure, which means that the tree will have branches. By default, the warehouse has only one branch, named master. When using git, multiple branches can be created, and the branches are independent of each other. Modifying the code on one branch will not affect other branches.

git branch # 查看当前分支
git branch <branch name> # 创建新的分支
git branch -d <branch name> # 删除分支
git switch <branch name> # 切换分支
git switch -c <branch name> # 创建并切换分支
git merge <branch name> # 和并分支

In development, code is written on its own branch. After the code is written, its own branch is merged into the main branch.

6 Rebase (rebase)

In development, in addition to merging branches through merge, you can also complete the merging of branches through rebase.

When we merge branches through merge, all the process of branch creation and branch merging will be displayed in the commit record, so when the project is more complex and the development process is more twists and turns, I have to repeatedly create, merge, and delete branches . This will make the commit record of our code extremely confusing.

Rationale (what happens when rebasing):

  1. When we initiate a rebase, git will first find the nearest common ancestor of the two branches
  2. Compare the history commits of the current branch relative to the ancestors, and extract them into a temporary file
  3. point the current section at the target's base
  4. Starting from the current base, re-execute the history operation

Rebasing and merge end up being the same for merging branches! But rebasing will make the code commit record cleaner and clearer! Notice! Merging and rebasing are interchangeable in most cases, but if the branch has already been submitted to a remote repository, try not to rebase at this time.

7 remote warehouse (remote)

At present, all my operations on git are performed locally. Obviously this cannot be done in development, then we need a remote git repository. There is no difference between the remote git warehouse and the local essence. The difference is that the remote warehouse can be accessed and used by multiple people at the same time, which is convenient for us to develop collaboratively. In actual work, git servers are usually built by the company for internal use or some public private git servers are purchased. During our learning phase, we directly use some open public git repositories. At present, there are two commonly used libraries: GitHub and Gitee (code cloud)

Upload the local library to git:

git remote add origin https://github.com/lilichao/git-demo.git
# git remote add <remote name> <url>

git branch -M main
# 修改分支的名字的为main

git push -u origin main
# git push 将代码上传服务器上

git push origin main:hello # 不想推给main 把本地的main分支推送给origin的hello分支

Upload the local library to gitee:

git remote add gitee https://gitee.com/ymhold/vue-course.git
git push -u gitee main

8 Commands for remote library operations

git remote # 列出当前的关联的远程库
git remote add <远程库名> <url> # 关联远程仓库
git remote remove <远程库名>  # 删除远程库
git push -u <远程库名> <分支名> # 向远程库推送代码,并和当前分支关联
git push <远程库> <本地分支>:<远程分支> # 推送到远程库指定的分支
git push origin main:hello # 不想推给main 把本地的main分支推送给origin的hello分支
git clone <url> # 从远程库下载代码

git push # 如果本地的版本低于远程库,push默认是推不上去
git fetch # 要想推送成功,必须先确保本地库和远程库的版本一致,fetch它会从远程仓库下载所有代码,但是它不会将代码和当前分支自动合并
		 # 使用fetch拉取代码后,必须要手动对代码进行合并
         git merge hello # 这是与本地分支合并的写法
         git merge origin/master # 这是与远程库分支合并的写法
git pull  # 从服务器上拉取代码并自动合并 

Note: Before pushing the code, be sure to pull the latest code from the remote library

9 tags

It has no effect, it is to label the commit node, so that we can find a certain node

  • When the head pointer does not execute the head of a certain branch, this state is called HEAD detached, and the operation code can also be operated in the state of the detached head pointer, but these operations will not appear on any branch. So be careful not to operate the warehouse in the state of separating the head pointer.
git switch <节点id> --detach # 让head指针回到指定节点(就是我们看到的代码
git switch master # 回到master
  • If you have to go back to the later node to operate the code, you can choose to create a branch and then operate

    git switch -c <分支名> <提交id>
    
  • You can set labels for the submission records. After setting the labels, you can quickly identify different development nodes through the labels:

    git tag
    git tag 版本
    git tag 版本 提交id
    git push 远程仓库 标签名
    git push 远程仓库 --tags # 推送所有标签
    git tag -d 标签名 # 删除标签
    git push 远程仓库 --delete 标签名 # 删除远程标签
    
    code . 可以在vscode中快速打开该文件 # 一个小技巧
    

10 gitignore

By default, git will monitor all content in the project, but some content, such as the content in the node_modules directory, we don't want it to be managed by git. We can add a file in the project directory .gitignoreto set those files that need to be ignored by git.

Write a file per line and the file will be ignored

#You can write comments later

11 github's static page

In github, you can deploy your own static pages to github, which will provide us with an address to make our page a real website, which can be accessed by users.

Require:

  • The branch of static pages must be called: gh-pages
  • If you want the page to be accessible through xxx.github.io, you need to configure the name of the library as xxx.github.io

12 docs

An open source static content management system launched by facebook, through which a static website can be quickly deployed, based on react

use:

  • URL:

    • https://docusaurus.io/
  • Install

    • npx create-docusaurus@latest my-website classic
  • Startup project

    • npm startoryarn start
  • build project

    • npm run buildoryarn build
  • Configuration items:

    • docusaurus.config.js project configuration file
  • Add page:

    • In the docusaurus framework, pages are divided into three types: 1.page, 2.blog, 3.doc

Guess you like

Origin blog.csdn.net/m0_55644132/article/details/128255107