Git初始化项目 和 Gitignore

第一步:生成ssh秘钥对  (不用每次输入用户名密码)
ssh-keygen -t rsa -C "893861319@qq.com"
一路回车:
CsyHost:.ssh liuge36$ ll
total 16
drwx------   4 liuge36  staff   128 11 12 09:47 ./
drwxr-xr-x+ 47 liuge36  staff  1504 11 12 09:47 ../
-rw-------   1 liuge36  staff  1823 11 12 09:47 id_rsa
-rw-r--r--   1 liuge36  staff   398 11 12 09:47 id_rsa.pub

CsyHost:.ssh liuge36$ mv id_rsa id_rsa_person_github
CsyHost:.ssh liuge36$ mv id_rsa.pub id_rsa_person_github.pub
CsyHost:.ssh liuge36$ cd ..

再次生成一对
ssh-keygen -t rsa -C "893861319@qq.com"
一路回车:
CsyHost:~ liuge36$ cd .ssh/
CsyHost:.ssh liuge36$ ll
total 32
drwx------   7 liuge36  staff   224 11 12 09:51 ./
drwxr-xr-x+ 47 liuge36  staff  1504 11 12 09:47 ../
drwxr-xr-x   2 liuge36  staff    64 11 12 09:48 config/
-rw-------   1 liuge36  staff  1823 11 12 09:51 id_rsa
-rw-r--r--   1 liuge36  staff   398 11 12 09:51 id_rsa.pub
-rw-------   1 liuge36  staff  1823 11 12 09:47 id_rsa_person_github
-rw-r--r--   1 liuge36  staff   398 11 12 09:47 id_rsa_person_github.pub
CsyHost:.ssh liuge36$ mv id_rsa id_rsa_person_gitee
CsyHost:.ssh liuge36$ mv id_rsa.pub id_rsa_person_gitee.pub
CsyHost:.ssh liuge36$ ll
total 32
drwx------   7 liuge36  staff   224 11 12 09:51 ./
drwxr-xr-x+ 47 liuge36  staff  1504 11 12 09:47 ../
drwxr-xr-x   2 liuge36  staff    64 11 12 09:48 config/
-rw-------   1 liuge36  staff  1823 11 12 09:51 id_rsa_person_gitee
-rw-r--r--   1 liuge36  staff   398 11 12 09:51 id_rsa_person_gitee.pub
-rw-------   1 liuge36  staff  1823 11 12 09:47 id_rsa_person_github
-rw-r--r--   1 liuge36  staff   398 11 12 09:47 id_rsa_person_github.pub

把这里的id_rsa*.pub 粘贴到你相应的git账号下。

因为,这个时候,有两个sshkey,需要新建一个config文件,配置多个sshkey:
vim config:
# 个人GitHub
Host github.com
HostName github.com
User liuge36
IdentityFile /Users/liuge36/.ssh/id_rsa_person_github

# 码云gitee
Host gitee.com
HostName gitee.com
User liuge36
IdentityFile /Users/liuge36/.ssh/id_rsa_person_gitee

这样,就可以了,
	假如,这个时候,你还需要配置别的git网站的地址,
	你再生成,重命名,添加config信息即可


拓展:windows的配置
	Host github.com
	HostName github.com
	User liuge36
	IdentityFile C:\\Users\\csy\\.ssh\\id_rsa

---------------------------
Run

CsyHost:.ssh liuge36$ git config --global user.email "893861319@qq.com"
CsyHost:.ssh liuge36$ git config --global user.name "liuge36"
---------------------------


第二步:Github的使用-仓库创建(init)、克隆(clone)、拉取(pull)和推送(push)

在github新建一个仓库

2.1仓库创建(init)
CsyHost:Desktop liuge36$ cd liugebigdata/
CsyHost:liugebigdata liuge36$ ls
CsyHost:liugebigdata liuge36$ git init
已初始化空的 Git 仓库于 /Users/liuge36/Desktop/liugebigdata/.git/
CsyHost:liugebigdata liuge36$ vim test.txt

CsyHost:liugebigdata liuge36$ git status
位于分支 master

尚无提交

未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	test.txt

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

CsyHost:liugebigdata liuge36$ git add test.txt
CsyHost:liugebigdata liuge36$ git status
位于分支 master

尚无提交

要提交的变更:
  (使用 "git rm --cached <文件>..." 以取消暂存)

	新文件:   test.txt


CsyHost:liugebigdata liuge36$ git commit -am "add:第一次提交init"
[master(根提交) 1dd6ebe] add:第一次提交init
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
CsyHost:liugebigdata liuge36$ git status
位于分支 master
无文件要提交,干净的工作区
CsyHost:liugebigdata liuge36$



工作区 缓存区
工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的缓存区,
还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD

小结:
git init
git status

git add test.txt 这一步叫做建立跟踪
git rm test.txt 以取消暂存

git commit -am "add" 这一步叫做提交


2.2推送(push)

在github新建一个仓库名称叫做:liugebigdata

CsyHost:liugebigdata liuge36$ git remote add origin git@github.com:liuge36/liugebigdata.git
CsyHost:liugebigdata liuge36$ git push -u origin master
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
枚举对象: 3, 完成.
对象计数中: 100% (3/3), 完成.
写入对象中: 100% (3/3), 232 bytes | 232.00 KiB/s, 完成.
总共 3 (差异 0),复用 0 (差异 0)
To github.com:liuge36/liugebigdata.git
 * [new branch]      master -> master
分支 'master' 设置为跟踪来自 'origin' 的远程分支 'master'。
CsyHost:liugebigdata liuge36$

这里初始化的方式有两种:
    1. 远程新建一个仓库liugebigdata
       在本地初始化一个仓库
        cd liugebigdata
        git init
       将本地仓库与远程仓库进行绑定:
        git remote add origin git@github.com:liuge36/liugebigdata.git
        git add .
        git commit -m "Initial commit"
        git push -u origin master

    2. 远程新建一个仓库liugebigdata
        git clone git@github.com:liuge36/liugebigdata.git
        cd liugebigdata
        touch README.md
        git add README.md
        git commit -m "add README"
        git push -u origin master


2.3拉取(pull)
别人做了代码的更改
你想拉取主分支上的代码
CsyHost:liuge1 liuge36$ git pull
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
展开对象中: 100% (3/3), 完成.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
来自 github.com:liuge36/liuge1
   28148a4..06f9c5e  master     -> origin/master
更新 28148a4..06f9c5e
Fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)
CsyHost:liuge1 liuge36$ ll
total 8
drwxr-xr-x   4 liuge36  staff   128 11 12 10:50 ./
drwx------@ 36 liuge36  staff  1152 11 12 10:50 ../
drwxr-xr-x  14 liuge36  staff   448 11 12 10:50 .git/
-rw-r--r--   1 liuge36  staff    33 11 12 10:50 test.txt
CsyHost:liuge1 liuge36$ cat test.txt
1111111
别人做了代码更改
CsyHost:liuge1 liuge36$

小结:
使用git pull 就会把远端仓库的最新代码替换掉你本地的代码


第三步: Github的使用-项目分支
# 查看本地分支
CsyHost:liuge1 liuge36$ git branch
* master
# 查看所有分支
CsyHost:liuge1 liuge36$ git branch -a
* master
  remotes/origin/master
CsyHost:liuge1 liuge36$

# 在本地创建新的分支dev,创建之后,你就会自动切到该分支上去

CsyHost:liuge1 liuge36$ git checkout -b dev
CsyHost:liuge1 liuge36$ git branch
* dev
  master

# vim text.txt
修改这个文件的内容
:wq

CsyHost:liuge1 liuge36$ git add test.txt
CsyHost:liuge1 liuge36$ git commit -m "first dev "


这个时候,text.txt文件已经发生了改变

# 我们切换回master分支查看文件是否有变化
CsyHost:liuge1 liuge36$ git checkout master
切换到分支 'master'
您的分支与上游分支 'origin/master' 一致。
CsyHost:liuge1 liuge36$ cat test.txt
1111111
别人做了代码更改


明显:master 分支的代码是没有变化的,满足我们的预期

(好比:我们在项目中,
创建新的分支(新分支一开始和master分支代码一致)之后,
我们对新的分支,进行代码的修改,提交,
这样不影响,master分支的代码
)

# 将dev 分支提交到远程分支去
CsyHost:liuge1 liuge36$ git branch -a
  dev
* master
  remotes/origin/master
CsyHost:liuge1 liuge36$ git push --set-upstream origin dev
枚举对象: 5, 完成.
对象计数中: 100% (5/5), 完成.
使用 8 个线程进行压缩
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 305 bytes | 305.00 KiB/s, 完成.
总共 3 (差异 0),复用 0 (差异 0)
remote:
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/liuge36/liuge1/pull/new/dev
remote:
To github.com:liuge36/liuge1.git
 * [new branch]      dev -> dev
分支 'dev' 设置为跟踪来自 'origin' 的远程分支 'dev'。
CsyHost:liuge1 liuge36$ git branch -a
  dev
* master
  remotes/origin/dev
  remotes/origin/master
CsyHost:liuge1 liuge36$



拓展:项目分支的删除
场景:线上的应用程序在运行过程中,发现了bug。
bug的修复,肯定是不能在当前工作dev分支上修复,
,一般来说,是在master这个生产分支上操作。


这个时候,从master分支(运行在生产环境的代码)拉出来一个分支
基于master分支,拉出来一份代码(bug分支),修改Bug,测试通过,合并到主分支,上线
上线之后,刚刚这个bug分支就没有用了,就需要删除

说白了就是有的分支已经合并到master分支上面去了,
就没有用了,就需要删除


---------------------
# 查看本地在哪一个分支上
git branch
git branch -a

git checkout -b bug
修复好merge到master
合并到主分支

# 当前工作分支为bug,删除bug
git branch -d bug

失败,因为需要切换到别的分支上

# 切换到master分支上
git checkout master

# 删除本地的bug分支
git branch -d bug

删除分支成功

# 同时要删除远程的bug 分支,怎么办?
 git branch -r -d origin/bug
 # 再加,把本地操作 推送到远程,就能删除远程的bug分支
 git push origin :bug

小结:
git branch
git branch -a
git branch -d bug
git checkout -b dev
git push --set-upstream origin dev
多人同时开发同一项目不同的模块时候
不想和别人(master或其余人)的代码起冲突,就可以建立新的分支,进行工作

丢弃工作区的改动:git checkout -- test.txt
git checkout -- test.txt

第四步:分支的合并操作
多人开发,合并分支
# 创建分支
git checkout -b dev2
CsyHost:liuge1 liuge36$ vim dev2Coder.txt


需要合并,先要推送到远程仓库
CsyHost:liuge1 liuge36$ git add dev2Coder.txt
CsyHost:liuge1 liuge36$ git commit -m "add:合并内容"
[dev2 bd4f717] add:合并内容
 1 file changed, 1 insertion(+)
 create mode 100644 dev2Coder.txt
CsyHost:liuge1 liuge36$ git push --set-upstream origin dev2
枚举对象: 4, 完成.
对象计数中: 100% (4/4), 完成.
使用 8 个线程进行压缩
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 317 bytes | 317.00 KiB/s, 完成.
总共 3 (差异 0),复用 0 (差异 0)
remote:
remote: Create a pull request for 'dev2' on GitHub by visiting:
remote:      https://github.com/liuge36/liuge1/pull/new/dev2
remote:
To github.com:liuge36/liuge1.git
 * [new branch]      dev2 -> dev2
分支 'dev2' 设置为跟踪来自 'origin' 的远程分支 'dev2'。
CsyHost:liuge1 liuge36$


# 切换到dev分支上

CsyHost:liuge1 liuge36$ git checkout dev
切换到分支 'dev'
您的分支与上游分支 'origin/dev' 一致。
CsyHost:liuge1 liuge36$ git branch
* dev
  dev2
  master
CsyHost:liuge1 liuge36$ ll
total 8
drwxr-xr-x   4 liuge36  staff   128 11 12 11:30 ./
drwx------@ 36 liuge36  staff  1152 11 12 11:30 ../
drwxr-xr-x  14 liuge36  staff   448 11 12 11:30 .git/
-rw-r--r--   1 liuge36  staff    76 11 12 11:30 test.txt
CsyHost:liuge1 liuge36$ git merge dev2
提示:等待您的编辑器关闭文件... error: There was a problem with the editor 'vi'.
未提交合并,使用 'git commit' 完成此次合并。
CsyHost:liuge1 liuge36$ ll
total 16
drwxr-xr-x   5 liuge36  staff   160 11 12 11:30 ./
drwx------@ 36 liuge36  staff  1152 11 12 11:30 ../
drwxr-xr-x  17 liuge36  staff   544 11 12 11:31 .git/
-rw-r--r--   1 liuge36  staff    28 11 12 11:30 dev2Coder.txt
-rw-r--r--   1 liuge36  staff    76 11 12 11:30 test.txt
CsyHost:liuge1 liuge36$ git branch
* dev
  dev2
  master
CsyHost:liuge1 liuge36$ git status
位于分支 dev
您的分支与上游分支 'origin/dev' 一致。

所有冲突已解决但您仍处于合并中。
  (使用 "git commit" 结束合并)

要提交的变更:

	新文件:   dev2Coder.txt

CsyHost:liuge1 liuge36$
CsyHost:liuge1 liuge36$ git commit -m "add :从dev2合并过来的代码"
[dev 7b53006] add :从dev2合并过来的代码
CsyHost:liuge1 liuge36$ git status
位于分支 dev
您的分支领先 'origin/dev' 共 2 个提交。
  (使用 "git push" 来发布您的本地提交)

无文件要提交,干净的工作区
CsyHost:liuge1 liuge36$


小结:
dev分支下合并dev2分支的代码:
git merge dev2





第五步:分支的合并产生的冲突问题



***工作中:master分支 是用来合并的,是不能在上面开发的

这里测试,使用dev2和master制造冲突:

CsyHost:liuge1 liuge36$ git merge dev2
更新 06f9c5e..bd4f717
Fast-forward
 dev2Coder.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 dev2Coder.txt
CsyHost:liuge1 liuge36$ ll
total 16
drwxr-xr-x   5 liuge36  staff   160 11 12 11:30 ./
drwx------@ 36 liuge36  staff  1152 11 12 11:30 ../
drwxr-xr-x  14 liuge36  staff   448 11 12 11:48 .git/
-rw-r--r--   1 liuge36  staff    28 11 12 11:48 dev2Coder.txt
-rw-r--r--   1 liuge36  staff    33 11 12 11:42 test.txt
CsyHost:liuge1 liuge36$ git status
位于分支 master
您的分支领先 'origin/master' 共 1 个提交。
  (使用 "git push" 来发布您的本地提交)

无文件要提交,干净的工作区
CsyHost:liuge1 liuge36$

CsyHost:liuge1 liuge36$ git branch
  dev
  dev2
* master
CsyHost:liuge1 liuge36$ git push
总共 0 (差异 0),复用 0 (差异 0)
To github.com:liuge36/liuge1.git
   06f9c5e..bd4f717  master -> master
CsyHost:liuge1 liuge36$


场景:多人协作开发的时候,大家同时修改同一个文件的时候
比如:刚刚的dev2分支,修改了文件test.txt的第三行
,另外的master分支也修改了第三行。

我们分别在dev2和master 都对test.txt 做修改
然后再分别提交到远程分支

# 这个时候,我们在master分支将dev2的代码合并过来
CsyHost:liuge1 liuge36$ git checkout master
切换到分支 'master'
您的分支与上游分支 'origin/master' 一致。
CsyHost:liuge1 liuge36$ git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。

无文件要提交,干净的工作区
CsyHost:liuge1 liuge36$
CsyHost:liuge1 liuge36$
CsyHost:liuge1 liuge36$ git merge dev2
自动合并 test.txt
冲突(内容):合并冲突于 test.txt
自动合并失败,修正冲突然后提交修正的结果。
CsyHost:liuge1 liuge36$


CsyHost:liuge1 liuge36$ git merge dev2
error: 无法合并,因为您有未合并的文件。
提示:请在工作区改正文件,然后酌情使用 'git add/rm <文件>' 命令标记
提示:解决方案并提交。



# 解决分支冲突之后,再次提交
git add test.txt
git commit -m "解决合并冲突"

git push

**gitignore========**

*.class

#package file
*.war
*.ear

#maven
target/

#eclipse ignore
.settings/
.project
.classpatch


#idea
.idea/
/idea/
*.ipr
*.iml
*.iws

#temp file
*.log
*.cache
*.diff
*.patch
*.tmp

# system ignore
.DS_Strore
Thnmbs.db

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值