Git的使用

1.Git安装

Git官网

git --version # 查看版本

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
git config --global user.name ""
git config --global user.email 
  • 设置默认的文件编辑器为vs code
# 设置git默认打开的编辑器为vs code,需要首先为vs code添加环境变量
# 不设置用的是vim
# --wait 是为了让git暂停,直到关闭文件
git config --global core.editor "code --wait"

git config --global -e # 打开git设置的默认配置文件 .gitconfig
  • 文件换行问题 CRLF——carriage return (回车) line feed (换行)

由于Windows换行符号为\r\n,而Mac和Linux为\n,为了让文件能在两个系统兼容,需要设置如下

Windows:上传需要去掉\r,同步到本地需要添加\r,设置git自动完成

git config --global core.autocrlf true

Mac/Linux:只需要上传时去掉编辑器所带的\r就行

git config --global core.autocrlf input
  • 设置文件查看比较器(针对修改的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 # 进行查看对比,看看是否添加正确
git difftool #进行查看对比 staging area和 working directory
git difftool --staged #进行查看对比 last commit和  staging area
  • 插件,为了让Git带上一些颜色(可选)

Mac:Zsh

Windows:posh-git

4.仓库(repository)

  • 在一个文件夹中,初始化一个空的仓库
git init
  • 查看仓库中的文件
git ls-files
  • 如果有文件,或者文件夹不需要同步到仓库。比如日志文件夹log,添加到.gitignore
 echo log/ > .gitignore

但是当修改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递归
  • 查看同步的文件
git status 
git status -s # M表示修改;A表示新增;??表示无法check的文件。红色表示没有同步(git add),绿色完成同步
  • 查看更改的文件(可参考上部的difftool)
git diff --staged 
git diff HEAD~2 HEAD # or specify the file name ;option(--name-only \ --name-status)
  • 查看提交的日志
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"

在这里插入图片描述

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
git show HEAD~1 #查看HEAD之后的1步作了什么修改
git show HEAD~1:+filename
git ls-tree HEAD~1

在这里插入图片描述

框框中的内容可以是:Commits、Blob(files)、tree(directories)、tags

  • 撤销Add的文件,
git restore --staged + Filename #忽视本次的Add,会将上次commit的内容替换staging area
git restore + Filename # 将staging area的内容替换到working area

# 如果File不存在,对于??的文件,需要使用 git clean

对于使用git rm删除的文件,如果要restore可进行如下操作:

git log --oneline
git restore --source=HEAD~1 + fileName # 从过去的提交中恢复

重命名:

git config --global alias.unstage "restore --staged ."
git unstage # equal : git restore --staged .
  • 给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

5.分支(branch)

  • 新建分支
git branch + Name
git branch # 查看分支
  • 切换分支
git checkout + Name
git switch + Name
  • 更改分支名称
git branch -m OldName NewName/Specify
  • 删除分支
git branch -d + Name
  • 查看分支BranchName中commit的,但是不在master的内容
git log master..BranchName
git diff master..BranchName
git diff --name-status master..BranchName #查看更改的文件
  • 当处在一个分支下进行内容的修改后,没有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 #删除所有
  • 分支的Merge

    分支结构的查询

    git log --oneline --all --graph
    

    还未merge的分支查询

    git branch --no-merged
    
    • Fast-forward merges

在这里插入图片描述

对于如上的分支结构,只需要将Master的指针指向Branch即可

git merge Branch
  • 3-way merges

对于如上的分支结构,会先创建一个merge commit节点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值