一些 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

取消 merge
git reset --merge

checkout 某一个 commit 快照
git reset --hard commit_sha
  • 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 

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
  • 查看用户名和地址
git config user.name
git config user.email
  • 修改用户名和地址
git config --global user.name "your name"
git config --global user.email "your email"
  • git clone: fatal:protocol error: bad line length character: Welc

将 /etc/ssh/ssh_config 里面的
ForwardX11 yes 注释掉

  • git clone 指定分支

git clone -b dev_jk http://10.1.1.11/service/tmall-service.git

  • 查看一个分支从哪个分支拉出来的

git reflog show my_develop

 

git clone --depth=1

https://www.jianshu.com/p/1031dd2a6c3a

--depth 会带来问题,就是你本地无论如何 git fetch --all,用 git branch -r 都看不到 remote 的所有分支,也无法通过 git checkout 切换分支

git branch -a does not show all remote branches - Stack Overflow       --no-single-branch

切换账户

1.打开git bash。

2.可以查看用户名:git config user.name

3.切换用户:git config --global user.name "xxx"

4.切换邮箱:git config --global user.email "xxx"。

5.切换成功。

MAC 的 gitConfig 地址

$HOME/.gitconfig

或者使用:

git config --global --edit

碰到 Permission denied (publickey):

Error: Permission denied (publickey) - GitHub Docs

git pull 一直提示 504

error: RPC failed; HTTP 504 curl 22 The requested URL returned error: 504 Gateway Time-out
改用:

git pull web_url branch_name

如:

git pull http://gitlab.xxxxxxxxx-inc.com/middleware/tddl5.git xxxxx_develop
 

github 不支持在电脑上用用户名密码 push 了

参考此文档生成一个 token:

git - Message "Support for password authentication was removed. Please use a personal access token instead." - Stack Overflow

下面的密码输入生成的 token

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值