Git 使用帮助(下)

Posted 暗夜影

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git 使用帮助(下)相关的知识,希望对你有一定的参考价值。

远程库

一,获取Git远程仓库
1.注册GitHub账号
2.创建SSH key
$ ssh-keygen -t rsa -C "[email protected]", 设置默认选项即可。
$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/thinkpad/.ssh/id_rsa):
/c/Users/thinkpad/.ssh/id_rsa already exists.
Overwrite (y/n)?

生成成功后,在.ssh的目录中会有id_rsa和id_rsa.pub两个文件,分为SSH的私钥和公钥。

3.登录GitHub,在setting中的SSH keys选项,点击“New SSH key”,输入title和key
,title无要求,key中将生成的id_rsa.pub中的内容全部复制进入即可。点击“Add SSH key”完成添加。GitHub支持添加多个key。

4.添加远程库,点击右上角的“+”号中的“New Repository”创建一个新的仓库。

5.关联本地仓库,参照git的提示在git bash中输入(确认本地库已创建)
$ git remote add origin [email protected]:zhang1988pan/git-test.git

$ git push -u origin master
Warning: Permanently added the RSA host key for IP address 192.30.255.113 to t                                                      he list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 225 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:zhang1988pan/git-test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

以后每次提交到远程库时使用 git push –u origin mster即可。

6.远程库克隆本地,在GitHub中创建一个名为git-server的新库
$ git clone [email protected]:zhang1988pan/git-server.git
Cloning into git-server...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Receiving objects: 100% (3/3), done.

二、分支管理

HEAD指针总是指向当前分支,创建分支时,HEAD指向分支,合并时指向master。
1.    git checkout –b dev创建名为dev的分支 
$ git checkout  -b dev
Switched to a new branch dev
2.    git branch 查看当前分支,星号所指
$ git branch
* dev
  master
3.    在test.txt中增加一行文字,add和commit
4.    合并,git checkout master切换主分支,git merge dev合并文件

$ git checkout master
Switched to branch master
Your branch is up-to-date with origin/master.

[email protected] MINGW64 /e/Git/gittest/gittest (master)
$ git merge dev
Updating 4ed0e02..c93ee8b
Fast-forward
 test.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

[email protected] MINGW64 /e/Git/gittest/gittest (master)
$ git status
On branch master
Your branch is ahead of origin/master by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

5.    删除分支,git branch –d dev
$ git branch -d dev
Deleted branch dev (was c93ee8b).

6.    当master分支和dev分支都有新的提交时,可能会有冲突,无法自动合并,需手动解决冲突。所有开发工作不建议在master上进行
7.    git stash临时保存未完成的分支工作
$ git stash
Saved working directory and index state WIP on dev: 13be0fe confict fixed
HEAD is now at 13be0fe confict fixed

[email protected] MINGW64 /e/Git/gittest/gittest (dev)
$ git status
On branch dev
nothing to commit, working tree clean

8.    git stash list查看保存的内容
$ git stash list
[email protected]{0}: WIP on dev: 13be0fe confict fixed

9.    git stash pop恢复保存的内容,同时删除保存记录
$ git stash pop
On branch dev
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/[email protected]{0} (bdcd2918d012a9b4ba12893beef30af13a58c978)

10.    git branch -D <name>强行删除一个未被合并的分支
11.     git remote –v 显示远程库的信息
$ git remote -v
origin  [email protected]:zhang1988pan/git-test.git (fetch)
origin  [email protected]:zhang1988pan/git-test.git (push)

 

以上是关于Git 使用帮助(下)的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

VIM 代码片段插件 ultisnips 使用教程

如何在片段中使用 GetJsonFromUrlTask​​.java

markdown Git代码片段