git使用配置说明

在单机上使用git

安装git服务,可以使用yum直接进行安装

 yum install -y git
[root@nfs2 data]# yum install -y git
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00     
elrepo | 2.9 kB 00:00:00     
epel/x86_64/metalink | 8.4 kB 00:00:00     
extras | 3.4 kB 00:00:00     
mongodb-org-4.0 | 2.5 kB 00:00:00     
updates  
---------------省略
Dependency Updated:
  perl-Git.noarch 0:1.8.3.1-14.el7_5                                                                                                                                           
​
Complete!

安装完成后,再来初始化git服务及创建项目,创建一个项目测试文件

[root@nfs2 /]# mkdir /data/github
[root@nfs2 /]# cd /data/github/
[root@nfs2 github]# git init
Initialized empty Git repository in /data/github/.git/
[root@nfs2 github]# echo -e "123456list" > 1.txt

向版本管理中添加个项目
并将这个版本上传至管理中心
git add 版本文件
git commit -m "add 版本文件"
初次执行代码上传会提示添加个联系人邮件地址,按照提示的运行  git config --global user.email 及以下的命令,指定邮件和联系人名字即可。如:

[root@nfs2 github]# git add 1.txt 
[root@nfs2 github]# git commit -m "add 1.txt"
​
*** Please tell me who you are.
​
Run
​
  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"
​
to set your account's default identity.
Omit --global to set the identity only in this repository.
​
fatal: unable to auto-detect email address (got 'root@nfs2.(none)')
[root@nfs2 github]# git config --global user.email "[email protected]"
[root@nfs2 github]# git config --global user.name "list"

向版本管理中提交代码版本
这次就顺利的提交了一个版本的文件。过程如下:

[root@nfs2 github]# git add 1.txt 
[root@nfs2 github]# git commit -m "add 1.txt"
[master (root-commit) e23962e] add 1.txt
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt

查看git版本更新信息
多做几次文件修改并提交版本更新,然后使用git log查看版本更新记录信息
git log查看信息要素较多,可以添加选项--pretty=oneline,让其只显示一行。
git log --pretty=oneline 
这里测试过程如下:

[root@nfs2 github]# echo "123" >> 1.txt
[root@nfs2 github]# git add 1.txt 
[root@nfs2 github]# git commit -m "add 1.txt"
[master 1812cdc] add 1.txt
 1 file changed, 1 insertion(+)
[root@nfs2 github]# git log               ---------------git log显示的内容
commit 1812cdc1c348887f1b97342e355ae9a02a58902e
Author: list <[email protected]>
Date: Fri Nov 23 14:41:29 2018 +0800
​
    add 1.txt
​
commit e23962e2d73df6d271b7def7a850424d4a1ef795
Author: list <[email protected]>
Date: Fri Nov 23 14:30:49 2018 +0800
​
    add 1.txt
[root@nfs2 github]# git log --pretty=oneline        -------------git log --pretty=oneline显示的内容
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt

git更新的历史版本管理
退回到某个版本
git reset --hard  版本ID
退回到某个版本后,如果再使用git log这个执行命令查看的话会发现最新版本不再显示在记录当中。这时候就需要使用git reflog查看所有的历史版本记录

[root@nfs2 github]# git log --pretty=oneline
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt
[root@nfs2 github]# git reset --hard e23962e2d
HEAD is now at e23962e add 1.txt
[root@nfs2 github]# git log --pretty=oneline
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt

再次将新版本提交,并使用git reflog查看所有的版本操作记录

[root@nfs2 github]# git reset --hard 1812cdc
HEAD is now at 1812cdc add 1.txt
[root@nfs2 github]# git reflog
1812cdc HEAD@{0}: reset: moving to 1812cdc
e23962e HEAD@{1}: reset: moving to e23962e2d
1812cdc HEAD@{2}: commit: add 1.txt
e23962e HEAD@{3}: commit (initial): add 1.txt

将版本中修改过的文件,这些文件只进行了add没有执行commit操作,让其退回到上一次提交的状态,使用git reset HEAD files,然后再执行
git checkout -- files  的操作
修改下1.txt文件,向文件内追加一行内容,然后执行git add做准备提交更新的操作。注意:不要执行git commit提交上去!恢复后文件中新追加的一行内容不存在了。测试过程如下:

[root@nfs2 github]# git reset --hard 1812cdc
HEAD is now at 1812cdc add 1.txt
[root@nfs2 github]# git reflog
1812cdc HEAD@{0}: reset: moving to 1812cdc
e23962e HEAD@{1}: reset: moving to e23962e2d
1812cdc HEAD@{2}: commit: add 1.txt
e23962e HEAD@{3}: commit (initial): add 1.txt

撤销更改
如果操作过程中不小心删除了版本中的某个文件,撤销删除使用

git chckout -- files 
测试过程如下:
[root@nfs2 github]# rm -rf 1.txt 
[root@nfs2 github]# git checkout -- 1.txt
[root@nfs2 github]# ls
1.txt
[root@nfs2 github]# cat 1.txt 
123456list
123

删除和恢复git某个版本
git commit -m "delete 文件"
创建个测试文件2.txt,文件内容为1234
首先删除掉2.txt本地文件,再删除掉git中的版本文件

[root@nfs2 github]# git rm 2.txt 
rm '2.txt'
[root@nfs2 github]# ls             ----------查看生效的文件
1.txt
[root@nfs2 github]# git log --pretty=oneline             ------在对git删除前先查看git中的记录
7a659af5d27ffb25baba2f5d5199a462db503a0d add 2.txt
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt
[root@nfs2 github]# git commit -m "delete 2.txt"       --------删除git中的2.txt版本记录
[master 5efa36d] delete 2.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 2.txt
[root@nfs2 github]# git log --pretty=oneline           --------查看这个版本记录的状态
5efa36d5e86aa9821e9c6772828cc9a9ca31e250 delete 2.txt
7a659af5d27ffb25baba2f5d5199a462db503a0d add 2.txt
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt

恢复git内删除的版本文件
git reset --hard  版本ID
首先查看删除2.txt后的现在git状态,恢复后再次查看git的状态信息。过程操作如下:

[root@nfs2 github]# git log --pretty=oneline 
5efa36d5e86aa9821e9c6772828cc9a9ca31e250 delete 2.txt
7a659af5d27ffb25baba2f5d5199a462db503a0d add 2.txt
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt
[root@nfs2 github]# git reset --hard 7a659af       ---------hard 恢复删除掉的版本的ID,将其删除操作撤销掉
HEAD is now at 7a659af add 2.txt
[root@nfs2 github]# git log --pretty=oneline       ---------查看恢复后的git版本状态信息
7a659af5d27ffb25baba2f5d5199a462db503a0d add 2.txt
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt

建立远程仓库

使用github仓库来管理代码版本
首先在github中创建一个新的项目
git使用配置说明
创建好项目后还需要给github访问服务器的密钥配置
以下是在github中添加服务器密钥的过程。过程如下:
git使用配置说明
点击头像的Settings,进入后再寻找到SSH and GPG keys的点击入口
git使用配置说明
这步先要在服务器上生成密钥对,然后把公钥内容保存到github中

[root@nfs1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
03:c5:c8:44:b8:60:b5:c2:fc:d2:3a:cb:2d:bb:a3:70 root@nfs1
The key's randomart image is:
+--[ RSA 2048]----+
|   ..=oo.        |
| oo ..o..        |
| .+....          |
|   +.  .         |
|  . o   S        |
|   o     .       |
|. E              |
|.oo+             |
|..==.            |
+-----------------+
[root@nfs1 ~]# 
[root@nfs1 ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWskJGS7f+p/KM8J3gelehgDTR19Pj6L6umZSGusJcyLzFwD3n92sbh5q4NwKnB/TYiGonZPY783CKl738A7h8CVy48HHtTyo4J9VQOFwxwD+aLC7qtHqHLDf2ypVLMCiP1kH8d0A9CN2lcG1wSTjIr4ZX5wEy8QY3uUtM9A7K4smJ/ZgZDM58c56z+bqn7j6lD5xDnCxrj/FtIuEcU2BrDju03WVAVwQXbKgzWfcQ8GzZgJeWQ0lKDTL3Eo7kbOOPFaxN3TnIdehDZnlmskJMlAnhavWhUG6Anx3a93homi0dSJEl3HRWClzZ3y+yseZ6Domb+vgEFOiTLUqU8KQ5 root@nfs1

然后在github中保存公钥的内容
git使用配置说明

到此步骤设置github完成
git使用配置说明
在服务器端/客户端使用版本上传等操作
在服务/用户端初始化github应用,在github上选择ssh模式的git方式
git使用配置说明

分步骤执行一,在项目目录下创建一个初始文件并添加到版本管理中
git使用配置说明

echo "# monice" >> README.md 
git init 
git add README.md 
git commit -m "first commit"
github的初始化过程操作如下:
[root@nfs1 monice]# echo "# monice" >> README.md 
[root@nfs1 monice]# git init 
Initialized empty Git repository in /usr/local/src/monice/.git/
[root@nfs1 monice]# git add README.md 
[root@nfs1 monice]# git commit -m "new ceshi"
[master (root-commit) bb896e0] new ceshi
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

分步执行二,指定github上的项目,github项目名称必须要和当前所在的项目目录名称相同,对应github中的项目名来修改用户端的项目名称
初次github上传会提示输入用户名和密码。这里的用户名和密码是github的用户名及密码
如果git使用的是SSH模式,则在git push时则不需要验证密码。
操作过程如下:
git使用配置说明

[root@nfs1 monice]# git remote add origin [email protected]:hanxiang233/monice.git
[root@nfs1 monice]# git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 217 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/hanxiang233/monice/pull/new/master
remote: 
To [email protected]:hanxiang233/monice.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

再次向github中更新几个脚本文件
再次上传代码到github中则不再需要验证github用户名和密码了,git更新过程如下
首先add项目文件到本地版本管理中

[root@nfs1 monice]# git add 1.sh fo
for.sh   fors.sh  foss.sh  
[root@nfs1 monice]# git add 1.sh for*
[root@nfs1 monice]# git commit -m "new shell"
[master 2390d8e] new shell
 3 files changed, 32 insertions(+)
 create mode 100644 1.sh
 create mode 100644 for.sh
 create mode 100644 fors.sh
[root@nfs1 monice]# git push
Counting objects: 6, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 641 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To [email protected]:hanxiang233/monice.git
   5ac93e9..2390d8e  master -> master

出现的问题
<br/>我在执行git add *.sh和git commit -m "new ceshi"<br/>执行git commit -m提示出(表示已经提交了一次,之前提交的版本内容没有git push):<br/>

[root@nfs1 monice]# git push
# 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 directory clean
然而就在我执行git push的时候又有信息提示,这次是警告信息:
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
​
  git config --global push.default matching
​
To squelch this message and adopt the new behavior now, use:
​
  git config --global push.default simple
​
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

安照提示我执行了git config --global push.default simple这句指令,然后重新git push。重新push会提示重新输入github的密码进行验证

[root@nfs1 monice]# git push
Username for 'https://github.com': hanxiang233
Password for 'https://[email protected]': 
Counting objects: 12, done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 1.78 KiB | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To https://github.com/hanxiang233/monice.git
   bb896e0..e7da5d5 master -> master

这次也成功的github中更新的版本内容:
git使用配置说明

再次完整测试
在项目目录下再创建一个2.txt文件进行上传到github中测试
这里还得注意的是,上传如果使用的是https方式,所以在每次push版本时都需要验证github的密码。
如果使用SSH初始化git时,再次执行git上传代码则不需要验证github的用户名和密码

[root@nfs1 monice]# echo "1122334455" > 2.txt
[root@nfs1 monice]# git add 2.txt 
[root@nfs1 monice]# git commit -m "add 2.txt"
[master 9b6d416] add 2.txt
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt
[root@nfs1 monice]# git push
Username for 'https://github.com': hanxiang233
Password for 'https://[email protected]': 
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/hanxiang233/monice.git
   e7da5d5..9b6d416  master -> master
   
   ----------------------------push 3.txt版本更新文件
[root@nfs1 monice]# git push
Username for 'https://github.com': hanxiang233
Password for 'https://[email protected]': 
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/hanxiang233/monice.git
   9b6d416..bc1cadd  master -> master

在github上查看push的文件,可以看到文件已经被上传至github上
git使用配置说明

克隆远程仓库

在github上随意找到一个其他人的开源项目,复制ssh的git地址,然后在自己本地服务器上git克隆下来整个项目,具体操作过程如下:
随意在本地创建一个目录,然后git  clone下载一个项目

[root@nfs1 src]# mkdir ceshi
[root@nfs1 src]# cd ceshi/
[root@nfs1 ceshi]# ls

git clone开源项目,将项目下载至本地,然后查看当前下载的项目文件内容

[root@nfs1 ceshi]# git clone [email protected]:DIYgod/diygod.me.git
Cloning into 'diygod.me'...
remote: Enumerating objects: 223, done.
remote: Counting objects: 100% (223/223), done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 1658 (delta 98), reused 217 (delta 98), pack-reused 1435
Receiving objects: 100% (1658/1658), 19.48 MiB | 36.00 KiB/s, done.
Resolving deltas: 100% (745/745), done. 
```   
github下来的开源项目文件

[root@nfs1 ceshi]# ls
diygod.me
[root@nfs1 ceshi]# cd diygod.me/
[root@nfs1 diygod.me]# ls
_config.yml package.json README.md scaffolds source themes yarn.lock

猜你喜欢

转载自blog.51cto.com/8844414/2321690