美文网首页
git 常用命令集合

git 常用命令集合

作者: FrankZX | 来源:发表于2019-12-07 21:16 被阅读0次

    1、gerrit 提交审核 git push origin HEAD:refs/for/master
    2、不改变chang-id的情况下,修改上一次推送 git commit --amend
    每个change-id对应一个review,可以手动修改chang-id提交覆盖上一版本的提交

    3、以图形化方式显示版本合并历史 git log --graph --pretty=oneline --abbrev-commit
    --- 用vscode配合Gitlens 插件能够更好地以图形化的方式显示提交历史等信息。

    4、默认情况下合并分支后,如果被合并的分支被删除则不会保留分支的信息,可以在merge的时候加上 --no-off 参数禁用 Fast Forward。
    例:
    git merge --no-ff -m "merge log" dev

    5、将代码回退到指定版本
    git reset --hard xxxxxxxxxxxxxxxxxxxxxxxx

    git diff xxxxx > patch
    git apply patch

    合并提交:
    git rebase -i HEAD~2
    更新到某个tag
    git checkout -b new_branch tag

    6、生成change-id
    git rebase

    7、
    git log --graph --pretty=oneline --abbrev-commit

    8、Git合并某个分支的一个cherry-pick到另一个分支
    例如要将A分支的一个commit合并到B分支:
    首先切换到A分支
    git checkout A
    git log
    找出要合并的commit ID :
    例如 0128660c08e325d410cb845616af355c0c19c6fe
    然后切换到B分支上
    git checkout B
    git cherry-pick 0128660c08e325d410cb845616af355c0c19c6fe
    然后就将A分支的某个commit合并到了B分支了

    转移多个提交
    Cherry pick 支持一次转移多个提交。
    git cherry-pick <HashA> <HashB> 上面的命令将 A 和 B 两个提交应用到当前分支。这会在当前分支生成两个对应的新提交。 如果想要转移一系列的连续提交,可以使用下面的简便语法。 git cherry-pick A..B
    上面的命令可以转移从 A 到 B 的所有提交。它们必须按照正确的顺序放置:提交 A 必须早于提交 B,否则命令将失败,但不会报错。
    注意,使用上面的命令,提交 A 将不会包含在 Cherry pick 中。如果要包含提交 A,可以使用下面的语法。
    $ git cherry-pick A^..B

    9、从库里删除但是不物理删除
    git rm --cached xxxx

    10、更新并合并
    git pull --rebase
    原理:把本地当前分支的未推送 commit 临时保存为补丁 (patch)(这些补丁放到 .git/rebase 目录中),然后将远程分支的代码拉取到本地,最后把保存的这些补丁再应用到本地当前分支上。
    若要把 rebase 当做 git pull 的预设值,可以修改 ~/.gitconfig 让所有 tracked branches 自动使用该设定:
    [branch]
    autosetuprebase = always

    11、Windows下面使用git拉取或者提交项目时,遇到长路径提示file name too long的解决方案
    可以使用以下命令来修复:
    git config --system core.longpaths true

    也可以仅设置当前项目:
    git config core.longpaths true

    查看设置状态:
    git config core.longpaths

    (git-cheatsheet_Page_2.png-26b069-1526711607289-0)
    git-cheatsheet_Page_2.png

    12、若要查看已暂存的将要添加到下次提交里的内容,可以用 git diff --cached 命令。
    (Git 1.6.1 及更高版本还允许使用 git diff --staged,效果是相同的,但更好记些。)

    13、Git Diff 的插件版本
    可以使用 git diff 来分析文件差异。 但是,如果你喜欢通过图形化的方式或其它格式输出方式的话,可以使用 git difftool 命令来用 Araxis ,emerge 或 vimdiff 等软件输出 diff 分析结果。 使用 git difftool --tool-help 命令来看你的系统支持哪些 Git Diff 插件。

    14、删除branch
    git branch -d/D xxxxx

    15、重命名本地branch
    git branch -m xxxxxx

    16、新建本地分支并推送到远程
    git checkout -b test-branch
    git push origin test-branch:test-branch
    本地新建branch,推送到gerrit上:
    确保推送的commit id 在库中可以找到,然后再在此基础上增加commit。

    17、和远程库同步删除的tag
    git fetch --prune --tags
    向远程库推送tag
    git push origin <nameOfYourTag>
    删除本地库tag
    git tag -d tag_name

    18、
    refs/changes/[CD]/[ABCD]/[EF]
    Where:
    [CD] is the last two digits of the change number
    [ABCD] is the change number
    [EF] is the patch set number

    You can use the change reference to fetch its corresponding commit:

    git fetch https://[GERRIT_SERVER_URL]/[PROJECT] refs/changes/[XX]/[YYYY]/[ZZ] \ && git checkout FETCH_HEAD

    Your ref spec in repo can be read with: git ls-remote | grep $(git rev-parse HEAD).

    19、git always says "Warning: Permanently added '...' (RSA) to the list of known hosts."
    解决办法:create a ~/.ssh/config file and insert the line:

    UserKnownHostsFile ~/.ssh/known_hosts

    20、git add
    Summary:

    git add -A stages all changes

    git add . stages new files and modifications, without deletions

    git add -u stages modifications and deletions, without new files

    21、git设置默认编辑为vim

    If you want to set the editor only for Git, do either (you don’t need both):

    • Set core.editor in your Git config: git config --global core.editor "vim"
    • Set the GIT_EDITOR environment variable: export GIT_EDITOR=vim

    22、sync delete tags with remote repo:
    git fetch origin --prune --tags

    push only the desired TAG:
    git push origin <tag name>

    23、推送特定的一个commit到远程库
    git push <remotename> <commit SHA>:<remotebranchname> works. the trick is to combine it with git rebase -i to move the commit you want as the first commit, and specify that commit-sha.

    24、git stash -p
    交互式stash部分文件

    25、解决冲突,应用某个版本:
    从一个特定分支获取文件拷贝(比如说你在合并master和feature123两个分支):

    git checkout master flash/foo.fla # 或者... git checkout feature132 flash/foo.fla
    # 然后... git add flash/foo.fla

    另一种方式是通过git输出文件 - 你可以输出到另外的文件名,然后当你决定了要用哪个后,再将选定的正确文件复制为正常的文件名:

    git show feature132:flash/foo.fla > feature132-foo.fla # 检出master-foo.fla和feature132-foo.fla
    # 假如说我们决定来自feature132的文件是正确的 mv feature132-foo.fla flash/foo.fla
    $ git add flash/foo.fla

    也可以用“git checkout —ours flash/foo.fla”和“git checkout —theirs flash/foo.fla”来检出特定版本的文件,而不用记住你在合并的分支名字。

    26、git reflog --date=iso

    27、git shortlog -sn 命令可以列出代码仓库的提交者统计

    相关文章

      网友评论

          本文标题:git 常用命令集合

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