Git入门操作(github上传与克隆)

参考

https://git-scm.com/book/zh/v2

yum install git -y

创建一个目录(Git仓库)

mkdir gitdemo

cd gitdemo

[[email protected] gitdemo]# git init

[[email protected] gitdemo]# echo file1 > file1
[[email protected] gitdemo]# git add file1
[[email protected] gitdemo]# git status -s
A  file1

 git status -s
 M README
MM Rakefile
A  lib/git.rb
M  lib/simplegit.rb
?? LICENSE.txt

Git入门操作(github上传与克隆)

[[email protected] gitdemo]# git config --global user.mail "[email protected]"
[[email protected] gitdemo]# git config --global user.name hzp

[[email protected] gitdemo]# git commit -m "add file1"
# On branch master
nothing to commit, working directory clean
[[email protected] gitdemo]# git status -s


因为之前提交过了 所以没有状态

修改状态

[[email protected] gitdemo]# git status -s
 M file1
[[email protected] gitdemo]# echo file1>> file1
[[email protected] gitdemo]# git status -s
 M file1
[[email protected] gitdemo]# git add file1
[[email protected] gitdemo]# git status -s
M  file1
[[email protected] gitdemo]# echo file1>> file1
[[email protected] gitdemo]# git status -s
MM file1


两个M说明提交后在缓冲区进行了修改

要再次提交

[[email protected] gitdemo]# git add file1
[[email protected] gitdemo]# git status -s
M  file1

[[email protected] gitdemo]# git commit -m "add file1"
[master aef1b8d] add file1
 Committer: hzp <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email [email protected]

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 3 insertions(+)

[[email protected] gitdemo]# l.
.  ..  .git

[[email protected] gitdemo]# vim .gitignore

.*
[[email protected] gitdemo]# git status -s
[[email protected] gitdemo]# l.
.  ..  .git  .gitignore

修改文件后还原文件

[[email protected] gitdemo]# git status
# On branch 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:   file1
#
no changes added to commit (use "git add" and/or "git commit -a")
[[email protected] gitdemo]# git checkout -- file1
[[email protected] gitdemo]# cat file1
file1
file1
file1
file1


查看日志

[[email protected] gitdemo]# git log
commit aef1b8d45c5d8d3692b6c4866480b17101c0ff4b
Author: hzp <[email protected]>
Date:   Sun Mar 31 10:48:01 2019 +0800

    add file1

commit 5991ab4dc29acb187c83533f580141be9c9140f6
Author: hzp <[email protected]>
Date:   Sun Mar 31 10:37:44 2019 +0800

    add file1

commit 02661c76cfdf312c3d52849b517b2434090a1f68
Author: root <[email protected]>
Date:   Sun Mar 31 10:32:02 2019 +0800

    add file1

 

git log --pretty=oneline

git reflog

[[email protected] gitdemo]# git reflog
aef1b8d [email protected]{0}: commit: add file1
5991ab4 [email protected]{1}: commit: add file1
02661c7 [email protected]{2}: commit (initial): add file1
[[email protected] gitdemo]# git log --pretty=oneline
aef1b8d45c5d8d3692b6c4866480b17101c0ff4b add file1
5991ab4dc29acb187c83533f580141be9c9140f6 add file1
02661c76cfdf312c3d52849b517b2434090a1f68 add file1

版本回滚

git reset --hard HEAD^

HEAD is now at 5991ab4 add file1
[[email protected] gitdemo]# git reflog^C
[[email protected] gitdemo]# cat file1
file1

 

也可指定

[[email protected] gitdemo]# git reflog
5991ab4 [email protected]{0}: reset: moving to HEAD^
aef1b8d [email protected]{1}: commit: add file1
5991ab4 [email protected]{2}: commit: add file1
02661c7 [email protected]{3}: commit (initial): add file1
[[email protected] gitdemo]# git reset --hard 5991ab4
HEAD is now at 5991ab4 add file1

误删

rm -fr file1

git checkout --file1

 

提交删除

git rm file1

git commit -m "del file1"

也可以返回

git reflog  找到删除之前的id

git reset --hard   86e1f9b

[[email protected] gitdemo]# git rm file1
rm 'file1'
[[email protected] gitdemo]# git commit -m "rm file1"
[master 2b74495] rm file1
 Committer: hzp <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email [email protected]

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 deletion(-)
 delete mode 100644 file1

[[email protected] gitdemo]# ls
file2

[[email protected] gitdemo]# git reflog
2b74495 [email protected]{0}: commit: rm file1
5991ab4 [email protected]{1}: reset: moving to HEAD^
aef1b8d [email protected]{2}: commit: add file1
5991ab4 [email protected]{3}: commit: add file1
02661c7 [email protected]{4}: commit (initial): add file1

[[email protected] gitdemo]# git reset --hard 5991ab4
HEAD is now at 5991ab4 add file1
[[email protected] gitdemo]# ls
file1  file2

上传自己的Git公共文件

 

 

Git入门操作(github上传与克隆)

[[email protected] ~]# cd /root/.ssh
[[email protected] .ssh]# cat
authorized_keys  id_rsa           id_rsa.pub       known_hosts      
[[email protected] .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCiB04UHHXhirqToLFm8PRu5bNqmRpPFS5XbE/IlmlpGNlbygGustR+AfKJJkOXowYkPPrDZjgEgw51DZg7BdbCVP19i4kkbmyt47UtmDZepLLkSUS6wxv+KZ01KatRX5uR2y9oqABENkDu3CF83gKKSG76Xa2dlxPMkUH7nuqAPILAWtjsrZ6t/8fCP9ub7LBTKiC2gkbzAYhPgNu3k+bzb30dcL6uESr9JLM08tKdL26E03k1rBL4pSUxGeUmTdmCX+ct7tyQOdKr9vTV4+t4Wg/gsxeScnb+aK9qvZpUvnN6kpxFqWG8yOs86EnGqVMhQhbxcTqFvJswLw3MiRKh [email protected]

上传公钥

 

https://github.com/settings/ssh/new

Git入门操作(github上传与克隆)

 

[[email protected] gitdemo]# echo "# demo" >> README.md
[[email protected] gitdemo]# git init
Reinitialized existing Git repository in /root/gitdemo/.git/
[[email protected] gitdemo]# git add README.md
[[email protected] gitdemo]# git commit -m "first commit"
[master b48ab30] first commit
 Committer: hzp <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email [email protected]

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 README.md
[[email protected] gitdemo]# git remote add origin [email protected]:heathcliffffffffs/demo.git
[[email protected] gitdemo]# git push -u origin master
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (9/9), 733 bytes | 0 bytes/s, done.
Total 9 (delta 0), reused 0 (delta 0)
To [email protected]:heathcliffffffffs/demo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

Git入门操作(github上传与克隆)

 

新建仓库

点上初始化的命令

Git入门操作(github上传与克隆)

[[email protected] gitdemo]# git remote -v
origin    [email protected]:heathcliffffffffs/demo.git (fetch)
origin    [email protected]:heathcliffffffffs/demo.git (push)

[[email protected] ~]# git clone [email protected]:heathcliffffffffs/test.git
Cloning into 'test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

[[email protected] ~]# cd test/
[[email protected] test]# ls
README.md

[[email protected] test]# cat README.md
# test
test

 

建立私有仓库

https://mirrors.tuna.tsinghua.edu.cn/

下载

gitlab-ce

yum install gitlab-ce-11.2.0-ce.0.el7.x86_64.rpm  -y

Git入门操作(github上传与克隆)

 

[[email protected] gitlab]# vim gitlab.rb
[[email protected] gitlab]# pwd
/etc/gitlab

external_url 'http://172.25.11.250'

重载服务

gitlab-ctl reconfigure

gitlab-ctl start

[[email protected] gitlab]# gitlab-ctl start
ok: run: alertmanager: (pid 14228) 35s
ok: run: gitaly: (pid 14154) 36s
ok: run: gitlab-monitor: (pid 14176) 36s
ok: run: gitlab-workhorse: (pid 14137) 37s
ok: run: logrotate: (pid 13395) 95s
ok: run: nginx: (pid 14453) 0s
ok: run: node-exporter: (pid 13681) 83s
ok: run: postgres-exporter: (pid 14261) 35s
ok: run: postgresql: (pid 12842) 160s
ok: run: prometheus: (pid 14207) 35s
ok: run: redis: (pid 12713) 166s
ok: run: redis-exporter: (pid 13817) 71s
ok: run: sidekiq: (pid 13204) 113s
ok: run: unicorn: (pid 13137) 119s
[[email protected] gitlab]# gitlab-ctl status
run: alertmanager: (pid 14228) 40s; run: log: (pid 14253) 40s
run: gitaly: (pid 14154) 41s; run: log: (pid 14246) 40s
run: gitlab-monitor: (pid 14176) 41s; run: log: (pid 14195) 41s
run: gitlab-workhorse: (pid 14137) 42s; run: log: (pid 14202) 40s
run: logrotate: (pid 13395) 100s; run: log: (pid 14203) 40s
run: nginx: (pid 14487) 0s; run: log: (pid 14204) 40s
run: node-exporter: (pid 13681) 88s; run: log: (pid 14247) 40s
run: postgres-exporter: (pid 14261) 40s; run: log: (pid 14347) 38s
run: postgresql: (pid 12842) 165s; run: log: (pid 14179) 41s
run: prometheus: (pid 14207) 40s; run: log: (pid 14221) 40s
run: redis: (pid 12713) 171s; run: log: (pid 14178) 41s
run: redis-exporter: (pid 13817) 76s; run: log: (pid 14194) 41s
run: sidekiq: (pid 13204) 118s; run: log: (pid 14181) 41s
run: unicorn: (pid 13137) 124s; run: log: (pid 14180) 41s

 

访问

http://172.25.11.250/users/sign_in

[[email protected] gitlab]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCiB04UHHXhirqToLFm8PRu5bNqmRpPFS5XbE/IlmlpGNlbygGustR+AfKJJkOXowYkPPrDZjgEgw51DZg7BdbCVP19i4kkbmyt47UtmDZepLLkSUS6wxv+KZ01KatRX5uR2y9oqABENkDu3CF83gKKSG76Xa2dlxPMkUH7nuqAPILAWtjsrZ6t/8fCP9ub7LBTKiC2gkbzAYhPgNu3k+bzb30dcL6uESr9JLM08tKdL26E03k1rBL4pSUxGeUmTdmCX+ct7tyQOdKr9vTV4+t4Wg/gsxeScnb+aK9qvZpUvnN6kpxFqWG8yOs86EnGqVMhQhbxcTqFvJswLw3MiRKh [email protected]

 

 

Git入门操作(github上传与克隆)

搭建私有仓库

 

[[email protected] gitlab]# git clone [email protected]:westos/test123.git
Cloning into 'test123'...
The authenticity of host '172.25.11.250 (172.25.11.250)' can't be established.
ECDSA key fingerprint is 7c:15:30:2d:0b:2b:e5:ec:b8:fc:89:ca:33:b3:c9:9f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.11.250' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

 

 1169  vim index.html
 1170  git add index.html
 1171  git commit -m "add index"
 1172  git push -u origin master