[Git] Git use (nanny-level explanation)

1. The first installation and use

Git download address: https://git-scm.com/download

1.1 Configure username and email

This is very important, because every Git commit will use this user information.

set up

​ Enter the following command in git bash.

git config --global user.name ""
git config --global user.email ""

view information

git config --global user.name
git config --global user.email

1.2 Configure aliases for common commands (optional)

​ It can simplify entering too long commands each time.

Create .bashrca file

​ User directory location under windows system:C:\Users\用户名

touch ~/.bashrc

Using aliasthe command : alias the command

​ You can use the vi/vim editor to edit in git bash, or you can directly open the editor locally.

#用于输出git提交日志
alias git-log='git log --pretty=oneline --all --graph --abbrev-commit'
#用于输出当前目录所有文件及基本信息,ll命令本身等同于 ls -l
alias ll='ls -al'
#git存储到本地仓库
alias git-local='func(){ git add .; git commit -m $1; };func'

take effect

​ The .bashrc created in the home directory of the windows system does not need to execute sourcethe command the command needs to be executed under the windows system .

source ~/.bashrc

problems encountered

​ If ~/.bashrcit is created for the first time, reopening git bash may display the following information, ignore it, it just generates a new ~/.bash_porfilefile in the home directory.

2. Create a new local warehouse

Select/create an empty directory locally as a local Git repository

In git bash type

git init

If the creation is successful, you can see the hidden .git directory under the folder.

3. Basic operation instructions

​ There are several states for file modification (addition, deletion, update) in the Git working directory, and the state of these modifications will change as we execute Git commands.

Use the following commands to switch files between these three states:

  • git add (workspace --> staging area)
  • git commit (temporary storage area --> local warehouse)

3.1 View the modified status (status)

​ Role: view the modified status (temporary storage area, work area)

git status

insert image description here
insert image description here
insert image description here

3.2 Add the workspace to the temporary storage area (add)

​ Role: Add the modification of one or more files in the workspace to the temporary storage area

git add 单个文件名|通配符

3.3 Submit the staging area to the local warehouse (commit)

​ Role: Submit the contents of the temporary storage area to the current branch of the local warehouse

git commit -m "注释内容"

3.4 Check the submission log (log)

​ Role: View submission records

git log [option]

options

  • --all: show all branches
  • --pretty=oneline: display the commit message as one line
  • --abbrev-commit: make the output commitId shorter
  • –graph: display in the form of a graph

​ The best viewing effect is to select all the options, that is, to alias the command earlier git-log, namely:

git log --pretty=oneline --all --graph --abbrev-commit

3.5 version fallback

​ Role: version switching

# commitID 可以使用 git-log 或 git log 指令查看
git reset --hard commitID
# 查看已经删除的提交记录
git reflog

3.6 Adding files to the ignore list (.gitignore file)

​ Role: The specified files do not need to be included in the management of Git, that is git add, git committhese files will not be put into the temporary storage area and warehouse during execution and .

Create a .gitignore file in your working directory

List file patterns to ignore

# 忽略所有以 .a 结尾的文件
*.a
# 一定要管理 lib.a 文件,即使上面忽略所有以 .a 结尾的文件
!lib.a
# 忽略当前目录下的 TODO 文件,子目录下的 TODO 文件照常管理
/TODO
# 忽略 build/ 目录下的所有文件
build/
# 忽略 doc/ 目录下所有以 .txt 结尾的文件,但子目录下的 .txt 文件照常管理
doc/*.txt
# 忽略 doc/ 目录下的所有 .pdf 文件(包括子目录)
doc/**/*.pdf

3.7 Possible problems

(1) Save the workspace to the temporary storage area (add command):

Link

warning: LF will be replaced by CRLF in xxx.
The file will have its original line endings in your working directory

Reason : Caused by the difference in line breaks in the file. This prompt means: It will convert the windows format (CRLF (that is, carriage return and line feed)) to Unix format (LF). These are warnings for converting file formats and will not affect the use. git supports LF by default. Git will convert CRLF to LF when windows commit code, and LF to CRLF when update code.

Solution :

git rm -r --cached .
git config core.autocrlf false
git add .
git commit -m ""

4. Branch management

​Branch function: You can separate the work from the main development line to make major bug modifications and develop new functions, so as not to affect the main development line.

4.1 View local branch

git branch

4.2 Create a local branch

git branch 分支名

4.3 Switch branch (checkout)

git checkout 分支名
# 接切换到一个不存在的分支(创建并切换)
git checkout -b 分支名

4.4 Merge branches (merge)

Commits from one branch can be merged into another branch

git merge 分支名称

4.5 Delete branch (branch -d /D)

​The current branch cannot , only other branches can be deleted

# 删除分支时,需要做各种检查
git branch -d b1
# 不做任何检查,强制删除
git branch -D b1

4.6 Merge Conflicts

​The reason for the conflict . Git cannot judge how to make the modification, so it gives the right of judgment to the user to make the modification.

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

4.7 Example of use

(1) Create an aaa.txtempty , execute the add and commit commands to save it to the local warehouse, and remark the operation as "create aaa.txt file".

insert image description here

(2) Create a new child_1 sub-branch , and the progress of this sub-branch is consistent with the master branch.

insert image description here

(3) Modify aaa.txtthe content , save it to the local warehouse, and remark the operation as "modify the aaa.txt file". It can be seen that the progress of the master branch is still in 创建 aaa.txt 文件the stage, and there are more operations in the child_1 branch 修改 aaa.txt 文件.

(4) Return to the master branch after submitting, confirming what (3) said, the modified aaa.txtfile does not exist on the master branch, and is still in a blank state .

(5) Create a child_2 sub-branch in the master branch again , and find that the child_2 branch also inherits the progress of the master branch.

(6) Create a bbb.txtfile , modify it, and execute the add and commit commands respectively to save it to the local warehouse.

(7) Return to the master branch again, create and modify a ccc.txtfile , and save it to the local warehouse.

(8) Merge the child_1 sub-branch and child_2 sub-branch into the master branch respectively.

(9) Comparing the three pictures of (7) and (8), we can get the most important conclusions:

  • Red line : steps that are not implemented in the master main branch.
  • Green line : steps that have been implemented by the master master branch .
  • * number : the step implemented by this branch.
  • Indentation : The master main branch is not indented, and the child branch is indented on the basis of the parent branch (the * symbol is used as the standard).

5. Remote warehouse

5.1 Add remote warehouse (remote add)

​ Before the operation, create a new warehouse in github/gitee.

git remote add <远端名称> <仓库路径>
  • Remote name: The default is origin, depending on the remote server settings.
  • Warehouse Path: Obtain this URL from the remote server.

5.2 Configure SSH public key

# 生成 ssh 公钥,不断回车即可(如果之前生成过的视情况是否需要覆盖原来的公钥)
ssh-keygen -t rsa
# 获取公钥
cat ~/.ssh/id_rsa.pub

Gitee set account public key

Verify that the configuration is successful

ssh -T [email protected]

5.3 View remote warehouse (remote)

git remote

5.4 Push to remote warehouse (push)

​Note : Save the file to the local warehouse before pushing.

git push [-f] [--set-upstream] [远端名称 [本地分支名][:远端分支名] ]
# 如果远程分支名和本地分支名称相同,则可以只写本地分支
git push origin master
# 该语句的意思是将本地仓库的 master 分支推送到远程仓库的 master 分支,该语句等同于上面那条语句
git push origin master:master
  • -f: Indicates mandatory coverage.
  • –set-upstream: When pushing to the remote end and establishing an association relationship with the remote branch, you can use git pushthe command push directly without specifying the branch name.
# 查看本地分支与远程分支的关联关系
git branch -vv

5.5 Cloning from a remote warehouse (clone)

git clone <仓库路径> [本地目录]
  • The local directory can be omitted, and a directory will be automatically generated

5.6 Grab from the remote warehouse (fetch)

git fetch [remote name] [branch name]
git merge 分支名称
  • The grab instruction is to grab all the updates in the warehouse locally without merging .
  • If no remote name and branch name are specified, all branches are fetched.

5.7 Pull from the remote warehouse (pull)

git pull [remote name] [branch name]
  • The pull command is to pull the modification of the remote warehouse to the local and automatically merge it , which is equivalent to fetch + merge.
  • If no remote name and branch name are specified, fetch all and update the current branch.

5.8 Push/Pull Conflicts

When multiple people collaborate, it is likely to encounter the following problems:

  • When pushing (push) the A file to the remote warehouse, it is found that someone has modified the A file, and the modified content is inconsistent with your local A file, and the push is blocked .

  • You have modified file A for a long time and have not finished modifying it, but you want to see if other people have made other modifications to the file, so you perform a pull (pull), and find that someone has indeed modified it, pull prohibited.

​ So how do you know about the modifications made by others? That is, how to resolve the conflict?

​ As you can see from the previous command, if the same file is found to be inconsistent when merging branches, the conflicting location will be marked in the file, allowing the user to choose which content to keep. The same is true here, to use the fetch (fetch) + merge branch (merge) command to solve the problem .

5.9 Possible problems

(1) Error reporting when pushing/grabbing:

Link

Reason : The content of the local warehouse is inconsistent with that of the remote warehouse (the remote warehouse has some content that is not available locally, or the content of the same file is different).

Solution :
If you want to submit by force

git push -f origin master

If you want to forcibly pull

git pull origin master --allow-unrelated-histories

(2) Every time you push to the remote warehouse, you need to enter the account password:

Link

git: 'credential-manager' is not a git command. See 'git --help'.

Solution :

git config --global credential.helper store
# 再次进行推送
git push origin master
# 输入账号密码后下次就不用输入了

Guess you like

Origin blog.csdn.net/weixin_48896613/article/details/129218519