Git使用总结

添加文件到Git仓库,分两步:

使用命令git add <file>,注意,可反复多次使用,添加多个文件;
使用命令git commit -m <message>,完成。

回退到上一个版本
git reset --hard HEAD^
回退到上上个版本
git reset --hard HEAD^^
回退之前几个版本
git reset --hard HEAD~n
回退到指定版本
git reset --hard <number>

查看命令历史
git reflog

改乱了工作区某个文件的内容,想直接丢弃工作区的修改时
git checkout -- file

不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步:
git reset HEAD <file>
git checkout -- file

从版本库中删除文件,如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。
git rm <file>

添加远程库
git remote add <git branch> <git address>

查看已添加的远程库
git remote

删除远程库
git remote rm <git address>

查看当前分支(*)
git branch

创建分支
git branch <branch name>

切换分支
git checkout <branch name>

创建+切换分支
git checkout -b <branch name>

合并某分支到当前分支
git merge <branch name>

删除分支
git branch -d <branch name>

查看当前分支历史
git log --graph --pretty=oneline --abbrev-commit

查看指定分支历史
git log --graph --pretty=oneline --abbrev-commit <branch name>

取回origin主机的next分支,与本地的master分支合并,如果远程分支(next)要与当前分支合并,则冒号后面的部分可以省略。
git pull <远程主机名> <远程分支名>:<本地分支名>

查看已经暂存起来的文件(staged)和上次提交时的快照之间(HEAD)的差异
git diff --cached
git diff --staged

显示工作版本(Working tree)和HEAD的差别
git diff HEAD

git远程仓库回退

1.git clone 远程分支master

2.git checkout 提交错误的分支

3.git reflog

4.根据操作日志查看想要回退的版本号(或远程提交记录里的版本号)

5.git reset --hard 需要回退到的版本号

6.git push -f

原文地址:https://www.cnblogs.com/Lxk0825/p/10606138.html