git学习及git hub使用

一。git使用:

1 提交到git暂存区: git add xxx.py

2 提交到git仓库: git commit -m 'this is a mark'

3 查看修改状态: git status

4 git暂存区回滚到工作区: git checkout xxx.py

5 查看历史记录: git log

6 查看历史提交记录,只看md5和提交mark:  git log --pretty=oneline

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ git log --pretty=oneline
f30e35dd53e39b4e19d15c73a4a5d4f0c7497001 (HEAD -> master) fourth commit
0df924a788a2a02b27299989e41741581250a3ee third commit
160fd49c9df4b75821269bf12572ff8f47a58539 my first git program

7 回滚前一个版本:git reset --hard head^

8 通过版本号回滚: git reset --hard f30e35dd53

    f30e35dd53:代表版本号前面一些数字,至少前六位

9 查看所有操作记录,回滚及提交记录:git relog

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ git reflog
f30e35d (HEAD -> master) HEAD@{0}: reset: moving to f30e35dd53
4890e1f HEAD@{1}: commit: commit 5
0df924a HEAD@{2}: reset: moving to HEAD^
f30e35d (HEAD -> master) HEAD@{3}: reset: moving to f30e35dd53
0df924a HEAD@{4}: reset: moving to HEAD^
f30e35d (HEAD -> master) HEAD@{5}: commit: fourth commit
0df924a HEAD@{6}: commit: third commit
160fd49 HEAD@{7}: commit (initial): my first git program

10. 创建分支 git checkout -b xxxx

查看分支 git branch

[root@localhost git_test]# git checkout -b dev
Switched to a new branch 'dev'
[root@localhost git_test]# git branch
* dev
  master

11 head指针切换到master :   git checkout master

12 head指针切换到分支xxx:   git checkout xxx

*******下面操作涉及到上传到git hub仓库,需要先注册git hub账号,具体看下边第二部分**************

13 上传到分支 git push origin xxx

[root@localhost git_test]# git push origin dev
Counting objects: 8, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 633 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/raylu123/git_test/pull/new/dev
remote: 
To git@github.com:raylu123/git_test.git
 * [new branch]      dev -> dev

14 下载主库到本地 git pull, 如果git pull提示 no tracking information ,表示本地分支和远程分支没有建立链接关系,

此时用命令创建: git branch --set-upstream branch-name orgin/branch-name

15 切换到本地主库,然后合并本地分支 git merge dev

当然如果有master和branch有冲突就先解决冲突再合并吧!

[root@localhost git_test]# git meger dev
git: 'meger' is not a git command. See 'git --help'.

Did you mean this?
	merge
[root@localhost git_test]# git merge dev
Merge made by the 'recursive' strategy.
 hello.py  | 1 +
 mydev1.py | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 mydev1.py

16 如果临时有BUG任务需要修复,而此时你的dev分支正在开发,还没有到提交的时候

可以用 git stash命令把你的分支暂时储藏起来,然后新建BUG修复分支,如git branch bug-101

然后修复你的内容,BUG修复完成后提交,再切回原来的开发分支dev

使用git stash list查看刚才暂存的信息,然后git stash apply即可恢复原来开发文档。

当然不要忘记使用git stash drop 或git stash pop(恢复的同时删除暂存区) 删除刚才的暂存区内容

二。git hub新建项目并使用:

1. 注册并登陆,start a project, 假如repository名字叫做git_test, 进入这么一个画面

2.

windows打开git bash窗口,linux直接输入命令行

把仓库下载到本地 git clone https://github.com/raylu123/git_test.git

mayn@DESKTOP-L7PRRDN MINGW64 /d
$ git clone https://github.com/raylu123/git_test.git
Cloning into 'git_test'...
warning: You appear to have cloned an empty repository.

3. 假如添加两个文件 readme和hello.py, 本地git add和git commit后

使用命令上传到github:  git push -u original master

中间需要输入github账号和密码

mayn@DESKTOP-L7PRRDN MINGW64 /d
$ cd git_test/

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ vim readme

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ vim hello.py

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ ls
hello.py  readme

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ git add .
warning: LF will be replaced by CRLF in hello.py.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in readme.
The file will have its original line endings in your working directory

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   hello.py
        new file:   readme


mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ git commit -m 'first commit'
[master (root-commit) d3bd778] first commit
 2 files changed, 2 insertions(+)
 create mode 100644 hello.py
 create mode 100644 readme

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ git push -u origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 298 bytes | 298.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To https://github.com/raylu123/git_test.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

4. 此时查看github网页就会看到已经更新成功:

三。将本地自建的git项目上传到github, 使用这两个命令即可,中间需要输入账号密码:

四。如果想更新github 而不想每次都输入账号密码呢?

需要在自己本地git生成一个密匙ssh key,公匙添加到你的git_hub上,就可以自由访问了

windows方法:

1.创建key:   ssh-keygen -t rsa -C 'xxx@mail.com'

后面让输入文件夹及passphrase 直接按回车表示默认

看到这句话:

Your identification has been saved in /c/Users/mayn/.ssh/id_rsa.
Your public key has been saved in /c/Users/mayn/.ssh/id_rsa.pub.

mayn@DESKTOP-L7PRRDN MINGW64 ~
$ ssh-keygen -t rsa -C 'luu888@vip.qq.com'
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/mayn/.ssh/id_rsa):
Created directory '/c/Users/mayn/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/mayn/.ssh/id_rsa.
Your public key has been saved in /c/Users/mayn/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:I9p/6XBHCdacwP+PYWzCaOiJz2BlC51An4BAuUmzJU0 luu888@vip.qq.com
The key's randomart image is:
+---[RSA 2048]----+
| .o=E.o  ..      |
|  = +. o ..+ .   |
| . B  . o o.+    |
|  +    o o ...   |
|      o S. ooo   |
|     o =.oo.o *  |
|    . +ooo...+ + |
|     ..+ooo.  . .|
|       .+o.      |
+----[SHA256]-----+

 

2. 查看公匙:

$ cat c:/Users/mayn/.ssh/id_rsa.pub
 

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGZT6YaXrGg26RlSiobYuVhC6Wfs11o1Hc/qV9zy89kOACOX5LttOjwLqvZPaSQvoMBo0OPl86lipdyELEhQoM9tZWXS8Z4bSpKgj0lWwGSahGp5xdopcJX5SEpJuOzrZhaBQ3cbVA8MygzZnuS3PJMCB0SYabKrqdDvpbMzVVPO9fiDIC0gyQsBAvVp2vsasgqjTBLs6SkljSDrtUCQcxXVn+B8Km8Dqq/Xr9w6VdoWvAE/rAqUJNc5/zGboWdcESSVAbp8VMvyVAxfAp/u7iyghRyLJ/QtSvY2/oDOI2L1HLybT56S5g6WQvWIy/2KlEfZ5V0UhmVLXmm6rqgYaX luu888@vip.qq.com

3. 进入git hub网站 settings,选择SSH and GPG keys,复制ssh key并添加

4.得到画面,表示OK了:

5 现在可以试试git push -u origin master,是不是不用密码啦

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ vim hello.py

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ ls
'@'   hello.py   readme

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ rm @

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

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:   hello.py

no changes added to commit (use "git add" and/or "git commit -a")

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ git add .
warning: LF will be replaced by CRLF in hello.py.
The file will have its original line endings in your working directory

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ git commit -m 'commit2'
[master 02e4a00] commit2
 1 file changed, 1 insertion(+)

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (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

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$ git push -u origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 304 bytes | 304.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/raylu123/git_test.git
   d3bd778..02e4a00  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

mayn@DESKTOP-L7PRRDN MINGW64 /d/git_test (master)
$

 

  • 2
    点赞
  • 9
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

raylu666

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值