Git学习笔记
Nevermore 2023-01-11 LinuxSoftware
# 1.Git安装
git --version # 查看版本
1
# 2.Git架构
- 文件
分成本地文件(working area)、暂存区文件(staging area)、远端仓库文件(remote)
working——(git add .
)——staging ——(git commit -m “ ”
)——remote
- Git各个分支的本质是一个指针,指向了不同的commit历史版本。
# 3.Git设置
- 查看文档
git config --help
git config -h
1
2
2
git config --global user.name ""
git config --global user.email
1
2
2
- 设置默认的文件编辑器为vs code
# 设置git默认打开的编辑器为vs code,需要首先为vs code添加环境变量
# 不设置用的是vim
# --wait 是为了让git暂停,直到关闭文件
git config --global core.editor "code --wait"
git config --global -e # 打开git设置的默认配置文件 .gitconfig
1
2
3
4
5
6
2
3
4
5
6
- 文件换行问题 CRLF——carriage return (回车) line feed (换行)
由于Windows换行符号为\r\n
,而Mac和Linux为\n
,为了让文件能在两个系统兼容,需要设置如下
Windows:上传需要去掉\r
,同步到本地需要添加\r
,设置git自动完成
git config --global core.autocrlf true
1
Mac/Linux:只需要上传时去掉编辑器所带的\r
就行
git config --global core.autocrlf input
1
- 设置文件查看比较器(针对修改的local文件和原来的staged文件)为vs code
git config --global diff.tool vscode #设置之后运行的名字
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
git config --global -e # 进行查看对比,看看是否添加正确
1
2
3
2
3
git difftool #进行查看对比 staging area和 working directory
git difftool --staged #进行查看对比 last commit和 staging area
1
2
2
- 插件,为了让Git带上一些颜色(可选)
Mac:Zsh
Windows:posh-git
# 4.仓库(repository)
- 在一个文件夹中,初始化一个空的仓库
git init
1
- 查看仓库中的文件
git ls-files
1
- 如果有文件,或者文件夹不需要同步到仓库。比如日志文件夹
log
,添加到.gitignore
echo log/ > .gitignore
1
但是当修改log
文件夹下的内容后,使用git status
查看发现仍有modified
,可以使用git rm
git rm -h # 查看帮助
rm filename #系统命令,只会删除working area的内容,对于staged area不会删除
git rm + fileName # 会删除本地和暂存区(staged area)的文件
git rm --cached -r + fileName # 只会删除暂存区的文件,不影响本地文件。若是文件夹-r递归
1
2
3
4
2
3
4
- 查看同步的文件
git status
git status -s # M表示修改;A表示新增;??表示无法check的文件。红色表示没有同步(git add),绿色完成同步
1
2
2
- 查看更改的文件(可参考上部的difftool)
git diff --staged
git diff HEAD~2 HEAD # or specify the file name ;option(--name-only \ --name-status)
1
2
2
- 查看提交的日志
git log
git log --oneline #simplify msg
git log --oneline --all # all branch
git log --oneline --reverse #chage showcut order
git log --oneline -3 #查看最后提价的3条记录
git log --oneline --author="Name" # 对commit信息进行过滤
git log --oneline --before="date" # 对commit信息进行过滤
git log --oneline --after="2020-01-01" # 对commit信息进行过滤,: "one week ago"\"yesterday"
git log --oneline --grep="GUI" # 对commit信息进行过滤
git log --oneline -S"hello" # 对commit内容进行过滤
git log --pretty=format:"%Cgreen%ma committed %h on %cd"
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13

git log --pretty=format:"%Cgreen%ma%Creset committed %h on %cd" #对颜色进行重置
# 对上诉的格式,可以使用别名进行定义
git config --global alias.lg "log --pretty=format:'%ma committed %h on %cd'"
git lg
1
2
3
4
5
2
3
4
5
git show HEAD~1 #查看HEAD之后的1步作了什么修改
git show HEAD~1:+filename
1
2
2
git ls-tree HEAD~1
1

框框中的内容可以是:Commits、Blob(files)、tree(directories)、tags
- 撤销Add的文件,
git restore --staged + Filename #忽视本次的Add,会将上次commit的内容替换staging area
1
git restore + Filename # 将staging area的内容替换到working area
# 如果File不存在,对于??的文件,需要使用 git clean
1
2
3
2
3
对于使用git rm
删除的文件,如果要restore可进行如下操作:
git log --oneline
git restore --source=HEAD~1 + fileName # 从过去的提交中恢复
1
2
2
重命名:
git config --global alias.unstage "restore --staged ."
git unstage # equal : git restore --staged .
1
2
2
- 给commit的项目添加tag
git log --online #查看id
git tag v1.0 + ID
git checkout v1.0
git tag -n # 查看所有tag
git tag -d v1.1 #删除tag
1
2
3
4
5
6
2
3
4
5
6
# 5.分支(branch)
- 新建分支
git branch + Name
git branch # 查看分支
1
2
2
- 切换分支
git checkout + Name
git switch + Name
1
2
2
- 更改分支名称
git branch -m OldName NewName/Specify
1
- 删除分支
git branch -d + Name
1
- 查看分支BranchName中commit的,但是不在master的内容
git log master..BranchName
git diff master..BranchName
git diff --name-status master..BranchName #查看更改的文件
1
2
3
2
3
- 当处在一个分支下进行内容的修改后,没有commit的情况下进行分支的切换,修改的内容会消失,这也是git不允许的。那可以将修改的内容先存储在某一位置(不会出现在history中)
git stash push -m "msg"
# 对于新建的文件,在历史中是untracked,若要将它存储许添加-a选项(means all --all)
git stash push -ma "msg"
# 查看stash list
git stash list
# 查看stash 具体修改的信息
git stash show 1(sequence number)
# 当切换回本分支后,需要对存储的内容进行使用,重新放入working area
git stash apply 1
# 删除stash
git stash drop 1
git stash clear #删除所有
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
分支的Merge
分支结构的查询
git log --oneline --all --graph
1还未merge的分支查询
git branch --no-merged
1- Fast-forward merges
对于如上的分支结构,只需要将Master的指针指向Branch即可
git merge Branch
1- 3-way merges
对于如上的分支结构,会先创建一个merge commit
节点