Git入门指南

引言

Git是2005年Linus Torvalds 为了帮助管理 Linux(R) 内核开发而开发的一个开放源码的版本控制软件, 可以说是当今世界上最先进的分布式版本控制系统。

SVN与Git

SVN作为集中式版本控制系统的,版本库放在中央服务器。代码贡献者将各自的代码推送到中央服务器。这也就决定了集中式的版本控制系统必须联网,而且网速还不能慢。

Git与之相反,是分布式的,它没有中央服务器,每个人的电脑是一个完整的版本库,这样工作就不需要联网,多人协作就靠将各自的修改推送给对方,就可以互相看到对方的修改了。

本篇试简单介绍Git的常用操作与原理

  • Workspace:工作区,即在电脑中看到的目录
  • Index / Stage:暂存区,存放在.git目录下的index文件中
  • Repository:仓库区(或本地仓库)工作区有一个隐藏目录.git,不算工作区,是git的版本库
  • Remote:远程仓库

新建代码库

  • 在当前目录新建一个Git代码库
    1
    git init
  • 新建一个目录,将其初始化为Git代码库
    1
    git init [project-name]
  • 下载一个项目和它的整个代码历史
    1
    git clone [url]
  • 克隆指定分支

    1
    git clone -b [branch] [url]

    配置

  • 显示当前的Git配置

    1
    git config --list
  • 编辑Git配置文件
    1
    git config -e [--global]
  • 设置提交代码时的用户信息
    1
    2
    git config [--global] user.name "[name]"
    git config [--global] user.email "[email address]"

    增加/删除文件

  • 添加改动文件(添加到暂存区)
    添加所有改动文件(不包括 .gitignore 忽略的文件):

    1
    git add *
  • 添加指定文件到暂存区

    1
    git add [file1] [file2] ...
  • 添加指定目录到暂存区,包括子目录
    1
    git add [dir]
  • 添加当前目录的所有文件到暂存区
    1
    git add .
  • 添加每个变化前,都会要求确认对于同一个文件的多处变化,可以实现分次提交
    1
    git add -p
  • 查看文件改动和提交状态
    1
    git status
  • 查看具体文件改动对比(工作区与版本库最新版本)
    1
    git diff HEAD -- [file]
  • 删除所有未添加文件的改动(使 git status 恢复到 clean 状态)
    1
    git checkout .
    删除某个文件的当前修改(未提交到暂存区)
    1
    git checkout -- [file]
  • 删除工作区文件,并且将这次删除放入暂存区
    1
    git rm [file1] [file2] ...
  • 停止追踪指定文件,但该文件会保留在工作区
    1
    git rm --cached [file]
  • 改名文件,并且将这个改名放入暂存区

    1
    git mv [file-original] [file-renamed]

    代码提交

  • 提交暂存区到仓库

    1
    git commit -m [message]
  • 提交暂存区的指定文件到仓库区
    1
    git commit [file1] [file2] ... -m [message]
  • 提交工作区自上次commit之后的变化,直接到仓库区
    1
    git commit -a
  • 提交时显示所有diff信息
    1
    git commit -v
  • 使用一次新的commit,替代上一次提交
    如果代码没有任何新变化,则用来改写上一次commit的提交信息
    1
    git commit --amend -m [message]
  • 重做上一次commit,并包括指定文件的新变化
    1
    git commit --amend [file1] [file2]

    分支的操作

  • 查看所有分支
    1
    git branch
  • 列出所有远程分支
    1
    git branch -r
  • 列出所有本地分支和远程分支
    1
    git branch -a
  • 新建一个分支,但依然停留在当前分支
    1
    git branch [branch-name]
  • 新建一个分支,并切换到该分支
    1
    git checkout -b [branch]
  • 新建一个分支,指向指定commit
    1
    git branch [branch] [commit]
  • 新建一个分支,与指定的远程分支建立追踪关系
    1
    git branch --track [branch] [remote-branch]
  • 切换到指定分支,并更新工作区
    1
    git checkout [branch-name]
  • 切换到上一个分支
    1
    git checkout -
  • 建立追踪关系,在现有分支与指定的远程分支之间
    1
    git branch --set-upstream [branch] [remote-branch]
  • 合并指定分支到当前分支
    1
    git merge [branch]
  • 选择一个commit,合并进当前分支
    1
    git cherry-pick [commit]
  • 删除分支
    1
    git branch -d [branch-name]
  • 当分支上面还有未完成的提交时,需要强制删除
    1
    git branch -D [branch-name]
  • 删除远程分支

    1
    2
    git push origin --delete [branch-name]
    git branch -dr [remote/branch]
  • 本地从远程分支拉取新建分支并切换到新分支

    1
    git checkout -b [branch-name] [remote/branch]

标签

  • 列出所有tag
    1
    git tag
  • 新建一个tag在当前commit
    1
    git tag [tag]
  • 新建一个tag在指定commit
    1
    git tag [tag] [commit]
  • 删除本地tag
    1
    git tag -d [tag]
  • 删除远程tag
    1
    git push origin :refs/tags/[tagName]
  • 查看tag信息
    1
    git show [tag]
  • 提交指定tag
    1
    git push [remote] [tag]
  • 提交所有tag
    1
    git push [remote] --tags
  • 新建一个分支,指向某个tag

    1
    git checkout -b [branch] [tag]

    查看信息

  • 显示有变更的文件

    1
    git status
  • 显示当前分支的版本历史
    1
    git log
  • 显示commit历史,以及每次commit发生变更的文件
    1
    git log --stat
  • 搜索提交历史,根据关键词
    1
    git log -S [keyword]
  • 显示某个commit之后的所有变动,每个commit占据一行
    1
    git log [tag] HEAD --pretty=format:%s
  • 显示某个commit之后的所有变动,其”提交说明”必须符合搜索条件
    1
    git log [tag] HEAD --grep feature
  • 显示某个文件的版本历史,包括文件改名
    1
    2
    git log --follow [file]
    git whatchanged [file]
    1
    2
    * 显示分支合并图 一行显示
    git log --graph --pretty=oneline --abbrev-commit # 得到sha1计算的版本号commit id
  • 显示指定文件相关的每一次diff
    1
    git log -p [file]
  • 显示过去5次提交
    1
    git log -5 --pretty --oneline
  • 显示所有提交过的用户,按提交次数排序
    1
    git shortlog -sn
  • 显示指定文件是什么人在什么时间修改过
    1
    git blame [file]
  • 显示暂存区和工作区的差异
    1
    git diff
  • 显示暂存区和上一个commit的差异
    1
    git diff --cached [file]
  • 显示工作区与当前分支最新commit之间的差异
    1
    git diff HEAD
  • 显示两次提交之间的差异
    1
    git diff [first-branch]...[second-branch]
  • 显示今天你写了多少行代码
    1
    git diff --shortstat "@{0 day ago}"
  • 显示某次提交的元数据和内容变化
    1
    git show [commit]
  • 显示某次提交发生变化的文件
    1
    git show --name-only [commit]
  • 显示某次提交时,某个文件的内容
    1
    git show [commit]:[filename]
  • 纪录每一次命令,可用于查找某一提交版本的 commit id。

    1
    git reflog

    远程同步

  • 下载远程仓库的所有变动

    1
    git fetch [remote]
  • 远程仓库的删除与重命名
    1
    2
    $ git remote rename [new_name] [old_name]
    $ git remote rm [old_name]
  • 显示所有远程仓库
    1
    git remote -v
  • 显示某个远程仓库的信息
    1
    git remote show [remote]
  • 增加一个新的远程仓库,并命名
    1
    git remote add [shortname] [url]
  • 取回远程仓库的变化,并与本地分支合并
    1
    git pull [remote] [branch]
  • 上传本地指定分支到远程仓库
    1
    git push [remote] [branch]
  • 强行推送当前分支到远程仓库,即使有冲突
    1
    git push [remote] --force
  • 推送所有分支到远程仓库
    1
    git push [remote] --all

撤销

  • 恢复暂存区的指定文件到工作区
    1
    git checkout [file]
  • 恢复某个commit的指定文件到暂存区和工作区
    1
    git checkout [commit] [file]
  • 恢复暂存区的所有文件到工作区
    1
    git checkout .
  • 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
    1
    git reset [file]
  • 重置暂存区与工作区,与上一次commit保持一致
    1
    git reset --hard
  • 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
    1
    git reset [commit]
  • 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
    1
    git reset --hard [commit]
  • 重置当前HEAD为指定commit,但保持暂存区和工作区不变
    1
    git reset --keep [commit]
  • 新建一个commit,用来撤销指定commit后者的所有变化都将被前者抵消,并且应用到当前分支
    1
    git revert [commit]
  • 暂时将未提交的变化移除,稍后再移入
    1
    git stash
  • 恢复操作(两步)
    1
    2
    3
    git stash apply # 恢复后,stash内容并不删除
    git stash drop # 此时stash内容删除
    git stash apply stash@{0} #多次stash 可以查看并恢复指定stash
  • 恢复操作(一步)
    1
    2
    git stash pop # 恢复的同时把stash内容也删了
    git stash list # 此时是空的

    其他记录

创建分支修复临时bug

1
2
3
4
5
6
7
git checkout master
git checkout -b issue-001
# 完成修复
git add *
git commit -m "fix bug 001"
git checkout master
git merge --no-ff -m "merged bug fix 101" issue-001 # 保存之前的分支历史,更好查看merge历史,禁用fast forward

忽略特殊文件

Git工作区的根目录下创建一个特殊的.gitignore文件

  • 强制添加被忽略的文件到git
    1
    git add -f [file] 
  • 查看.gitignore是否是否规则有问题

    1
    git check-ignore -v [file]

    配置别名

  • 针对用户

    1
    2
    3
    4
    git config --global alias.st status
    git config --global alias.co checkout
    git config --global alias.ci commit
    git config --global alias.br branch
  • 针对仓库
    在.git/config里面alias 里面配置

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!