查看所有文章链接:(更新中)GIT常用场景- 目录
文章目录
- 1. 环境说明
- 2. 安装配置
- 2.1 配置user信息
- 2.2 config的三个作用域
- 3. 建git仓库
- 3.1 把已有的项目代码纳入git管理
- 3.2 新建的项目直接用git管理
- 3.3 配置local的user和email
- 3.4 优先级:local > global > system
- 4. 远程仓库配置
- 5. 往仓库里添加文件并同步到远程仓库
1. 环境说明
使用git + gitee
2. 安装配置
ubuntu@VM-8-3-ubuntu:~/wuxiang/git$ git --version
git version 2.25.1
ubuntu@VM-8-3-ubuntu:~/wuxiang/git$
2.1 配置user信息
ubuntu@VM-8-3-ubuntu:~/wuxiang/git$ git config --global user.name 'wuxxxxx'
ubuntu@VM-8-3-ubuntu:~/wuxiang/git$ git config --global user.email 156xxxx5802@163.com
2.2 config的三个作用域
local只对仓库有效
global对登录用户的所有仓库有效
system对系统的所有⽤用户有效
git config --local
git config --global
git config --system
- 显示config的作用域,加list
git config --list --local
git config --list --global
git config --list --system
ubuntu@VM-8-3-ubuntu:~/wuxiang/git$ git config --list
user.name=wuxxxxx
user.email=156xxxx5802@163.com
3. 建git仓库
3.1 把已有的项目代码纳入git管理
cd 项⽬目代码所在的⽂文件夹
git init
ubuntu@VM-8-3-ubuntu:~/wuxiang/git$ ll
total 8
drwxrwxr-x 2 ubuntu ubuntu 4096 Aug 10 20:13 ./
drwxrwxr-x 5 ubuntu ubuntu 4096 Aug 10 20:13 ../
ubuntu@VM-8-3-ubuntu:~/wuxiang/git$ git init
Initialized empty Git repository in /home/ubuntu/wuxiang/git/.git/
ubuntu@VM-8-3-ubuntu:~/wuxiang/git$ ll
total 12
drwxrwxr-x 3 ubuntu ubuntu 4096 Aug 10 20:22 ./
drwxrwxr-x 5 ubuntu ubuntu 4096 Aug 10 20:13 ../
drwxrwxr-x 7 ubuntu ubuntu 4096 Aug 10 20:22 .git/
ubuntu@VM-8-3-ubuntu:~/wuxiang/git$
3.2 新建的项目直接用git管理
cd 某个文件夹
git init your_project #会在当前路路径下创建和项目名称同名的文件夹
cd your_project
ubuntu@VM-8-3-ubuntu:~/wuxiang/git2$ ll
total 8
drwxrwxr-x 2 ubuntu ubuntu 4096 Aug 10 20:24 ./
drwxrwxr-x 6 ubuntu ubuntu 4096 Aug 10 20:24 ../
ubuntu@VM-8-3-ubuntu:~/wuxiang/git2$ git init empty_git_project
Initialized empty Git repository in /home/ubuntu/wuxiang/git2/empty_git_project/.git/
ubuntu@VM-8-3-ubuntu:~/wuxiang/git2$ ll empty_git_project/
total 12
drwxrwxr-x 3 ubuntu ubuntu 4096 Aug 10 20:24 ./
drwxrwxr-x 3 ubuntu ubuntu 4096 Aug 10 20:24 ../
drwxrwxr-x 7 ubuntu ubuntu 4096 Aug 10 20:24 .git/
ubuntu@VM-8-3-ubuntu:~/wuxiang/git2$
3.3 配置local的user和email
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ ll
total 12
drwxrwxr-x 3 ubuntu ubuntu 4096 Aug 10 20:22 ./
drwxrwxr-x 5 ubuntu ubuntu 4096 Aug 10 20:26 ../
drwxrwxr-x 7 ubuntu ubuntu 4096 Aug 10 20:28 .git/
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git config --local user.name 'uuuuu'
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git config --local user.email '156xxxx5802@163.com'
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git config --local --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=uuuuu
user.email=156xxxx5802@163.com
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git config --list
user.name=wuxiang
user.email=156xxxx5802@163.com
pack.windowmemory=32m
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=uuuuu
user.email=156xxxx5802@163.com
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$
3.4 优先级:local > global > system
4. 远程仓库配置
ubuntu@VM-8-3-ubuntu:~$ ssh-keygen -t rsa -C "156xxxx5802@163.com"
(跳出的选项一直回车确认就好)
ubuntu@VM-8-3-ubuntu:~$ cd .ssh/
ubuntu@VM-8-3-ubuntu:~/.ssh$ ls
authorized_keys id_rsa id_rsa.pub known_hosts
ubuntu@VM-8-3-ubuntu:~/.ssh$
拷贝id_rsa.pub文件中的所有内容,复制到如下位置(可以通过主页右上角 「个人设置」->「安全设置」->「SSH 公钥」-> … ,添加生成的 public key(id_rsa.pub) 到当前账户中。)
使用ssh -T 测试,通过的话输出如下内容:
ubuntu@VM-8-3-ubuntu:~/.ssh$ ssh -T git@gitee.com
Hi wuxiang(@wuxiang16)! You've successfully authenticated, but GITEE.COM does not provide shell access.
ubuntu@VM-8-3-ubuntu:~/.ssh$
关联远程仓库:
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git remote add origin git@gitee.com:wuxiang16/git_learn.git
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git remote -v
origin git@gitee.com:wuxiang16/git_learn.git (fetch)
origin git@gitee.com:wuxiang16/git_learn.git (push)
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$
由于远程仓库在创建时包含了LICENSE文件,本地仓库包含了Readme文件,两个仓库内容不同步,可以通过如下命令先同步:
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git pull origin master --allow-unrelated-histories
From gitee.com:wuxiang16/git_learn* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy..gitignore | 18 ++++LICENSE | 674 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++2 files changed, 692 insertions(+)create mode 100644 .gitignorecreate mode 100644 LICENSE
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ ll
total 52
drwxrwxr-x 3 ubuntu ubuntu 4096 Aug 11 11:03 ./
drwxrwxr-x 5 ubuntu ubuntu 4096 Aug 10 20:26 ../
drwxrwxr-x 8 ubuntu ubuntu 4096 Aug 11 11:03 .git/
-rw-rw-r-- 1 ubuntu ubuntu 350 Aug 11 11:03 .gitignore
-rw-rw-r-- 1 ubuntu ubuntu 35147 Aug 11 11:03 LICENSE
-rw-rw-r-- 1 ubuntu ubuntu 0 Aug 10 20:30 Readme
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git status
On branch master
nothing to commit, working tree clean
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$
5. 往仓库里添加文件并同步到远程仓库
创建几个测试文件,并同步到远程仓库:
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ ll
total 52
drwxrwxr-x 3 ubuntu ubuntu 4096 Aug 11 11:03 ./
drwxrwxr-x 5 ubuntu ubuntu 4096 Aug 10 20:26 ../
drwxrwxr-x 8 ubuntu ubuntu 4096 Aug 11 11:03 .git/
-rw-rw-r-- 1 ubuntu ubuntu 350 Aug 11 11:03 .gitignore
-rw-rw-r-- 1 ubuntu ubuntu 35147 Aug 11 11:03 LICENSE
-rw-rw-r-- 1 ubuntu ubuntu 0 Aug 10 20:30 Readme
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ mkdir guojing
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ touch guojing/xianglongshibazhang.c
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ mkdir yangguo
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ touch yangguo/anranxiaohunzhang.c
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git add .
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)new file: guojing/xianglongshibazhang.cnew file: yangguo/anranxiaohunzhang.cubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git commit -m'add guojing and yangguo'
[master 103b057] add guojing and yangguo2 files changed, 0 insertions(+), 0 deletions(-)create mode 100644 guojing/xianglongshibazhang.ccreate mode 100644 yangguo/anranxiaohunzhang.c
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ git push origin master
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 2 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (9/9), 873 bytes | 873.00 KiB/s, done.
Total 9 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:wuxiang16/git_learn.git879782f..103b057 master -> master
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$
本地仓库如下:
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$ tree
.
├── guojing
│ └── xianglongshibazhang.c
├── LICENSE
├── Readme
└── yangguo└── anranxiaohunzhang.c2 directories, 4 files
ubuntu@VM-8-3-ubuntu:~/wuxiang/git_learn$
远程仓库如下: