一些 git 经验

  • 推荐 UI 工具:Sourcetree : free for Windows and Mac

  • 在做自己不熟悉的任何 -d 命令前,先要做好备份。。。

  • 常用命令

查看所有分支
git branch -a 
 
拿特定branch的代码
git checkout origin/daily/1.4.1

切换branch
git checkout #branch name#
(如果是线上已有的 branch,一定不要加 -b,加 -b 就是从当前 branch 新建的)

创建branch
git checkout –b #branch name#

删除branch
git branch –d #branch name#   

丢弃最新的commit
git reset --hard HEAD~1

Revert 最新的commit,去除commit,但是不丢弃改动本身
git reset --soft HEAD~1           

查看单个文件 history
gitk [filename]
or to follow filename past renames
gitk --follow [filename]

git uncommit 自己最近的 commit
如果自己最近 commit 了多个,就执行如下,其中 asdf 是自己最近第一条 commit 的前一条 commit
git reset --soft "asdf"         

把新添加的文件也stash
git stash -u  

查看某branch自己的提交历史
git log develop --author=your.name --oneline -10

git revert 已经 push 的 commit
git revert <commit_hash>
git revert <oldest_commit_hash>..<latest_commit_hash>
https://stackoverflow.com/questions/22682870/git-undo-pushed-commits

Git cherry-pick 多个 commit
git cherry-pick -n 90f121ae7 4a42a1d33 e257dcda6 6760de43d 0784f0cde 80a4c3bf6 321482012
加 -n 是为了 cherry-pick 过来后不自动 commit
cherry-pick 建议流程
	1. 多个 commit 同时 cherry-pick
	2. 如果中间提示有冲突,则 处理冲突,然后 git cherry-pick --continue
	3. 如果git cherry-pick --continue 提示本地 需要 stash/commit,则先 stash,在 continue         (不过用 commit 可能更好?因为会保留 commit 号码?)
cherry-pick 全部完成后,按照 stash 顺序重新应用到 本地   (如果前面不用 stash 而用 commit 就不需此)

查看 branch 的地址
git remote -v
origin  http://gitlab.alibaba-inc.com/DRDS/manager.git (fetch)
origin  http://gitlab.alibaba-inc.com/DRDS/manager.git (push)

查看一个 branch 内两个日期之间的 不同文件
https://stackoverflow.com/questions/1161609/how-can-i-get-the-diff-between-all-the-commits-that-occurred-between-two-dates-w
git diff --stat @{2018-10-24}..@{2018-8-1}
or
git diff --stat @{2.weeks.ago}..@{last.week}

git clone 指定 branch
git clone --single-branch -b branch host:/dir.git
如 git clone -b feature/support_cross_schema_query http://gitlab.my-inc.com/middleware/my_proj.git
  • git 要把一个 fork(repo) 的改动应用到另一个 fork

在本 repo 上 commit,但不 push
通过 source tree push 时,手动选择相应 branch。
http://xigua366.iteye.com/blog/2400153

  • 如果错误地把改动 commit 到了 HEAD detached 

https://stackoverflow.com/questions/4845505/gitx-how-do-i-get-my-detached-head-commits-back-into-master/4845543
    1. git reflog and git reflog --all will give you the commit hashes   找到你提交的 commit
        git merge HEAD@{1}   (后面数字从 git reflog 结果查看)将改动 merge 到当前 branch

  • Fatal Error when updating submodule using GIT

https://stackoverflow.com/questions/8197089/fatal-error-when-updating-submodule-using-git
修改 /Users/your.name/Documents/Work/my_proj/.git/config   和 
/Users/your.name/Documents/Work/my_proj/.gitmodules 里内容:
[submodule "my-calcite"]
    path = my-calcite
    url = http://gitlab.my-inc.com/my_proj/my-calcite.git
    branch = master
改完之后,一定要执行  git submodule sync

  • 在 sourcetree 上查看两个 commit 之间的不同

在 history 页面只显示当前 branch 的,然后按    (OSX) 选中任意两个 commit,就可以查看二者之间的不同了。

  • 如果 sourcetree git 拉代码突然没权限,可能是改了系统用户密码的原因

这是需要通过命令行拉一次,就可以了。

  • 如果碰到 本地并无自己的改动,但从一个干净的远程 branch 切换到另一个干净的 branch 时,出现:

则直接采用  force
git checkout -f another-branch

  • 如果 git pull 一个干净 branch 时出现

error: The following untracked working tree files would be overwritten by merge:
        my-calcite/src/main/java/org/apache/calcite/rel/core/SemiJoin.java
        my-calcite/src/main/java/org/apache/calcite/rel/metadata/RelMdPredicates.java
把本地相应文件删除再 pull
或者:
The only commands that worked for me were:
git fetch --all
git reset --hard origin/{{your branch name}}
如:git reset --hard origin/my_dev_mpp_stable_partialy_merged

  • 查看哪些文件被 ignore 了

git status --ignored

  • ignore 的几个地方

1. 工程 git 库根目录
work/project_A/ .gitignore 

扫描二维码关注公众号,回复: 8666280 查看本文章

2. 总目录

/Users/your_name/.gitignore_global

3. exculde 文件

work/manager2/manager/.git/info/exclude
ignore 只会ignore 未 add 的文件,已经add 的文件修改的话不会被 ignore。

例:想把 idea 产生的文件都 ignore 掉,则修改 project_A/.git/info/exclude 文件为:

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store
.idea
*.iml
target
发布了34 篇原创文章 · 获赞 0 · 访问量 1381

猜你喜欢

转载自blog.csdn.net/justsomebody126/article/details/103838929