Git基本操作手册

生成、配置 SSH key

先使用 ssh-keygen 命令生成一个 key,记得带上你在相应 git 服务器的用户名,然后查看 .pub 结尾的文件,复制文件中的文本填写到 git 服务相应的设置(一般在设置→SSH-KEY)里面。

# 生成(POSIX系统)
ssh-keygen -t rsa -C "fzjlovemy@163.com"
# 查看
cat ~/.ssh/id_rsa.pub

# 生成(Windows系统)
ssh-keygen -t rsa -C "feizhaojun@xdf.cn"
# 查看
cat /c/Users/Mukti/.ssh/id_rsa.pub

# 指定存储路径
ssh-keygen -t rsa -C 'fzjlovemy@163.com' -f ~/.ssh/id_rsa_gitee

在 git 服务的 SSH-KEY 中填好之后,可以使用命令测试是否配置成功:

# 测试
ssh -T git@gitee.com
同设备访问多个git帐户配置SSH

在 ~/.ssh 目录下新建一个config文件,添加如下内容(其中Host和HostName填写git服务器的域名,IdentityFile指定私钥的路径)

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github

# 66xue
Host gitlab.66xue.com
HostName gitlab.66xue.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

用ssh命令分别测试

$ ssh -T git@gitee.com
$ ssh -T git@github.com

用户操作

git config user.name  # 查看用户名
git config --global user.name  # 查看全局用户名
git config user.name "Mukti"  # 设置用户名
git config credential.helper store  # 记住密码

基本操作

git init    # 初始化一个 git repo
git status # 查看状态
git log # 查看历史记录
git log --pretty=oneline # 单行简要显示一次记录
git reflog # 可以查看所有分支的所有操作记录,包括已经被删除的 commit 记录和 reset 的操作
git add <file> # 将一个文件添加到暂存区 stage
git commit -m -a # 提交,参数“-a”包括了“git add”
git rm </file><file> # git add 相反
git rm --cached </file><file> # unstage,但是保留物理文件
git diff # 查看文件变更(工作区和暂存区对比)
git diff --cached # git add 之后查看与上一版本的区别(暂存区和已提交对比)

悔改

git checkout -- <filename> # 撤销未暂存的文件(工作区)的修改
git reset --hard HEAD^  # HEAD指针上移一个,回退到上一个版本。^^回退前2个版本
git reset --hard HEAD~100  # 回退前100个版本
git reset --hard <commitid> # 回退到某个版本号
git reset --hard origin/master # 回退到远端仓库

远程仓库

git clone <url> # 克隆,会自动关联远程分支,pull或push无需指定远程分支
git remote # 查看关联的远程仓库
git remote -v # 查看关联的远程仓库的详细信息
git remote add <origin> git@gitee.com:mukti/mukti-test.git # 添加远程仓库的关联
git remote remove origin # 删除远程仓库的关联
git remote set-url origin <newurl> # 修改远程仓库的关联,也可以删除再添加,也可以修改.git目录的config文件
git branch --set-upstream-to=origin/master master # 关联本地与远程的分支,每次pull和push不需要指定分支
// git pull --allow-unrelated-histories # 远程库和本地库本来是独立的库,需要合并两个库
git pull <origin> <branchname>
git push # 推送当前分支到默认远程库的对应分支,如果远程不存在对应分支则报错
git push <origin> # 推送当前分支到远程库</origin><origin>的对应分支
git push </origin><origin> <branchname> # 将本地</branchname><branchname>推送到远程</branchname><branchname>分支,如果本地不存在</branchname><branchname>分支则报错
git push -u origin master # 参数“-u”将“origin master”设置为默认的远程仓库和分支,以后不需要再指定

分支

git branch # 列出 repo 中所有分支
git branch -a # 列出本地和远程所有分支
git branch </branchname><branchname> # 创建一个新的分支
git branch <newbranchname> <branchname> # 基于</branchname><branchname>分支创建一个新的分支
git brach </branchname><branchname> <commitid> # 基于当前分支的提交版本号创建新的分支
git branch -d <branchname> # 删除分支 -D 大写是强制删除,不出现合并提示

# 删除远程分支
git push <origin> :<branchname>
git push -d <origin> <branchname>
git push --delete <origin> <branchname>

# 切换到分支
git checkout </branchname><branchname>
git checkout -b </branchname><branchname> # 创建一个新的分支,并切换到新分支 相当于 git branch [name] + git checkout [name]

git checkout –b <newbranchname> <branchname> # 基于</branchname><branchname>分支创建一个新的分支
git checkout –b <newbranchname> <origin>/<branchname> # 基于远程的某个分支创建一个新的分支,从远程分支拉去的新分支push的时候会在远程查找同名分支,如果没有会提示,不会直接push到源分支。如果远程分支是新分支,需要先 pull

合并分支?

git merge </branchname><branchname> # 合并</branchname><branchname>到当前分支
git merge --abort # 放弃并入分支(非当前)的修改
git merge –no-ff -m "<comments>" <branchname> # 不使用Fast forward模式合并分支,合并的分支删除后,可以保留提交记录。
git merge <brancha> <branchb> # 将<brancha>分支合并到<branchb>分支

git rebase <branchname>

暂时隐藏工作

git stash # 隐藏当前工作
git stash list # 查看所以被隐藏的工作列表
git stash apply # 恢复被隐藏的工作,不删除隐藏工作列表中的记录
git stash drop # 删除隐藏工作列表中的记录
git stash pop # 恢复隐藏的工作并删除隐藏工作列表中的记录

HEAD

cat .git/HEAD
// git symbolic-ref HEAD
git checkout <hash>
git checkout master^
git checkout HEAD^
git checkout HEAD~4
git branch -f <branchname> HEAD~3

相关文章

学习路线

git init
git add
git commit
git status
git diff

git log
git reset --hard
git reflog

git checkout -- <filename>

git clone
git remote add
git push
git pull

git checkout
git branch
git merge

git stash

其他

git diff HEAD -- file
git reset  # 退回到工作区,撤销修改、找回文件,退回到最新版本,切换分支

# 显示信息类命令 
git ls-files -u 显示冲突的文件,-s是显示标记为冲突已解决的文件
git ls-files --stage 检查保存在stage的文件
git log -g则会查询reflog去查看最近做了哪些动作,这样可以配合git branch 恢复之前因为移动HEAD指针所丢弃的commit对象。如果reflog丢失则可以通过git fsck --full来查看没被引用的commit对象。 
git log -p -2 对比最新两次的commit对象 
log -1 HEAD
git log --stat 1a410e 查看sha1为1a410e的commit对象的记录
git blame -L 12,22 sth.cs 如果你发现自己代码中 的一个方法存在缺陷,你可以用git blame来标注文件,查看那个方法的每一行分别是由谁 在哪一天修改的。下面这个例子使用了-L选项来限制输出范围在第12至22行

# 撤销类命令 
如果是单个文件 
1.use "git reset HEAD <file>..." to unstage 
如果已经用add 命令把文件加入stage了,就先需要从stage中撤销
然后再从工作区撤销 
2.use "git checkout -- </file><file>..." to discard changes in working directory
git checkout a.txt  撤销a.txt的变动(工作区上的文件) 
如果是多个文件 
git chenkout .
如果已经commit 了,则需要 
git commit --amend 来修改,这个只能修改最近上一次的,也就是用一个新的提交来覆盖上一次的提交。因此如果push以后再做这个动作就会有危险
$ git reset --hard HEAD 放弃工作区和index的改动,HEAD指针仍然指向当前的commit.(参照第一幅图)
这条命令同时还可以用来撤销还没commit的merge,其实原理就是放弃index和工作区的改动,因为没commit的改动只存在于index和工作区中。
$ git reset --hard HEAD^ 用来撤销已经commit的内容(等价于 git reset --hard HEAD~1) 。原理就是放弃工作区和index的改动,同时HEAD指针指向前一个commit对象。
git revert 也是撤销命令,区别在于reset是指向原地或者向前移动指针,git revert是创建一个commit来覆盖当前的commit,指针向后移动


# 删除类命令
git mv a.txt b.txt 把a.txt改名为b.txt

# Merge类命令
在冲突状态下,需要解决冲突的文件会从index打回到工作区。
1.用工具或者手工解决冲突 
2.git add 命令来表明冲突已经解决。 
3.再次commit 已解决冲突的文件。
$ git reset --hard ORIG_HEAD 用来撤销已经commit 的merge. 
$ git reset --hard HEAD 用来撤销还没commit 的merge,其实原理就是放弃index和工作区的改动。
git reset --merge ORIG_HEAD,注意其中的--hard 换成了 --merge,这样就可以避免在回滚时清除working tree。

# 暂存类命令
通过git stash将工作区恢复到上次提交的内容,同时备份本地所做的修改,之后就可以正常git pull了,git pull完成后,执行git stash pop将之前本地做的修改应用到当前工作区。
git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。
git stash pop/git stash pop stash@{0}: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。
在出现一个搁置栈的情况下,比如如果你想找回栈中的第2个,可以用 git stash apply stash@{1}
git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。
git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。

FAQ

  • fatal: refusing to merge unrelated histories
    git pull origin master --allow-unrelated-histories

您的赞助将会支持作者创作及本站运维

发表评论


TOP