git commands and understanding

Preface

Git is a method used for multi-person collaborative writing projects. Detailed description is as follows


First, you need to understand what version control is

What is version control?

Version control refers to the management of changes in various program codes, documentation and other files during the software development process. It will track file changes, record the file change time, change content, and even the executor of the change, and at the same time record each stage. Add a version number to specific changes (not just changes to a file) to facilitate future review of change information at a specific stage, or even rollback

What is Git?

It belongs to the manual version controller

Save the content of different stages of the project through manual copying and add appropriate description text to distinguish it.

  • Cumbersome and error-prone
  • Generate large amounts of duplicate (redundant) data

version control tools

Complete the above manual version control behavior through programs

  • Convenient and powerful
  • Only record changes between different versions

Common version control tools

  • CVS
  • SVN
  • Git
  • ……

How does it work?

First, we need to understand two important concepts

  • state
  • area

git file life cycle

Insert image description here

state

At the same time, git provides three (or four) different record states.

  • Modified
  • Staged
  • Committed

has a special status

  • Untracked

area

git provides three different workspaces to store different content

  • Work list
  • staging area
  • Git repository

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-bkFxl9BU-1676979124415)(.\assets\areas.png)]

Install

https://git-scm.com/

Configuration

The first thing you should do after installing Git is to set your username and email address. This is important because every Git commit will use this information, and it will be written to every commit you make and cannot be changed.

git config user.name "你的姓名"
git config user.email "你的邮箱"

– global

--globalGlobal configuration information can be set through options

git config --global user.name "你的姓名"
git config --global user.email "你的邮箱"

Check configuration

# 打印所有config
git config --list
# 打印指定config
git config user.name

Create warehouse

Enter the project directory that you want to include in git version control and use git initInitialize

git init

This command will create a .gitsubdirectory named. This subdirectory contains all the necessary files in the Git repository you initialized. This directory is also one of the three areas we mentioned above. This directory is also where Git saves data records. It is very Important, do not change it easily unless necessary

Workflow and basic operations

When a project is initialized by Git, it just means that we want to use Git to manage the version records of the current project files in different periods. However, at this time, the files that already exist in the project or the files that will be added in the future will not enter version control management. , they are in 未追踪(Untracked)the state of

View the file status of your workspace

git status

What to do if garbled characters are displayed

Display garbled characters
git config --global core.quotepath false
Terminal garbled characters

Menu->Settings->Text->Local/Encoding

Or modify the configuration file

进入windoes的cmd 然后分别输入以下五行
git config --global core.quotepath false
git config --global gui.encoding utf-8
git config --global i18n.commit.encoding utf-8
git config --global i18n.logoutputencoding utf-8
set LESSCHARSET=utf-8

Execute the command after completing the input

Add workspace files to the staging area

git add

git add 1.txt
# 添加多个文件
git add 2.txt 3.txt
# 添加整个目录
git add ./a
# 添加多个目录
git add ./b ./c
# 添加所有文件
git add .

Create version

git commit

Submit the changes in the staging area to the local git repository, that is, for this work (usually a work with a specific meaning will be regarded as a version, which can be changes to multiple files)

  • Each submission will generate a 40-bit hash value as the unique ID of the submitted version.
Submit notes

Remarks are required for each submission

git commit
# 会调用默认(或自定义)的文本编辑器
Modify default editor
git config core.editor notepad

# 添加 vscode 编辑器 - mac
# 通过 vim 打开环境变量配置文件
vim ~/.bash_profile
# 添加环境变量
export PATH=/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin:$PATH
# 保存退出
source ~/.bash_profile
# 测试:在终端中直接通过命令 code 调用 vscode
git config --global core.editor "code --wait"
Single line remarks
git commit -m 备注信息

View commit log

git log

// 完整格式
git log
// 简要格式(单行)
git log --oneline

fix commit

git commit --amend

Repair (replace the previous) commit by appending the newly modified code to the previous commit without adding a new commit version

git commit --amend -m 提交

delete

git rm

# 从 git 仓库与工作区中删除指定文件
git rm 文件

# 只删除 git 仓库中的文件
git rm --cached 文件

# rm 以后,需要 commit 这次操作,否则 rm 将保留在暂存区
git commit -m 修正

Undo reset

git reset

Undo from staging area to workspace
// 从暂存区中撤销一个指定文件
git reset HEAD 文件名称
// 从暂存区中国年撤销所有文件
git reset HEAD .
This command can be used to roll back the version
# 回退到指定的 commitID 版本
git reset --hard commitID

Compare

# 比较 工作区和暂存区
git diff 文件 
# 比较 暂存区和仓库
git diff --cached [commitId] 文件
# 比较 工作区和仓库
git diff commitId filename
# 比较 仓库不同版本
git diff commitId1 commitId2

branch

Our development is like a game task, which is developed on the main line (master) by default. Many times, there are various side tasks, and git supports us to create branches for project development.

View branches

git branch

Create a branch

git branch 分支名称

switch branch

git checkout 分支名称
# 也可以使用 checkout -b 来新建分支
git checkout -b 分支名称

Branch merge

# B 合并到 A,需要切换到 A 分支
git merge 被合并分支

# 查看已经合并的分支
git branch --merged
# 查看未合并的分支
git branch --no-merged

delete branch

# 如果分支为未合并状态,则不允许删除
git branch -d 分支名称
# 强制删除
git branch -D 分支名称

Merge records

rebase

# 合并 HEAD 前两个祖先记录
git rebase -i HEAD~2
~ and ^

~ : portrait

^ : landscape

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-3d94WqTi-1676979124417) (assets/path.png)]

rebase operation
# p, pick = use commit => 使用
# r, reword = use commit, but edit the commit message => 使用,但重新编辑说明
# e, edit = use commit, but stop for amending => 使用
# s, squash = use commit, but meld into previous commit => 使用,但合并上一次
# f, fixup = like "squash", but discard this commit's log message => 就像 squash 那样,但会抛弃这个 Commit 的 Commit message
# x, exec = run command (the rest of the line) using shell => 执行脚本
# d, drop = remove commit => 移除
git rebase -i HEAD~3
# 弹出编辑器,根据需要的进行修改,然后保存
# 如果为 r,s 则会再次弹出编辑器,修改新的 commit message,修改之后保存

If there are some problems, you can re-edit and save through git rebase --edit-todoandgit rebase --continue

merge conflicts

Sometimes, different branches may operate on the same file content and location, which may cause conflicts during the merge process.

  • View conflict files
  • Fix conflicting content
  • submit

Label

Sometimes, we want to put some labels on a specific historical submission

New tag

git tag -a v1.0.0 HEAD/commitId

View tag

git tag

Collaborative development

All the above operations are established locally. If we want to carry out collaborative team development, then at this time, we need to share the git warehouse information with everyone in the team.

  • Distributed - Centralization and Decentralization

github

First register an account

Use ssh link

SSH

https://help.github.com/cn/articles/connecting-to-github-with-ssh

https://help.github.com/cn/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

Generate SSH key
ssh-keygen -t rsa -C "[email protected]"
Add proxy

Use ssh-addthe agent, if it is not started, you can start it manually

eval $(ssh-agent -s)
Add private key
ssh-add 私钥路径
Add public key on github

Personal Center->Settings->ssh->Add

test
ssh -T [email protected]

git remote

Link
git remote add origin [email protected]:miaov-zmouse/kkb-test.git






View remote commits
git remote -v
Submit (sync) remote

Synchronize local warehouse to remote

git push origin -u master
# -u 简化后续操作
git push origin master
remote branch
# 提交到远程(分支)
git push origin [本地分支名称]:[远程分支名称]

# 远程先创建好分支然后拉取到本地
git checkout -b [本地分支名称] origin/[远程分支名称]

# 拉取远程分支到本地
git pull origin [远程分支名称]:[本地分支名称]

# 查看远程仓库
git remote show origin

# 查看本地分支
git branch

# 查看远程分支
git branch -r

# 查看所有分支
git branch -a

# 删除本地分支
git branch -d [本地分支名称]

# 删除远程分支
git push origin --delete [远程分支名称]
# or
git push origin :[远程分支名称]

# 设置默认提交分支
git branch --set-upstream-to=origin/[远程分支名称] [本地分支名称]
delete branch

git remote remove origin

Extension: Workflow - git work flow

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-tqWGKVF3-1676979124418) (assets/gitflow.png)]

GUI tools

https://git-scm.com/download/gui/win

  • Sourcetree
  • other editor

Summary:
In summary, git is divided into two methods. One is to enter the command with cmd, and the other is to download the plug-in "Git History" in the compiler and use a graphical tool.
Git is completely self-taught.

Guess you like

Origin blog.csdn.net/www61621/article/details/129148601