发布于 

Git命令快速指北

  

前言

Git是分布式版本管理系统,什么意思呢?

  • 分布式:没有中心服务器,在本地即可完成提交代码

  • 版本管理: 让代码跳转到某次提交的版本。

    eg:
    小明第一次学c语言写了个 hello.c ,输出Hello World!。但是第二天他学了新的知识,重写了 hello.c 并保存了,出了 bug。他现在想回到第一天的 hello.c ,但 ctrl+z 已经没有用了,因为小明已经保存了代码,这时 Git 便能再次实现撤回的效果,回滚到第一天的 hello.c

    Git中的术语

  • 工作区:文件管理器的目录

  • 暂存区:叫 stage 或 index。 是.git/里面的 index 文件(.git/index)

  • 版本库:当前目录/.git

  • 远程仓库:github上创建的仓库

  • HEAD 指针:指向当前版本

关系:工作区->暂存区->版本库

由工作区 add 到暂存区, 由暂存区 commit 到版本库

指令速查

  • git init 在空目录中初始化仓库

  • git clone https://xxxx

  • git status 粗略查看工作区状态

  • git diff 文件名 (详细查看修改细节)

  • git add 添加
    git add fileName
    git add --all 新增和修改文件提交到暂存区

  • git commit 提交备注
    git commit -m "comments"

  • git fetch && git merge 拉取与合并
    git fetch origin tag 远程标签名 拉取远程标签
    git merge 分支名 合并本地某分支到当前分支
    例如:将dev合并到master,先checkout master ,然后git merge dev

  • git pull
    git pull origin 远程分支名 相当于 fetch + merge

  • git push
    git push -u origin 分支名(master) 第一次push到仓库,set-upsteam,一次性推送本地master
    git push origin 分支名|标签名 推送至远程仓库 通常是写git push origin master
    git push --all origin 推送所有 branch 至远程仓库
    git push --tags 推送所有 tag
    git push origin --delete 分支名|标签名 删除远程 branch 或 tag

  • git checkout 分支管理

    git checkout 分支名 仅切换分支(或git switch 分支名)
    git checkout -- 文件名 撤销修改工作区
    git checkout -b 分支名 创建一个新的分支并切换(或git switch -c)
    git branch -m 旧名 新分支名 重命名本地分支
    git branch -a 查看所有分支(本地,远程)
    git branch 分支名 查看本地分支
    git branch -r 分支名 查看 remote(远程)分支
    git branch -d 分支名|标签名 删除本地合并后的分支或标签
    git branch -D 分支名|标签名 强制删除本地分支或标签,不管是否合并

  • git cherry-up (同步某次commit的提交)

  • git tag 标签
    git tag 查看本地 tag
    git show tag名 查看单个 tag 详情
    git tag 标签名 创建 tag
    git tag -a 标签名 -m 备注信息 创建 tag 并备注
    git tag -s 标签名 -m 备注信息 创建 PGP密钥 tag,并备注
    git -tag -d tag名 删除tag
    git push origin <tagname> 根据tag名推送

  • git reset
    git reset HEAD 文件名 撤销暂存区,回到工作区(撤销 git add 内容)

  • git remote
    git remote add origin git@server-name:path/repo-name.git 添加远程仓库关联
    git remote rm origin 删除远程仓库的关联

代码冲突解决

无法使用 merge 进行直接合并,报错:合并冲突

1
2
3
4
git merge feature1
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result

手动修改冲突文件的内容,git会以<<<<<<<,=======,>>>>>>>标记出不同分支的内容
编辑完冲突内容后,add,commit,最后再删除不需要的feature1分支
git branch -d feature1

版本回退

git log 查看提交记录,根据 commit id 进行后续回退

  • soft(默认)
    git reset --soft [commit id]
    版本库的 HEAD 回滚到某个 commit ,但本地代码不变,处于未 commit 的状态。
  • hard
    git reset --hard [commit id]
    HEAD 和本地代码都回到某个 commit,后面的更改将会被丢弃。(约等于ctrl+z)

    远程推送失败

  • git pull
    若失败则将本地分支与远程分支建立关联git branch --set-upstream-to=origin/<分支名> dev
    然后 git pull,解决冲突,最后pushgit push origin dev

忽略文件

在 Git 工作区的根目录下创建一个特殊的 .gitignore 文件,然后把要忽略的文件名填进去,Git 就会自动忽略这些文件。

GitHub 在线模板:https://github.com/github/gitignore

忽略文件的原则是:

忽略操作系统自动生成的文件,比如缩略图等
忽略编译生成的中间文件、可执行文件等
忽略你自己的带有敏感信息的配置文件,比如存放口令的配置文件。

Git Bash alias

  • Git 下的 alias 别名
    git config –global alias.别名 “原字符”
    例如下面这样的魔鬼别名代码

    1
    2
    3
    git config --global alias.lg "log --color --graph 
    --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'
    --abbrev-commit"
  • bash 下的 alias 别名
    当然也可以如同在 Liunx 环境下编辑 bash 的配置文件.bash_profile
    添加alias ls="ls -al"
    注意:=不能有空格

附上我的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# generated by Git for Windows
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc
alias bashalias='code ~/.bash_profile'
alias bashcolor='code ~/.minttyrc'
alias bashconfig='code /etc/profile.d/git-prompt.sh'
alias gitconfig='code ~/.gitconfig'

alias .='cd ~'
alias ..='cd ..'
alias ...='cd ../..'
alias e='exit'
alias cls='clear'

alias gs='git status'
alias ga='git add .'
alias gc='git commit -m'
alias gp='git push'
alias gitauto='git add . && git commit -m "auto deploy" && git push'

alias sysoff='shutdown -s -t 0'
alias sysre='shutdown -r -t 0'

alias host='code /c/Windows/System32/drivers/etc/hosts'

alias yt='you-get -o D:\\Video\\you-get'
alias WK='python D:\\code\\python\\ZhiHuiShu.py'
alias h3='hexo clean && hexo generate && hexo serve'
alias h4='hexo clean && hexo generate && hexo serve && hexo deploy'