git 基本命令

git init 初始化本地仓库,生成.git目录

克隆代码到本地:
git clone <repo> 克隆远程仓库。
git clone <repo> <directory> 克隆远程仓库到指定目录

添加代码到暂存区:
git add <file> 添加文件
git add . 添加所有文件
git add -u 添加所有修改的文件
git add -u *.cpp 添加修改的后缀为cpp的文件

提交代码到版本库:
git commit -m "" 提交文件
git commit -am "" 合并add和commit(不建议使用)

推送代码到远程:
git push origin master 推送分支master到远程仓库origin
git push origin --delete (branchname) 删除远程分支

更新代码:
git pull:是从远程获取最新版本到本地,并自动merge;
git pull origin (branchname) 更新分支代码
git fetch:是从远程获取最新版本到本地,不会自动merge;
git merge:远端仓库提取数据并尝试合并到当前分支
git merge (branchname) 合并分支到当前目录

撤销reset
git reset HEAD 清除暂存区已缓存的内容
git reset –hard 撤销到最新一次commit
git reset –hard commitId 撤销某次提交并撤销代码修改,未push
git reset commitId 完成Commit命令的撤销,但是不对代码修改进行撤销,可重新commit,未push

撤销提交commit且push
git revert [commitid]

查看最新提交commit代码差异
git show 查看最新的commit
git show commitId 查看指定commit hashID的所有修改
git show commitId fileName 查看某次commit中具体某个文件的修改

创建分支branch:
git branch (branchname) 创建分支
git branch -d (branchname) 删除本地分支
git branch 查看本地分支
git branch -a 查看本地和远程所有分支
git branch <branch> <start point> 以某个commit创建新分支
git branch -m oldbranch newbranch 修改分支名称

切换分支:
git checkout (branchname) 切换分支
git checkout -b (branchname) 创建并切换分支
git checkout -b new_branchname commitid 从某次提交拉取分支
git checkout -B <branch> 强行创建分支,覆盖掉原来分支
git checkout -p <branch> 这个命令主要用来比较两个分支间的差异内容,并提供交互式的界面来选择进一步的操作
git checkout --merge <branch> 在切换分支的时候,将当前分支修改的内容一起打包带走,同步到切换的分支下,容易引起冲突,当前分支修改丢失。慎用。
git checkout --datch <branch> 切换到分支的游离状态,默认以该分支下的最后一次提交ID

文件检出:
git checkout <commit> --filepath 缺省commit 从暂存区替换文件到工作目录
不缺省commit 用于指定版本的文件覆盖工作区中对应的文件
commit可以是具体的commit hash值,分支名称,tag名称
git checkout . 从版本库检出所有文件到工作目录,会覆盖工作目录文件
git checkout filepath 从版本库检出文件,会覆盖工作目录文件
git checkout <commitId> 工作目录切换到某次提交版本,分支处于游离状态(游离状态不能提交代码,会丢失),重新切换回分支解除游离状态

日志管理:
git log 提交历史记录
git log --oneline 简洁的提交历史记录
git log --reverse --oneline 反转时间提交历史记录
git commit --amend 修改已经提交的日志信息

文件管理:
git rm -f <file> 删除暂存区和工作目录的文件
git rm --cached <file> 只删除暂存区中的文件,不从工作区中删除文件
git mv 命令用于移动或重命名一个文件、目录、软连接。

比较两次提交的差异
git diff <filename> 未add,修改文件
git diff HEAD -- <filename> 已经add,未commit的文件
git diff HEAD^ -- <filename> 已经add, commit后的文件

# 远程仓库管理
git remote add origin git@github.com:tianqixin/runoob-git-test.git 添加远程仓库
git remote set-url origin git@github.com:tianqixin/runoob-git-test.git 设置远程创库的url
git remote rm [别名] 删除远程仓库
git remote 查看有哪些远程仓库
git remote -v 每个别名的实际链接地址

设置命令别名:
$ 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 -e 该命令通过修改配置文件设置别名

删除未track的文件:
git clean -f 删除文件
git clean -f filepath 删除文件
git clean -df 删除目录和文件

查看某个文件的修改记录:
git log 文件名 查看该文件提交记录
git log --pretty=oneline 文件名 查看该文件提交记录
git show commitid 查看具体修改内容
git log -p filename 既显示修改记录和修改内容
git show c5e69804bbd9725b5dece57f8cbece4a96b9f80b filename 只显示本次提交该文件的改动内容

将当前分支的修改合并到其他分支
git stash
git checkout branchname
git stash pop

配置github:
1 ssh-keygen -t rsa -C “youEmail” 配置SSH key,在根目录下生成.ssh文件下生成密钥
2 cp ~/.ssh/id_rsa.pub 拷贝内容到github添加到key中
3 设置username和email,因为github每次commit都会记录他们
git config --global user.name "github的用户名"
git config --global user.email"注册邮箱名"

submodule操作
git submodule update
git submodule update --init --recursive

其他一些命令:
git status
git status -s 简单显示
git status -uno 只显示修改文件
git config --list 查看配置

修改最后一次提交时间:
date -R 命令计算当前时间为"Wed, 29 Jul 2020 14:55:15 +0800"
git commit --amend --author="wangjian <1076987915@qq.com>" --date="Wed, 29 Jul 2020 16:55:15 +0800"

冲突解决方法:
1 commit your changes or stash them before you can merge.冲突解决办法
git stash 备份当前工作目录修改,恢复版本
git pull
git stash pop 恢复修改内容到工作目录

4.找到你本地的pom.xml文件,并打开

你会在文件中发现<<<<<<< HEAD ,=======  ,>>>>>>> ae9a0f6b7e42fda2ce9b14a21a7a03cfc5344d61

这种标记,<<<<<<< HEAD和=======中间的是你自己的代码,  =======  和>>>>>>>中间的是其他人修改的代码

自己确定保留那一部分代码,最后删除<<<<<<< HEAD ,=======  ,>>>>>>>这种标志

2 强制更新,丢弃本地修改代码
git fetch --all
git reset --hard origin/master

原文地址:https://www.cnblogs.com/whwywzhj/p/10346347.html