git 的学习和使用

一、git 配置

1. 生成ssh-key 并上传到 git服务器上

#cd $HOME

#ssh-keygen -t rsa -C "youremail@example.com"

-t 是类型,-C 是注释

这样就会在/$HOME/.ssh 生成ssh-key,然后我们将生成的公钥id_rsa.pub 粘贴到git服务器上就可以了。

2. 如果出现如下错误,可以配置:

 可以在~/.ssh目录下面,新建一个config文件。里面这样写:
Host *
KexAlgorithms +diffie-hellman-group1-sha1

3. 配置gitLog模板。

cd ~

touch .gitCommit

git config --global  commit.template   ~/.gitCommit

执行git config -l,可以看到多了一行commit.template=~/.gitCommit 的信息
当提交代码时,执行git commit 
这时就会出现交互提交模板,照既定的格式填写必要的内容保存就可以了。

4. 另外 ,git config 可以配置好多内容,通过 git config --list 可以查看到很多配置项

git config --global user.name "name"

git config --global user.email  "email"

git config --global  core.editor vi

git config --global core.whitespace cr-at-eol    #忽略文件中的 ^M

上述这些配置选项都会保存在 ~/.gitconfig 文件中

二、git 的使用

引用博客:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

1. 提交代码

先下载: git clone git@github.com:userName/skills.git

修改代码后,

将文件从工作区提交到暂存区:git add fileName

将文件从暂存区提交到本地版本库:git commit -m "log"

从本地版本库提交到远程仓库:请看下面 git push

2. 在本地回退版本

回退到指定版本,重置暂存取和工作区: git reset  --hard  Commit_ID

回退到上一版本,只重置暂存区: git reset HEAD^

回退到上上版本,不会重置暂存区: git reset --soft HEAD~2

撤出暂存区的文件到工作区: git reset HEAD fileName

3. 提交代码

查看远程主机名,以及网址: git remote -v

查看所有分支名: git branch -a

更新代码: git pull

git pull  远程主机名  远程分支名:本地分支名

提交代码: git push

git push  远程主机名  本地分支名:refs/heads/远程分支名

特别说明:

git push remote_name local_branch:refs/for/remote_branch

// refs/for 的意义是我们提交代码后,是需要经过code review之后才能merge到服务器的,而refs/heads 不需要。我在repo服务器下测试有效,在gitHub上测试没效果

4. 创建和合并分支

查看分支:    git branch

创建分支:    git branch branchName

切换分支:    git checkout branchName

创建+切换分支:git checkout -b branchName

合并某分支到当前分支:git merge branchName

                                       git merge --no-ff origin/featur/branch_xx

删除分支:git branch -d branchName

 

5. 查看提交后的代码日志以及修改

使用 git log 可以参考,介绍的很详细

https://blog.csdn.net/north1989/article/details/53355346

推荐命令:

git log  -5  --oneline

git log  --oneline  --graph

git log -5 --name-only

git show  commit_Id  -- file_name    # 个人测试是在 git 的顶层目录下使用

git show  commit_Id    --name-only

git diff  commi_id_1  comit_id_2  -- file_name  # 比较两个commit 中文件的差异

git log --author=UserName

git log  --commitor=Name

git log --since=2018-09-12

git log --since=1days

注:除了--since,还有 --after  --before --until

git log --after="2018-11-12 12:13:15"  --before="2018-12-01 00:00:00"

取值可以是xxxdays , xxxweeks ,  2016-11-25 , 或 2 years 1 day 3 minutes ago

 

简单介绍:

git log --name-only  -2

--name-only 仅在提交信息后显示已修改的文件清单; -2 则仅显示最近的两次更新;

git log --pretty=oneline

只是用一行显示每次提交的日志,等同于git log --oneline

git log -p

 -p 选项展开显示每次提交的内容差异,相当于git show 了所有提交文件;

参考博客:https://www.git-scm.com/book/zh/v1/Git-基础-查看提交历史

6. 其他

1>git revert commit_ID

#撤销某一次的提交,这相当一个重新提交??

2>暂存本次修改,并更新后,再提交。

使用 git stash 将修改“储藏”起来;

使用 git pull 拉取远程库上最新的代码;

使用 git stash pop 重新应用储藏;

修改冲突;

提交(git add);

7. 推荐阅读的博客,关于git:

http://blog.csdn.net/zeroboundary/article/details/10552115

http://www.cnblogs.com/qianqiannian/p/6008140.html

posted @ 2019-03-07 19:44  靖意风  Views(176)  Comments(0)    收藏  举报