首页 > 技术文章 > git 使用记录

JanSN 2019-10-08 17:16 原文


参考:
https://www.cnblogs.com/simple1368/p/9084077.html

git 服务器新建仓库

git init --bare 仓库名.git

[git@localhost git]$ git init --bare hugin.git
Initialized empty Git repository in /home/git/git/hugin.git/
[git@localhost git]$ ls
hugin.git
[git@localhost git]$ pwd
/home/git/git

本地同步 git 服务器仓库

Z17023031@ZHS-W54001050 MINGW64 /e/code/SVN/Hugin/L11RackMonitor/trunk/sourcecode (master)
$ pwd
/e/code/SVN/Hugin/L11RackMonitor/trunk/sourcecode

1、 将本地目录变为 git 本地仓库

git init

$ git init
Initialized empty Git repository in E:/code/SVN/Hugin/L11RackMonitor/trunk/sourcecode/.git/

$ ls
hugin/
  • 设置忽略文件 .gitignore
$ touch .gitignore

$ ls
hugin/

2、 查看仓库文件状态

Z17023031@ZHS-W54001050 MINGW64 /e/code/SVN/Hugin/L11RackMonitor/trunk/sourcecode (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        hugin/

nothing added to commit but untracked files present (use "git add" to track)

3、 追踪除了忽略文件以外所有文件

Z17023031@ZHS-W54001050 MINGW64 /e/code/SVN/Hugin/L11RackMonitor/trunk/sourcecode (master)
$ git add .

4、 将文件提交到本地仓库 (本地仓库可多次提交

git commit -m '提交信息'

git commit -m 'first commit'

5、 关联远程 git 仓库

git remote add origin 远程库地址(用户@IP:git仓库目录路径)

$ git remote add origin git@10.41.95.207:/home/git/git/hugin.git

6、 将本地 git 仓库改动全部同步到远程 git 仓库

第一次p ush 的时候,加上 -u 参数,
Git 就会把本地的 master 分支和远程的 master 分支进行关联起来, 我们以后的 push 操作就不再需要加上 -u 参数了

$ git push -u origin master
git@10.41.95.207's password:
Enumerating objects: 764, done.
Counting objects: 100% (764/764), done.
Delta compression using up to 8 threads
Compressing objects: 100% (735/735), done.
Writing objects: 100% (764/764), 2.71 MiB | 5.61 MiB/s, done.
Total 764 (delta 80), reused 0 (delta 0)
To 10.41.95.207:/home/git/git/hugin.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

注意:若远程 git 仓库为空,可直接做这一步

若远程 git 仓库不为空

7、远程库与本地同步合并

git pull --rebase origin master

8、克隆远程仓库内容

同步了远程之后想看看最新的内容有没有在上面,于是我在 git 远程仓库所在的服务器上的另一个目录进行克隆

[hugin@localhost test1]$ git clone git@127.0.0.1:/home/git/git/hugin.git
Cloning into 'hugin'...
git@127.0.0.1's password:
remote: Counting objects: 764, done.
remote: Compressing objects: 100% (655/655), done.
remote: Total 764 (delta 80), reused 764 (delta 80)
Receiving objects: 100% (764/764), 2.71 MiB | 0 bytes/s, done.
Resolving deltas: 100% (80/80), done.
[hugin@localhost test1]$ ls
hugin

看了一下目录的内容,有成功同步

可以看到远程克隆的时候要输 git 用户密码,用来专门控制克隆权限。
所以一开始就要新建 git 用户专门用于控制远程 code 文件

提交本地修改

1、本地提交

git push -u origin master

2、异地更新

git pull

推荐阅读