Git笔记

来源:互联网 发布:sql执行顺序 编辑:程序博客网 时间:2024/05/19 01:31

Git使用相关的命令以及概念的理解记录在这里边。

常用命令

写开发日志(commit message)注意事项

第一行一定要是少于50字的开发概括信息,而且第二行务必是空行,第三行开始才可以开始细致描述开发信息。这是因为很多版本服务系统中的email机制都会选取log中的第一行为邮件题目。

克隆远程repo

$ git clone [url]

查看分支

# 查看所有本地分支$ git branch# 查看所有远程分支$ git branch -r# 查看所有分支(本地+远程)$ git branch -a

新建分支

# 基于base_branch创建一个新分支new_branch,并切换过去$ git checkout -b [new_branch] [base_branch]

切换分支

$ git checkout [existed_branch]

删除分支

# 注意,existed_branch不能是目前所在的分支$ git branch -d [existed_branch]

查看变更

$ git status

查看当前分支的所有版本历史

$ git log

添加文件

# 将当前目录及子目录下的所有新增文件都添加到暂存区$ git add .# 添加指定的文件到暂存区$ git add [file1] [file2] ...# 删除文件,并将删除操作保存到暂存区$ git rm [file1] [file2] ...

提交代码

# 将暂存区里的所有更改提交$ git commit -m [message]# 提交暂存区里的指定文件改动$ git commit [file1] [file2] ... -m [message]

代码合并

# 将develop分支中的改动合并到当前分支(比如当前分支为master)$ git merge --no-ff develop

提交本地的分支到服务器

# 创建一个新分支test$ git branch test# 切换到分之test$ git checkout test# 将test分支推到服务器(之前没有)$ git push --set-upstream origin test

全局或者局部设置用户名与邮箱

全局设置如下

$ git config --global user.email abc@123.com$ git config --global user.name "User Name"

在一个单独的git repo中

$ git config user.email abc@123.com$ git config user.name "User Name"

将服务器的变动更新到本地(commit)

使用git pull命令来进行操作,使用起来稍微有些复杂。
先参考这篇文章 git pull

将服务器的变动更新到本地(branch)

使用git fetch命令来进行操作,比如服务器删除了某些分支,如何将其同步到本地repo呢?直接

$ git fetch

这个命令还有其它几种用法,具体可以参考Git 真正理解 git fetch, git pull 以及 FETCH_HEAD

修改远程地址

$ git remote set-url origin [url]

差异对比

做了改动之后,如果查看具体的改动,可以使用git内置的diff工具

$ git diff

详情参考:Git学习教程(七) Git差异比对

创建tag并提交remote

创建tag

$ git tag -a {tag_name} -m “tag_comment″

push到remote服务器

$ git push origin --tags

更多内容,参考Git打Tag相关操作

根据commit message在log里搜索commit

假如要在所有分支里边搜索包含”Build 1200”字符串的commit,命令如下:

$ git log --all --grep='Build 1200'

更多内容,参考How to search a Git repository by commit message?

查看指定commit的变更

两种命令,

$ git show $COMMIT

或者

$ git diff ${COMMIT}^!

更多内容,参考How to diff a commit with its parent?

分支管理

不同公司根据自己的业务需求会指定不同的分支使用规范。这里列一个网上流传比较广泛的Git分支使用模型:A successful Git branching model。

有热心的人根据这个模型做了一个Git扩展,可以非常快速地按照上述的规范来使用Git: nvie/gitflow。

其实Git之分支创建策略 这篇文章就是根据这个模型来写的。

概念理解

HEAD是什么意思?

HEAD is a ref (reference) to the currently checked out commit. In normal states, it’s actually a symbolic ref to the branch you have checked out - if you look at the contents of .git/HEAD you’ll see something like “ref: refs/heads/master”. The branch itself is a reference to the commit at the tip of the branch.

Hot fixes是什么意思?

When a critical bug in a production version must be resolved immediately, a hotfix branch may be branched off from the corresponding tag on the master branch that marks the production version.

Feature branches是什么?

这类分支只存在于开发者本地的repo中。

它一般用于开发新特性/功能(包括实验性质的,尚未确定是否最终会发布的特性/功能)。

git submodule机制是什么,如何使用?

submodule的信息都存储在repo根目录的”.gitmodules”文件中

执行
$ git submodule init
会将module的git地址与注册到本机的指定路径

然后执行
$ git submodule update
会对每个module执行clone操作

参考

  1. A successful Git branching model
  2. nvie/gitflow
  3. 3.3 Git 分支 - 分支的管理
  4. Review code with pull requests
  5. 常用 Git 命令清单
  6. git 查看远程分支、本地分支、创建分支、把分支推到远程repository、删除本地分支
  7. 芒果iOS开发之Merge branch ‘master’ of XXX Please enter a commit message to explain why this merge
  8. Git之分支创建策略
  9. 如何使用git创建项目,创建分支
  10. git 有用却易忘的知识与命令
  11. GUI Clients
  12. Make an existing Git branch track a remote branch?
  13. How do you create a remote Git branch?
  14. 2.6 Git Basics - Tagging
  15. github创建tag
  16. git pull
  17. git修改远程仓库地址
  18. Git学习教程(七) Git差异比对
  19. Git打Tag相关操作
  20. Git 真正理解 git fetch, git pull 以及 FETCH_HEAD
  21. How to search a Git repository by commit message?
  22. How to diff a commit with its parent?
  23. Git Submodule使用完整教程
0 0
原创粉丝点击