git 常用操作

作者: Stake | 来源:发表于2016-08-21 14:23 被阅读52次

拉代码

git clone ssh://user-name@url

获取远程信息

git fetch (--all)

更新代码

git pull (--rebase| -r)
//(pull 表示把远程仓库的更新拉下来, --rebase 虽然是可选项, 但是一般都建议一起使用,这里涉及到 “基”的概念。

分支

切分支

代码拉下来后默认是在master分支, 需要用 git checkout ref_name切过去

列分支

git branch 列出本地分支
git branch -r列出远程分支

切分支

``git checkout branch_name (执行命令前本地不能有未缓存的提交,否则失败)

新建分支

git branch 'branch_name'
git checkout -b 'branch_name'  // 执行完后会立即切换到新分支
git branch 'branch_name' 'origin/branch_name' //新建远程分支 
git checkout branch_name (等价于 git checkout  -b 'branch_name'  'origin/branch_name' )
//使用远程分支,并在本地建立branch

删除本地分支

git branch -D 'branch_name' //大写D表示强制删除

本地分支与远程分支的对应

特殊情况下,执行 git pull 命令时会提示找不到对应的远程分支, 需要用 git branch --set-upstream-to=<upstream>命令指定一个远程分支.

显示远程分支的url

//if referential integrity has been broken:
git config --get remote.origin.url

//If referential integrity is intact:
git remote show origin

//本地git与远程服务器绑定
git remote add origin git@github.com:xxx.git

提交

本地提交

  • 一般都用图形化工具 citool.用git citool命令可调起。
  • 命令行的方式:
 git add // 把文件加到版本库的管理中 ,执行完后文件在缓存区
 git add --all //将所有文件放入stage中,包括新增加的文件
 git rest // unstaged all files
 git commit -m 'msg' //将stage中文件提交
 git commit -am 'msg' //将modify和 stage 中文件提交,不包括不在stage中的新增文件
 git commit --amend -m‘msg’ 将本次提交和上次提交合并。在本地的提交中使用,还没有push到服务器中

推送到服务器

git push 
git push origin HEAD:branch_name //本地分支名与远程名称不对应时候
git push origin HEAD:refs/for/ref_name //推送到gerrit

注意1:HEAD在LINUX下需要大写
注意2: 不要忘记加 HEAD:refs/for/ 否则有可能会创建一个新分支。

冲突的解决

命令:

git mergetool 'file_path'

git 对比

git difftool --dir-diff(-d) //git open all diff files 
git difftool -d HEAD //stage 中files对比
git difftool -d sha sha^1 // 提交sha的文件变更

git svn

git svn  fetch // 更新svn代码
git svn rebase //更新本分支head到最新

tag

打tag

命令:

git tag V_1.0.0.0 -m “version 1.0”

一般每编一次发布版,都会打上一个tag

将tag推送到远程

git push origin tag_name

注意: 推送到远程的TAG必须得有日志信息,对应于上面 -m 参数后的内容。

gitk使用

查看分支

gitk // 查看当前本地分支
gitk ref_name //查看其他分支
gitk origin/ref_name //查看某个远程分支
gitk --all //查看所有分支

本地修改

常用命令

git reset [--hard|--soft|--mixed] [revision] //reset命令只对入库的文件有效,
git clean –fd //对未入库的文件,可使用 git clean –fd 清除
git reset --hard //回退所有的内容

//可以回退某一个提交,这个操作相当于是把当时那个提交逆向PATCH了一次。
//操作完成后,需要COMMIT到仓库。
git revert –n sha1-id
//回退某一次的提交某一个文件
git checkout sha1-id file (file1...)


git stash

//工作到一般时有紧急任务时, 配合 
git stash
git stash pop
git stash save -m'log'
git stash -p //stash 某一文件
// y - stash this hunk
// n - do not stash this hunk
// q - quit; do not stash this hunk or any of the remaining ones
// a - stash this hunk and all later hunks in the file
// d - do not stash this hunk or any of the later hunks in the file
// g - select a hunk to go to
// / - search for a hunk matching the given regex
// j - leave this hunk undecided, see next undecided hunk
// J - leave this hunk undecided, see next hunk
// k - leave this hunk undecided, see previous undecided hunk
// K - leave this hunk undecided, see previous hunk
// s - split the current hunk into smaller hunks
// e - manually edit the current hunk
// ? - print help// 
git stash clear // delete all stashes at once

git 提取patch.

  1. 可以先使用git citool本地提交。
  2. git format-patch -w head~1
  3. 使用 git reset --hard 撤销本地提交.
  4. 使用gitk查看是否撤销本地提交。
  5. 应用patch.
git diff --binary > xxx.patch //生成patch
git apply xxx.patch   //好用
git am xxxxxx.patch   //不好用.

git 回退

git reset HEAD~1
git reset HEAD^

(master分支向前回退一个提交。gqg:回退本地提交,恢复到提交前更改状态, 或者用:git reset HEAD~1 1表示回退一次提交,N表示回退N次提交).

  • 只要有.git目录 就可以使用git reset --hard 把代码恢复出来。

git 修改只有大小写区别的文件

git mv (--force) myfile MyFile
(mv: allow renaming to fix case on case insensitive filesystems)

revert某个提交:

  1. 使用gitk, 查看要revert的提交,把SHA1 ID复制下来.
  2. git revert -n xxxxxxxxxxxxxxxxxxxxxxxxxxxx
  3. git citool 提交然后push.

cherry pick

git cherry-pick "branch_name"  //cherry pick "branch_name" 最上方修复,并提交
git cherry-pick "sha-id"  //cherry pick 某一次提交

回退制定版本

git checkout 'SHA' //回退到制定'sha'值

git 配置

  1. 修改git默认的编辑器nano为vim的方法git config --global core.editor vim
  2. 指定全局 ignore 文件 git config --global core.excludesfile '/Users/xx/.gitignore_global'
  3. 修改某个git库下的用户名和邮箱
git config user.name 'user-name'
git config user.email 'email'

相关文章

  • 组件化(第一篇)

    组件化 git 常用操作指令 cocoapods的基本使用 cocoapods本地私有库 一、git 常用操作指令...

  • git的常用操作

    git的常用操作

  • GIt 操作补充

    常用的git操作命令 常用的git操作命令已经能够满足日常的工作需求 现补充一些高级命令 git branch -...

  • git使用整理

    git使用常用操作-常用基本命令 克隆:git clone 【url】 查看修改状态: git status (g...

  • GIT 版本管理 常用命令

    Git 常用命令流程图 Git常用操作命令: 初始化创建:$ git init //检出仓库:$ git clon...

  • 🍏常用 git 操作指北

    ? 常用 git 操作指南 ? git图形界面操作软件 ? 开发流程 git clone 拉取项目代码,不必多说 ...

  • git常用操作 🎀

    git常用操作 ? 基本知识 查看git信息 修改git配置 提交类操作 分支类操作 创建分支 查看分支 修改分支...

  • git常用操作命令

    git常用操作命令 1 git工作原理图 2 git远程操作 2.1 git clone 远程操作的第一步,通常是...

  • 真 git 操作大全 不会就看

    git 常用命令git 远程操作git 撤销操作 推酷git大全 react router4 中文文档 koa 简...

  • git常用操作

    git缩写配置: 在.gitconfig文件中添加: [alias] git常用操作: git clone 克隆一...

网友评论

    本文标题:git 常用操作

    本文链接:https://www.haomeiwen.com/subject/pyfwsttx.html