iOS 开发交流

Git常用命令

2019-04-18  本文已影响0人  Mr__Peng__

常用Git命令,日常工作是够了,默认已安装Git

安装Git(Mac)

安装Xcode默认安装Git,需要最新版本,去下载
图形化操作工具,去下载

配置

配置用户名和邮件地址

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
$ git config --global alias.co checkout // 设置别名

检查配置信息

$ git config --list
$ git config <key>  查询某一项配置 

获取帮助

$ git help <verb> // 例如 config
$ git <verb> --help
$ man git-<verb>
init
git clone

已知远程仓库地址

$ git clone https://github.com/PSLoveYSJ/PS_iOS.git
本地初始化

新建文件夹 Test
终端-Test目录下

$ git init 
$ git add test.txt
$ git commit -m "first commit" // 不需要上传到网络的情况下够了
$ git remote add origin https://github.com/xxx/xxx.git // 添加远程origin地址
$ git push -u origin master // 推送到远程终端 前提是添加的地址存在
add

添加文件跟踪

$ git add .  // 添加所有修改的文件
$ git add filename // 添加文件
$ git add *.c  添加所有.c格式文件
$ git add 目录 // 添加目录下的文件
status

状态

$ git status // 检查当前文件状态

结果出现Untracked files说明有文件未被跟踪,如需要跟踪,需要add 文件
Changes to be committed说明文件在暂存状态,需要commit
Changes not staged for commit 暂存区的文件作了修改 但没放到暂存区,需要add 重新添加到暂存区
文件同时存在Changes to be committedChanges not staged for commit两种状态,提交后版本是add的最后一个版本

commit

提交暂存区的修改

$ git commit -m "first commit" //  提交
$ git commit -a -m "所有已经跟踪过的文件暂存起来一并提交"

重新提交

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend // 最终只会有一个提交 此次替代第一次
diff
$ git diff  // 尚未暂存的文件更新了哪些部分
$ git diff --staged/--cached // 将要添加到下次提交里的内容
删除 rm
$ git rm 文件 // 下一次提交后 这个文件就不纳入版本管理了
$ git rm log/\*.log // 删除 log/ 目录下.log 文件
$ git rm \*~ 删除 ~结尾的文件
移动 mv
$ git mv file_from file_to // 
相当于执行
$ mv file_from file_to
$ git rm file_from
$ git add file_to
log

查看提交历史

$ git log
$ git log --oneline --decorate // 查看各个分支当前所指的对象
$ git log --oneline --decorate --graph --all 输出你的提交历史、各个分支的指向以及项目的分支分叉情况

列出所有的更新

commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 16:40:33 2008 -0700

    removed unnecessary test

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 10:31:28 2008 -0700

    first commit

显示每次提交的内容差异

$ git log -p -2  // -2显示最近两次 不加就是所有的

显示每次提交简略统计信息

$ git log --stat 

使用不同默认格式的方式展示提交历史

$ git log --pretty=oneline // 每个提交放在一行显示

还有 short full,fuller
另外还有format ,自定义显示输出格式

$ git log --pertty="%H  %s"

常用的有

%H 提交对象(commit)的完整哈希字串

%h 提交对象的简短哈希字串

%T 树对象(tree)的完整哈希字串

%t 树对象的简短哈希字串

%P 父对象(parent)的完整哈希字串

%p 父对象的简短哈希字串

%an 作者(author)的名字

%ae 作者的电子邮件地址

%ad 作者修订日期(可以用 --date= 选项定制格式)

%ar 作者修订日期,按多久以前的方式显示

%cn 提交者(committer)的名字

%ce 提交者的电子邮件地址

%cd 提交日期

%cr 提交日期,按多久以前的方式显示

%s 提交说明
reset
$ git reset HEAD 文件名 取消暂存某个文件
checkout

撤销文件修改

$ git checkout -- 文件名 //  撤销修改 还原成上次的样子
remote

远程仓库

$ git remote -v // 查看远程仓库
$ git remote add ps https://github.com/PSLOVEYSJ/ddd // 添加远程仓库
$ git remote show origin // 查看某一个远程仓库信息
$ git remote rename pb paul 远程仓库重新命名
$ git remote rm paul 移除远程仓库
fetch
$ git fetch [remote-name] // 远程仓库拉取
push

推送到远程仓库

$ git push [remote-name]  [branch-name]
$ git push origin v1.5  // 共享远程标签
$ git push origin --tags // 将所有本地标签共享到远程
$ git push origin :refs/tags/v1.4 //  更新远程仓库
tag

标签

$ git tag 查看标签
$ git tag -a v1.4 -m 'my version 1.4' // 创建附注标签 可以通过git show v1.4查看标签详细信息
$ git tag v1.4-lw 创建轻量标签 show 不会显示额外信息
$ git tag -d v1.4 删除标签 // 不会删除远程标签 需要配合$ git push origin :refs/tags/v1.4 使用
branch 分支
创建
$ git branch -v //查看每个分支的最后一次提交
$ git branch //查看所有分支
$ git branch new // 创建新分支
$ git checkout master // 切换分支
$ git  checkout -b new // 创建并切换分支
$ git merge hotfix // 合并分支到当前分支
$ git branch --merged // 查看哪些分支已经合并到当前分支
$ git branch --no-merged  // 查看哪些分支还未合并到当前分支
$ git branch -d aaa  // 删除分支
$ git branch -D aaa  // 强制删除分支

如果出现冲突

$ git merge iss53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

找到冲突文件,搜索HEAD,会发现以下内容

<<<<<<< HEAD:index.html
<div id="footer">contact : email.support@github.com</div>
=======
<div id="footer">
 please contact us at support@github.com
</div>
>>>>>>> iss53:index.html

====== 上半部分是当前分支,下半部分是合并分支,解决需要使用的内容,完事git add index.html表示冲突解决完成,通过git status 查看是否已经将所有的冲突解决完成,然后commit提交一次

上一篇 下一篇

猜你喜欢

热点阅读