本篇文章主要收集记录git常用命令,也是在git使用过程中踩的坑:
1:想要把本地仓库的文件check下来:
git checkout main -- ./xxx/sss.v 【一般本地文件做了修改但是不想要了】
2:要比较两个版本之间文件的区别:
git diff 版本号1 版本号2 ./xxx/sss.v
3:git rm 把本地库上有的【但是本地文件没了】文件(红色deleted)删除掉;然后再git ci
4:git branch 查看当前分支
5:git alias
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.logl 'log --oneline'
然后在~/.gitconfig 文件中就有
这样就能比较快的操作git了,根据个人习惯去操作就行。
6: 报warning
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
‘matching’ 参数是 Git 1.x 的默认行为,其意是如果你执行 git push 但没有指定分支,它将 push 所有你本地的分支到远程仓库中对应匹配的分支。而 Git 2.x 默认的是 simple,意味着执行 git push 没有指定分支时,只有当前分支会被 push 到你使用 git pull 获取的代码。
修改git push行为即可:
git config --global push.default matching
7:git push冲突报错:
一个解决办法:
git fetch -all
git reset -hard origin/main
git pull
上述操作就把远端仓库最新的版本pull到本地了;【最好是把本地的文件做一下备份】
8:理解git push的行为,其实是把本地仓库的版本push到remote仓库,本地仓库每次ci log也就跟随版本push到了远端仓库上。
9:git pull的时候报:
Your local changes to the following files would be overwritten by merge
意思是说你本地有改动,让你先ci以后再pull,但是你如果想拉最新的到本地,那你需要先把本地不想要的删除或者移动走,然后再pull,git不会pull新的文件来覆盖你的旧的文件。
9:撤回commit操作:
git reset --soft HEAD~1
作用是把该文件的改动丢弃掉
10:git查看当前版本和上一个版本之间更新了那些内容:
git show xxxxx_当前版本的commit号
11:本地对文件进行了修改,想知道此时和库上文件的区别:
git st 得到变动的文件信息,然后git diff -- xxx.sv 就打印出来了文件的变化内容
12:想要pull指定版本的代码到本地
——TBD
13:删除已经push到分支上的文件/文件夹(保留本地文件)
1. git rm --cached 文件(夹)名,此时只删除了仓库中的缓存,实际文件不会删除
2. git commit -m '备注'
3. git push origin 分支