赞
踩
心得:继续努力拿下Git,版本管控是你的了,你正在一步一步慢慢的走向成功的路上。
成功注册了github同时上传了关于Django项目的代码,持续更新中:
https://github.com/Laughingtt
集中式版本控制:
版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推送给中央服务器。中央服务器就好比是一个图书馆,你要改一本书,必须先从图书馆借出来,然后回到家自己改,改完了,再放回图书馆。必须在线更新,对带宽的需求比较大。
分布式版本控制
分布式版本控制系统根本没有“中央服务器”,每个人的电脑上都是一个完整的版本库,这样,你工作的时候,就不需要联网了,因为版本库就在你自己的电脑上。既然每个人电脑上都有一个完整的版本库,那多个人如何协作呢?比方说你在自己电脑上改了文件A,你的同事也在他的电脑上改了文件A,这时,你们俩之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。
1.进入官网选择版本
https://git-scm.com/downloads
2.全部按照默认一直往下走,详细安装步骤请参考网址:
https://www.cnblogs.com/theone-unicorn/p/10645910.html
3.打开git bash配置SSH KEY
git config --global user.name "username"
git config --global user.email "123456789@163.com"
ssh-keygen -t rsa -C "123456789@163.com"
cd ~/.ssh
cat id_rsa.pub
//拷贝秘钥 ssh-rsa开头
cd ~/.ssh
cat id_rsa.pub
1 在本地新建git项目仓库
git init firstproject
2 克隆远程服务器的工程项目
git clone [SSH_address]
3 将修改的文件加入待提交区也就暂存区
追踪单个文件
git add filename
全部追踪tracked
git add .
4 删除文件,当需要将已提交的文件删除,删除之后还需要再确认提交一次
git rm a.txt
git commit -m "del a.txt"
git rm # 删除工作区,添加到暂存区
rm # 仅删除工作区
5 移动/改名
git mv file1 file2
6 对比
当我们在工作区修改过还没有添加都暂缓区的时候可以用这些命令查看刚刚在工作区改动了哪里。
git diff <filename>
7 查看状态工作区,注意提示语句
git status
8 提交
提交单个文件
git commit filename -m "commit_mssage"
提交全部文件
git commit -am "all_file"
9 查看日志
git log
扩展用法:
git log -1
#只查看最新一行
git log --name-status
#可以查看文件的惭怍类型
10 推送
git push origin branch_name
其中orgin是远程端的名称,我们可以通过git remote
进行查看
11 分支的相关操作
git branch new_branch_name
git checkout -b new_branch_name
git branch
git branch -r #查看remote端的分支
git branch -a #查看本地和远端的分支
git branch -d branch_name
git branch -d -r branch_name #删除远端的分支
git checkout branch_name
pull:拉取数据会合并
git pull origin remote_branch:local_branch
fetch:不会合并
git fetch origin remote_branch:local_bracn
冲突造成的原因:
如果我切换到分支b ,修改了master 原本哪行的代码,提交.然后再切换到master.我不知道b修改了这个文件.我也修改这一行的代码,并提交.好了,“两个分支相同文件相同位置的的不同操作” 我这个时候就在master merge b 或在 b上merge master. 冲突!!! 不解决冲突是没法提交和切换到别的分支的. 反正这个冲突后的文件必须被改动一下.
1>直接合并分支
git merge feature
HowiedeiMac:hello howie$ git merge feature
Auto-merging newFunc.go
CONFLICT (content): Merge conflict in newFunc.go
Automatic merge failed; fix conflicts and then commit the result.
2>竟然失败了,说明我两个分支之前的版本已经不同步了,需要手动合并冲突,再提交:
先查看冲突文件:git status
HowiedeiMac:hello howie$ git status
On branch master
Your branch is ahead of 'origin/master' by 7 commits.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: newFunc.go
3>打开文件,进行修改
原文件:
func NewFunc() {
<<<<<<< HEAD
=======
fmt.Println("add new func")
>>>>>>> feature
}
修改后:
func NewFunc() {
fmt.Println("add new func")
}
4>现在通过add添加,然后commit提交
HowiedeiMac:hello howie$ git add newFunc.go
HowiedeiMac:hello howie$ git commit -m 'merge master and feature'
[master 562ec58] merge master and feature
5>现在在查看一下分支提交历史:
HowiedeiMac:hello howie$ git log --oneline --graph
* 562ec58 (HEAD -> master) merge master and feature
|\
| * 4f58ab8 (feature) add new func
* | 0e80f97 do something
|/
* 94c134b init base
merge:
git merge branch_name self_branch
merge 会把公共分支和你当前的commit 合并在一起,形成一个新的 commit 提交
git rebase branch_name
rebase会把你当前分支的 commit 放到公共分支的最后面,所以叫变基。就好像你从公共分支又重新拉出来这个分支一样。
详细解释rebase和merge区别
用rebase开发和并新功能
1>现在将版本退回到合并前,也就是回退一个版本 git reset --hard head^
HowiedeiMac:hello howie$ git reset --hard head^
HEAD is now at 0e80f97 do something
HowiedeiMac:hello howie$ git log --oneline --graph
* 0e80f97 (HEAD -> master) do something
* 94c134b init base
2>先切换回feature分支:在feature分支上执行: git rebase master
这句命令的意识是:以master为基础,将feature分支上的修改增加到master分支上,并生成新的版本。
HowiedeiMac:hello howie$ git rebase master First, rewinding head to replay your work on top of it... Applying: add new func Using index info to reconstruct a base tree... M newFunc.go Falling back to patching base and 3-way merge... Auto-merging newFunc.go CONFLICT (content): Merge conflict in newFunc.go error: Failed to merge in the changes. Patch failed at 0001 add new func hint: Use 'git am --show-current-patch' to see the failed patch Resolve all conflicts manually, mark them as resolved with "git add/rm <conflicted_files>", then run "git rebase --continue". You can instead skip this commit: run "git rebase --skip". To abort and get back to the state before "git rebase", run "git rebase --abort".
3>失败了,原因很简单,两个分支修改个同一个文件,产生了冲突。所以先需要解决冲突:
打开冲突的文件,解决冲突
原文件:
func NewFunc() {
<<<<<<< HEAD
=======
fmt.Println("add new func")
>>>>>>> add new func
}
修改后:
func NewFunc() {
fmt.Println("add new func")
}
4>现在通过add添加
HowiedeiMac:hello howie$ git add newFunc.go
5>现在是重点,之前的rebase其实只是完成了一半,由于出现冲突而终止,现在冲突解决,可以通过git rebase —continue继续完成之前的rebase操作。
HowiedeiMac:hello howie$ git rebase --continue
Applying: add new func
6>rebase完成,再查看一下提交历史:
HowiedeiMac:hello howie$ git log --oneline --graph
* b2593e6 (HEAD -> feature) add new func
* 0e80f97 (master) do something
* 94c134b init base
7>提交记录已经是一条完美的直线。现在切换到主分支master,将feather分支上的提交合并过来。
git checkout master
git merge feature
HowiedeiMac:hello howie$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 7 commits.
(use "git push" to publish your local commits)
HowiedeiMac:hello howie$ git merge feature
Updating 0e80f97..b2593e6
Fast-forward
newFunc.go | 1 +
1 file changed, 1 insertion(+)
再次查看一下提交历史:
HowiedeiMac:hello howie$ git log --oneline --graph
* b2593e6 (HEAD -> master, feature) add new func
* 0e80f97 do something
* 94c134b init base
删除分支
branch branch -d feature
echo "# python" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:Laughingtt/python.git
git push -u origin master
12 撤销操作
git reset commit_id
回退到对应提交id的那个版本,新增记录会被删除
13 冲突问题
分支冲突:
在first和master分支下分别存在c.txt的文件,但第一行的内容不同,我们将这两个分支的内容提交后,合并发现冲突:
进入冲突文件中将,冲突的行删除,并将标志性的特殊符号也删除,进一步提交,MERGING会消失,显示合入成功。
merge request冲突
1> Fork是指,复制别人的代码仓中的代码到自己的仓库中
2> 如果需要合入主仓库的代码,需要发出merge request,将分支合入到主仓库中
3> 当发生merge request 冲突时,可以在线上点击resolve conflict来进行解决,选择需要保留的代码
4> 进一步merge就会成功
附录:
~/.bash_profile
文件或者全局文件/etc/bashrc
中添加需要运行的脚本文件即可。{‘name’:‘tian’,‘age’:‘18’}
如何在shell中读取键对应的值呢,我们可以用正则来实现一下:
cat dic.json | sed 's/[,{}]/\n/g' | grep "name" |awk '{print $1}' |sed 's/'//g'
将,逗号替换为换行符
匹配具有name的行
截取一行中第二列字符
去掉行中单引号’
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。