Git常用命令

Git中文文档

设置与配置

git config --list 列出所有git配置项

git config --global user.name "yangliuan" 配置用户名

git config --global user.email "yangliuancn@foxmail.com" 配置用户邮箱

git config --global core.editor vim 配置默认编辑器

git config --global core.filemode false 所有版本库忽略文件权限跟踪

git config core.filemode false 当前版本库忽略文件权限跟踪

建立版本库

git init 新建项目

git clone 从线上拉取一个项目

git clone -b 分支名称 仓库地址 [文件夹名称]     克隆指定分支  文件夹名称选填不需要重命名的时候不填

git add . 监控工作区的状态树,把工作区的所有变化提交到暂存区包括文件内容修改(modified)以及新文件(new),但不包括被删除的文件

git add -u  仅监控已经被add的文件(即tracked file)他会将被修改的文件提交到暂存区

git add -A 是上面两个功能的合并(git add --all的缩写)

提交代码

git commit -m "注释"

git push 推送

git push origin  [branch] 推送到远程指定仓库

git push -u origin master  推送到远程指定仓库并设置为默认推送分支

git pull 拉取

git pull origin [branch] 拉去远程代码

git pull = git fetch + git merge

git pull --rebase = git fetch + git rebase

git rebase用于把一个分支的修改合并到当前分支。

仓库管理

git remote rename oldname newname 重命名仓库

git remote rm name  删除仓库

git remote set-url origin git@xxxxx.git

更换仓库出现如下错误,原因是两个分支是两个不同的版本,具有不同的提交历史

Git :fatal: refusing to merge unrelated histories
//使用如下命令其强制合并
git pull origin master --allow-unrelated-histories
如果还有问题,出现合并冲突之类的
删除本地仓库中的.git文件 重新clone新仓库到新目录,将代码文件和.git文件移动到旧的本地仓库中

分支管理

git push origin --delete branchname 删除远程分支

git checkout branchname 切换分支

git branch -D branchname 删除本地分支

git checkout -b branchname 在当前分支基础上新建分支

git push --set-upstream origin branchname 将新建的本地分支推送到远程并关联

拉取所有远程分支

git clone只能clone远程库的master分支,无法clone所有分支,解决办法如下:

找一个干净目录,假设是git_work
cd git_work
git clone http://myrepo.xxx.com/project/.git ,这样在git_work目录下得到一个project子目录
cd project
git branch -a,列出所有分支名称如下:
remotes/origin/dev
remotes/origin/release
git checkout -b dev origin/dev,作用是checkout远程的dev分支,在本地起名为dev分支,并切换到本地的dev分支
git checkout -b release origin/release,作用参见上一步解释
git checkout dev,切换回dev分支,并开始开发。
git tag 列出所有tag

git tag -a 20190815 -m "注释" f8a5b56af1e962744d3dbecb19d971496715b260
指定commit 打标签 

git show xxx   查看指定名称tag

git push origin --tags  推送本地所有tag

git push origin [tagName]  推送单个tag

git clone --branch [tags标签] [git地址] 拉去指定tag的项目

git clone -b 标签名称 --depth=1 git地址 拉去指定tag的项目 --depth 表示克隆深度, 1 表示只克隆最新的版本. 因为如果项目迭代的版本很多, 克隆会很慢

强制回退到制定版本

git log 查看日志,找到你要回退的版本查看commit 编号

git reset --hard commit编号 回退到指定版本

git push -f -u origin master 强制提交到master分支,注意保存版本代码,回退之后无法恢复,master分支可以替换成你要回退的远程分支

注意多个本地仓库时,都需要回退版本。比如你开发环境回退到xxx然后推送到远程仓库,此时,要在生成和测试环境上同意做回退操作保持和远程仓库一致,才能继续更新。

删除文件或目录

#--cached不会把本地的dirname删除
git rm -r --cached dirname

git commit -m 'delete dir'

git push -u origin master

将被.gitignore文件所忽略的文件从版本库中删除

git rm -r --cached .
git add .
git commit
git push  -u origin master

空目录内创建.gitkeep文件保证目录被纳入版本管理

使用git在本地新建一个分支后,需要做远程分支关联。如果没有关联,git会在下面的操作中提示你显示的添加关联。

关联目的是在执行git pull, git push操作时就不需要指定对应的远程分支,你只要没有显示指定,git pull的时候,就会提示你。

git branch --set-upstream-to=origin/remote_branch  your_branch

其中,origin/remote_branch是你本地分支对应的远程分支;your_branch是你当前的本地分支

git config advice.objectNameWarning false 待完善

其它命令

git symbolic-ref --short -q HEAD  打印当前分支名称,shell脚本用的上

git submodul

git submodule 的使用

FAQ

1错误信息

warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:

  git config pull.rebase false  # merge (the default strategy)
  git config pull.rebase true   # rebase
  git config pull.ff only       # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.

解决方案

git config --global pull.ff only

参考
https://stackoverflow.com/questions/62653114/how-to-deal-with-this-git-warning-pulling-without-specifying-how-to-reconcile

指令参考

上述文件转自EASY微博

https://learnku.com/articles/61909