git常用命令

克隆分支(ssh方式)

git clone git@xxx:branch.git#xxx代表服务器

设置全局的用户名和邮箱

git config --global user.name 'name'
git config --global user.email 'email'

查看已设配置

git config --list

查看远程分支的git路径

git remote -v

查看当前版本的状态(是否修改)

git status

查看日志

git log

查看具体修改

git log -p commit值
如:
git log -p dd1ea518b70795b1060589d2aeaaaef6d4ff2afc

查看n条日志

git log -n

查阅最近修改的文件

git log --stat

将日志按照格式导入到文件中

复制代码
git log --pretty=format:'%h was %an, %ar, message: %s' > git.log

git log --pretty=format:'%h was %an, %ar, message: %s' > d:/git.log

#查看最近七天提交的信息
git --pretty=format:'%h was %an,%ai, message: %s' --since="7 day ago"

#查看某个人最近七天提交的信息
git log --pretty=format:'%h was %an,%ai, message: %s' --since="7 day ago" --author='joshua317'

#查看匹配grep后面字符串的最近7天提交的信息
git log --pretty=format:'%h was %an,%ai, message: %s' --since="7 day ago" |grep 'joshua317'

#查看某个人最近七天提交的部分信息
git log --pretty=format:'%ai, message: %s' --since="7 day ago" --author='joshua317'
复制代码

查看修改:

git show

git show e554c39fd6144fc9358b319786598481dee1937b

拉取分支代码

复制代码
1.拉取并合并
git pull

2.拉取,比较,不合并
git fetch
git diff origin/master --stat
git diff origin/master -- filename #查看更改的内容
复制代码

查看本地分支:

git branch

查看远地分支:

git branch -r

查看本地分支和远程分支

git branch -a

查看各个分支最后一次提交

git branch -v

查看两个分支

git diff branch_a branch_b

创建本地分支

git branch branch_name

切换分支

git checkout branch_name

创建并切换本地分支

git checkout -b branch_name

删除文件,并把它从git的仓库管理系统中移除

git rm file

强制删除文件,并把它从git的仓库管理系统中移除

git rm -f file

删除文件夹folder,并把它从git的仓库管理系统中移除。

git rm -r folder

删除本地分支

git branch -d | -D branch_name  #-D代表强制删除

删除远程分支

git push origin --delete branch_name

推送分支

git push origin branch_name

push本地分支代码到远端服务器

git push branch_name origin/branch_name

合并分支

git merge branch_name # 将branch_name分支合并到当前分支中去

重命名分支

git branch -m|-M oldbranch_name newbranch_name #-M用来强制重命名

取远程分支并分化一个新分支(With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy)

git checkout -b branch_name origin/branch_name

添加文件到分支

git add file

提交文件

git commit -m '注释'

添加并提交到分支

git commit -am '注释'

显示本地分支和服务器分支的映射关系

git branch -vv

合并本地master分支到当前分支,比如当前处于dev分支,则把master上的代码merge到dev分支上

git merge master

合并远程master分支到当前分支

git merge origin/master

备注:

git merge –no-ff 可以保存你之前的分支历史。能够更好的查看 merge历史,以及branch 状态。

git merge 则不会显示 feature,只保留单条分支记录。

强制回滚命令

git reset --hard b188005656968504c81eefe202e0a04205803e51(某次提交的号)

查看标签

git tag
或者
git tag -l

打标签

git tag -a v0.01 -m "Relase version 0.0.1"

切换标签,与切换分支命令相同,用git checkout [tagname]

git checkout v0.0.1

提交标签到远程仓库,注解:就像git push origin master 把本地修改提交到远程仓库一样,-tags可以把本地的打的标签全部提交到远程仓库。

git push origin --tags

删除标签,注解:-d 表示删除,后面跟要删除的tag名字

git tag -d v0.0.1

删除远程标签,注解:就像git push origin:branch_name 可以删除远程仓库的分支branch_name一样, 冒号前为空表示删除远程仓库的tag。

git push origin :refs/tags/v0.0.1

git不区分文件名大小写解决

git mv -f myfile MyFile (推荐)
然后commit就好了。

或者修改配置

git config core.ignorecase false

删除一些 没有git add 的 文件

复制代码
git clean 参数

-n 显示 将要 删除的 文件和目录
-f 删除文件
-df 删除文件 和 目录

git clean -n

git clean -df

git clean -f
复制代码

使用过程中遇到的问题汇总:

1.推送的时候出现失败

复制代码
$ git push
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'git@xxx:xxx.git'

解决:
git add .
git commit -am 'push'
git push origin master
复制代码

2.假设存在文件或git仓库( existing folder or Git repository)

cd existing_folder
git init
git remote add origin git@yourserver:yourproject #红色部分视情况而定
git add .
git commit
git push -u origin master

 来自:https://www.cnblogs.com/joshua317/p/4928930.html(了解学习使用)

原文地址:https://www.cnblogs.com/tutubaobao/p/10330056.html