######################################## Git ######################################## 相对位置 **************************************** 除了使用 commit id 作为绝对路径外,Git 还支持三种相对路径:-, ^ 和 ~ - \- 用来表示刚刚的 commit id - \~ 表示 commit id 前的某个 commit 例如 :: git checkout 6feba~2 表示 6feba 前两个 commit 常用命令 **************************************** - 远程强制覆盖本地:: git fetch --all && git reset --hard origin/master && git pull - 本地强制覆盖远程:: git push origin master --force - 查看两个版本的差异:: git diff ba9f93 f6a6ae - 查看指定文件的版本历史:: git log conf.py - 回退到指定版本:: git reset --hard ba9f93 - 回退文件到指定版本:: git reset ba9f93 conf.py git checkout - 更改子模块的上游仓库 :: 1. 直接修改 .gitmodules 文件 2. 运行 git submodule sync - 下载指定文件夹的数据 :: 1. 复制链接,并将 tree/master 替换为 trunk 2. svn co https://github.com/Mooophy/Cpp-Primer/trunk/ch03 - 取消暂存 :: git reset HEAD path 合并多个项目 **************************************** 需要将一个项目添加到另一个项目的子路径下,同时保存项目的 commit 历史 :: # Assume the current directory is where we want the new repository to be created # Create the new repository git init # Before we do a merge, we have to have an initial commit, so we'll make a dummy commit git commit --allow-empty -m "Initial dummy commit" # Add a remote for and fetch the old repo # (the '--fetch' (or '-f') option will make git immediately fetch commits to the local repo after adding the remote) git remote add --fetch old_a # Merge the files from old_a/master into new/master git merge old_a/master --allow-unrelated-histories # Move the old_a repo files and folders into a subdirectory so they don't collide with the other repo coming later mkdir old_a dir -exclude old_a | %{git mv $_.Name old_a} # Commit the move git commit -m "Move old_a files into subdir" # Do the same thing for old_b git remote add -f old_b git merge old_b/master --allow-unrelated-histories mkdir old_b dir –exclude old_a,old_b | %{git mv $_.Name old_b} git commit -m "Move old_b files into subdir" .. seealso:: - `git subtree - Merge two Git repositories without breaking file history `_ 将更改提交到新的分支上 **************************************** :: //步骤1:在当前的develop分支上的修改暂存起来 git stash //步骤2:暂存修改后,在本地新建分支(develop_backup为新分支的名字) git checkout -b develop_backup //步骤3:将暂存的修改放到新建分支中 git stash pop //步骤4:使用命令进行常规的add、commit步骤 git add. git commit -a "修改内容" //步骤5:将提交的内容push到远程服务器(在远程也同步新建分支develop_backup) git push origin develop:develop_backup - `Git:将当前修改的内容提交到新的分支上 `_ 合并部分文件 **************************************** 将分支 B 中的部分文件或文件夹合并到分支 A 上 :: git checkout A git checkout B public/** view/index.html 合并部分 commit **************************************** 合并指定分支的某个 commit 到另一分支 :: git cherry-pick commit_id 本地强制覆盖远程和远程强制覆盖本地 **************************************** 代码为 :: git push --force git pull --rebase 撤销 commit **************************************** 如果想撤销某次 commit,可以使用 :: git reset --soft commit_id git reset --hard commit_id 两者的区别是 soft 只是撤销了 commit_id,更改的代码会保留到 暂存区 撤销代码更改 **************************************** 使用下面代码可以撤销当前工作区的更改 :: git restore . 删除子模块 **************************************** 执行: .. code-block:: none git submodule deinit -f module_name git rm --cached module_name 子模块的使用 **************************************** Git 在添加子模块后会将子模块固定到特定的 commit_id 上,主代码库中不会显示发生在子模块中的更改。 如果克隆时忘记克隆子模块,只需要执行 :: git submodule update --init --recursive 如果需要更新子模块到更新的 id 上,执行 :: git submodule update --remote git submodule update --remote --merge 区别是第二种会使用远程覆盖掉本地的更改。 然后在主代码库中执行 :: git add . git commit 有时候我们需要在子模块中更改代码,然后提交到上游,只需要在子模块路径下 :: git checkout master # do some changes git add . git commit git push 然后在主仓库中正常提交即可 .. seealso:: - `Git - 子模块 `_ 撤销暂存 **************************************** 在添加文件后,使用以下命令撤销暂存 :: git reset HEAD files 修改 commit 信息和添加新文件到上个 commit ****************************************** 命令分别为 :: git commit --amend git add files git commit --amend git pull **************************************** 有时候 git pull 会失败。这时候只需要执行 :: git merge 并解决冲突即可 另外,git pull 相当于 git fetch + 快速合并 此问题的根本原因是上游代码发生了更改,然后下游在没有拉取代码的前提下又产生了新的 commit 撤销指定行 **************************************** 有些时候我们可能需要撤销指定行,使用 :: git checkout -p .. seealso:: - `git - Discard changes in one single line `_ 代码提交 **************************************** 在使用 CVS 提交代码时,应当遵循相关的规范以方便后期的查看,这种方式被成为 **约定式提交** 。 消息格式 [#angular_standard]_ ======================================== 每个提交消息都包含一个标题、一个正文和一个页脚。标题包括了类型、范围和主题: .. code-block:: none ():