Git 的基本命令
- 创建一个 本地仓库
$ git init
- 配置仓库
- 告诉 git 你是谁
$ git config —-global user.name 用户名
-
- 告诉 git 怎么联系你
$ git config —-global user.email 你的邮箱
- 查看当前所有配置
$ git config -l
- 查看文件状态(红色 代表在 工作区; 绿色 代表在 暂存区)
$ git status
- 添加文件到暂存区(每次新建或者修改之后都需要重新 add)
$ git add 文件名
- 添加文件到 本地仓库
$ git commit 文件名 -m ”日志内容"
- 查看所有版本库日志
$ git log
- 查看指定文件的版本日志
$ git log 文件名
- 配置带颜色的 log 别名
$ 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"
- 回到上当前版本, 放弃所有没有提交的修改
$ git reset —-head HEAD
- 回到上一个版本
$ git reset --head HEAD^
- 回到指定版本号的版本
$ git reset 版本号
- 查看所有修改信息(所有版本)
$ git reflog
- 查看文件的变化
$ git diff
- 撤销对文件做的修改
$ git checkout 文件名称
- 建立 git 远程仓库(这个仓库仅仅是用于管理代码, 不参与开发)
$ git init --bare
- 忽略指定文件不纳入版本库的管理
- 在命令行进入与 .git 同级的目录
$ cd /Users/用户名/Desktop/git本地仓库/XXX/项目名
-
- 将以下命名一次性粘贴到命令行中(参考网址:https://github.com/github/gitignore)
echo -e "# Xcode # build/ *.pbxuser *.mode1v3 *.mode2v3 *.perspectivev3 xcuserdata *.xccheckout *.moved-aside DerivedData *.hmap *.ipa *.xcuserstate # CocoaPods # # We recommend against adding the Pods directory to your .gitignore. However # you should judge for yourself, the pros and cons are mentioned at: # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control # # Pods/" > .gitignore
-
- 将 .ignore 添加进代码库
$ git add .gitignore
$ git commit .gitignore -m “日志内容"
- 查看当前标签
$ git tag
- 在本地代码库给项目打上一个标签
$ git tag -a v1.0 -m ‘日志内容'
- 将标签推送到远程代码库中
$ git push origin v1.0
- 切换到指定的 v1.0 标签的版本
$ git checkout v1.0
- 根据提示: 开启一个新的分支
$ git checkout -b 分支名称
- 查看远程分支
$ git branch -r
- 删除远程分支
$ git branch -r -d origin/分支名称