Git常用命令总结

IT-Pony 2018-01-09 PM 1301℃ 0条

git 库操作

  • Git 全局设置
    git config --global user.name "大飞"
    git config --global user.email "support@junfei.ma"
  • 创建一个新仓库
    git clone git@code.testing.cn:itpony/testing.git
    cd chinapay
    touch README.md
    git add README.md
    git commit -m "add README"
    git push -u origin master
  • 推送现有文件夹
    cd existing_folder
    git init
    git remote add origin git@code.to2.cn:itpony/chinapay.git
    git add .
    git commit -m "Initial commit"
    git push -u origin master
  • 推送现有的 Git 仓库
    cd existing_repo
    git remote rename origin old-origin
    git remote add origin git@code.to2.cn:itpony/chinapay.git
    git push -u origin --all
    git push -u origin --tags

Git使用和规范

1、 分支命名

example: 创建新的分支命令如下

git checkout -b dev_cf



2、 提交备注

commit 备注 须备注清楚  修改文件 及 工作内容  指定但前修改的行数
---------------------------------------------------------------
example:
    
今天2016年5月12日 我修改了ConfigurationController.php 文件下的 setRegisteredUsers方法 功能是添加友盟的注册

git commit -m "添加友盟第三方注册功能,修改文件:ConfigurationController.php ->setRegisteredUsers方法 从第27行开始至100行左右结束"



3、 版本冲突、注意事项及解决方法

**注意事项**

(1) 严禁出现冲突后`脱离gitlab`跟踪机制线下本地导入导出文件。
(2) 严禁在代码未经过审查时合并公共分支。
(3) 严禁回滚及删除公共分支。
(4) 解决冲突时必须与相关开发者当面确定相关代码段。
(5) 开发时如果清楚会涉及到别人文件时须跟相关开发者沟通询问具体操作规范

**解决方法**

合并或拉取对方分支时出现冲突文件,首先打开冲突文件 gitlab 冲突代码段标记格式如下,

<<<<<< xxx
    code...    // 本地分支的代码段
===========    
    code...    // 当前拉取分支的代码段
>>>>>>>>xxx



  解决冲突文件时选择保留其中一段或俩者保留,根据和相关开发者沟通结果确定修改完后,删除标记格式,保存,提交到解决冲突的开发者分支后,让相关开发者pull你的分支即可。


常用命令清单

(1) 代码库相关

$ git clone [url] # 下载一个项目和它的整个代码历史



(2) 添加删除文件

$ git add [file1] [file2] ... # 添加指定文件到暂存区

$ git add [dir]  # 添加指定目录到暂存区,包括子目录

$ git add .  # 添加当前目录的所有文件到暂存区

$ git rm [file1] [file2] ...  # 删除工作区文件,并且将这次删除放入暂存区

$ git rm --cached [file]  # 停止追踪指定文件,但该文件会保留在工作区

$ git mv [file-original] [file-renamed]  # 改名文件,并且将这个改名放入暂存区



(3) 代码提交

$ git commit -m [message]  # 提交暂存区到仓库区

$ git commit [file1] [file2] ... -m [message]  # 提交暂存区的指定文件到仓库区



(4) 分支

$ git branch  # 列出所有本地分支

$ git branch -r  # 列出所有远程分支

$ git branch -a  # 列出所有本地分支和远程分支

$ git branch [branch-name]  # 新建一个分支,但依然停留在当前分支

$ git checkout -b [branch]  # 新建一个分支,并切换到该分支

$ git checkout [branch-name]  # 切换到指定分支,并更新工作区

$ git checkout -  # 切换到上一个分支

$ git merge [branch]  # 合并指定分支到当前分支

$ git branch -d [branch-name]  # 删除本地分支


<font color=#999999 size=3>删除远程分支</font>

$ git push origin :[branch-name]  (origin 后面有空格) 

$ git push origin --delete [branch-name] <font color=#999999 size=3></font>

$ git branch -dr [remote/branch] 



(5) 查看信息

$ git status  # 显示有变更的文件

$ git log  # 显示当前分支的版本历史

$ git log --stat  # 显示commit历史,以及每次commit发生变更的文件

$ git log -S [keyword]  # 搜索提交历史,根据关键词

$ git log [tag] HEAD --grep feature  # 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件

$ git diff  # 显示暂存区和工作区的差异

$ git reflog  # 显示当前分支的最近几次提交



(6) 远程同步

$ git fetch [remote]  # 下载远程仓库的所有变动

$ git remote -v  # 显示所有远程仓库

$ git pull [remote] [branch]  # 取回远程仓库的变化,并与本地分支合并

$ git push [remote] [branch]  # 上传本地指定分支到远程仓库

$ git push [remote] --force  # 强行推送当前分支到远程仓库,即使有冲突



(7) 撤销

$ git checkout [file]  # 恢复暂存区的指定文件到工作区

$ git checkout [commit] [file]  # 恢复某个commit的指定文件到暂存区和工作区

$ git checkout .  # 恢复暂存区的所有文件到工作区

$ git reset [file]  # 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变

$ git reset --hard  # 重置暂存区与工作区,与上一次commit保持一致

$ git reset [commit]  # 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变

$ git reset --hard [commit]  # 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致,eg: git reset --hard HEAD~3:将最近3次的提交回滚


暂时将未提交的变化移除,稍后再移入


$ git stash

$ git stash pop 

.gitignore规则不生效的解决办法

// 清除本地缓存
git rm -r --cached .
// 添加文件
git add .
// 提交修改
git commit -m 'update .gitignore'
标签: none

非特殊说明,本博所有文章均为博主原创。

评论啦~