TracerLee / tracerlee.github.io

Personal blog written by Tracer
4 stars 0 forks source link

版本控制工具 #4

Open TracerLee opened 8 years ago

TracerLee commented 8 years ago

git和github的使用(Windows)

安装 git

TracerLee commented 8 years ago

git命令思维导图

TracerLee commented 8 years ago

git常用

提交

$ git commit -m "some msg" -a
$ git commit -m "add msg to readme.txt" readme.txt    

撤销修改

$ git checkout head readme.txt todo.txt
$ git checkout head *.txt
$ git checkout head .

分支

$ git branch
$ git branch -a
$ git branch <branchname>
$ git checkout <branchname>
$ git merge <branchname>
$ git branch -m <branchname> <newname>
$ git merge tool

标签

$ git tag 1.0

日常

$ git status
$ git log
$ gitk
$ git clone <url>
$ git branch -r
$ git pull
$ git push origin master
TracerLee commented 7 years ago

摘抄他人总结

git笔记

git使用之前的配置

1. git config --global user.email xxx@163.com
2. git config --global user.name xxx
3. ssh-keygen -t rsa -C xxx@163.com(邮箱地址)      // 生成ssh
4. 找到.ssh文件夹打开,使用cat id_rsa.pub    //打开公钥ssh串
5. 登陆github,settings - SSH keys  - add ssh keys (把上面的内容全部添加进去即可)

说明: 然后这个邮箱(xxxxx@gmail.com)对应的账号在github上就有权限对仓库进行操作了。可以尽情的进行下面的git命令了。

git常用命令

1. git config user.name  /  user.email     //查看当前git的用户名称、邮箱
2. git clone https://github.com/jarson7426/javascript.git  project  //clone仓库到本地。
3. 修改本地代码,提交到分支:  git add file   /   git commit -m “新增文件”
4. 把本地库推送到远程库:  git push origin master
5. 查看提交日志:git log -5
6. 返回某一个版本:git reset --hard 123
7. 分支:git branch / git checkout name  / git checkout -b dev
8. 合并name分支到当前分支:git merge name   /   git pull origin
9. 删除本地分支:git branch -D name
10. 删除远程分支: git push origin  :daily/x.x.x
11. git checkout -b mydev origin/daily/1.0.0    //把远程daily分支映射到本地mydev分支进行开发
12. 合并远程分支到当前分支 git pull origin daily/1.1.1
13. 发布到线上:
    git tag publish/0.1.5
    git push origin publish/0.1.5:publish/0.1.5
14. 线上代码覆盖到本地:
    git checkout --theirs build/scripts/ddos
    git checkout --theirs src/app/ddos

参考: http://luckykun.com/work/2016-10-11/com-func-skill.html

TracerLee commented 7 years ago

.gitignore

文件 .gitignore 的格式规范如下: 所有空行或者以注释符号 # 开头的行都会被 Git 忽略。 可以使用标准的 glob 模式匹配。 匹配模式最后跟反斜杠(/)说明要忽略的是目录。 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。

所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。 星号(*)匹配零个或多个任意字符; [abc] 匹配任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c); 问号(?)只匹配一个任意字符; 如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配(比如 [0-9] 表示匹配所有 0 到 9 的数字)。

我们再看一个 .gitignore 文件的例子:

# 忽略所有 .a 结尾的文件
*.a

# 但 lib.a 除外
!lib.a

# 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
/TODO

# 忽略 build/ 目录下的所有文件
build/

# 会忽略 doc/notes.txt 但不包括 doc/server/notes.txt
doc/notes.txt

自动生成 .gitignore 文件的网址:http://www.gitignore.io/

参考: https://segmentfault.com/a/1190000000522997

TracerLee commented 7 years ago

svn回滚

更新项目至版本

将你的工作副本更新到选中的版本。如果你想要你的工作副本折返到过去的某个时间,或者在版本库中有一系列提交而你想每次只更新工作副本一小步,那这个功能就很好用。你最好是更新工作副本的整个目录而不是单一某个文件,因为如果只更新某个文件,否则你的工作副本就可能不一致。

如果你想要永久撤销先前的更改,使用 复原到此版本。

复原到此版本

恢复到某个以前的版本。如果你做了多处修改,然后决定要返回到版本 N,你就可以使用这个命令。恢复的修改位于你的工作副本,在你提交之前,并不会影响版本库。注意,这将会丢弃从那个版本以来的所有修改,使用选中的版本来替换文件/文件夹。

如果你的工作副本处于未修改的状态,在执行此操作后,工作副本将会显示为已修改。如果你已经进行了本地修改,这个命令将会把撤销的改变合并至你的工作副本中。

内部的动作是 Subversion 对选中版本之后的修改内容执行了反向合并,撤销这些先前提交产生的影响。

如果在执行这个动作后你察觉到你需要撤销这次撤销并且让工作副本返回到先前没有修改的状态,你应该在 Windows 资源管理器中使用 TortoiseSVN → SVN 还原, 这个命令将会丢弃本次撤销动作带来的修改。

如果你只是想看看某个文件或者文件夹在先前的版本是什么样子,使用 更新至版本 或 保存版本为... 功能替代此操作。

复原此版本作出的修改

还原选中版本所做的修改。还原的内容只在你的工作副本中,所以此操作完全不会影响版本库!要注意的是,这个操作仅仅还原该版本中的修改。不是将整个文件替换成选中的那个版本。它对于已经做过其它无关修改的还原早期修改非常有用。

如果你的工作副本处于未修改的状态,在执行此操作后,工作副本将会显示为已修改。如果你已经进行了本地修改,这个命令将会把撤销的改变合并至你的工作副本中。

内部的动作是 Subversion 对这个版本的修改内容执行了反向合并,撤销先前提交产生的影响。

你可以使用上文复原到此版本中描述的撤销这次撤销。

复原此版本作出的修改可能会导致树冲突,我使用的方式是先导出某个想要回滚的版本到本地文件夹,然后把当前版本的文件全部删除,然后将导出的这份文件复制进去,再执行提交操作,就生成一个新版本了

参考: https://tortoisesvn.net/docs/nightly/TortoiseSVN_zh_CN/tsvn-dug-showlog.html

TracerLee commented 7 years ago

warning: LF will be replaced by CRLF in .gitignore.

warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory.

出现这个是因为Windows使用回车和换行两个字符来结束一行,而Mac和Linux只使用换行一个字符。

在windows机器上解决方式:

打开bash工具

$ git config --global core.autocrlf true

参考: http://blog.csdn.net/lysc_forever/article/details/42835203

TracerLee commented 7 years ago

更新远程分支source到本地分支source

语法

$ git pull <远程主机名> <远程分支名>:<本地分支名>
$ git pull origin source:source

参考: http://www.ruanyifeng.com/blog/2014/06/git_remote.html

TracerLee commented 7 years ago

分支操作

git拉取远程分支并创建本地分支

一、查看远程分支

使用如下Git命令查看所有远程分支:

git branch -r

二、拉取远程分支并创建本地分支

使用如下命令:

git checkout -b 本地分支名x origin/远程分支名x

使用该方式会在本地新建分支x,并自动切换到该本地分支x。

使用如下命令:

git fetch origin 远程分支名x:本地分支名x

使用该方式会在本地新建分支x,但是不会自动切换到该本地分支x,需要手动checkout。

参考: http://blog.csdn.net/jtracydy/article/details/53174175

TracerLee commented 7 years ago

git alias

创建文件,~/.gitconfig

[alias]
    st = status
    cm = commit
    co = checkout
    br = branch
    df = diff
    unstage = reset HEAD --
    last = log -1 HEAD

git常用命令

TracerLee commented 7 years ago

手动Merge Request

Check out, review, and merge locally

Step 1. Fetch and check out the branch for this merge request

git fetch origin
git checkout -b feature-supercall-cb origin/feature-supercall-cb

Step 2. Review the changes locally

Step 3. Merge the branch and fix any conflicts that come up

git checkout develop
git merge --no-ff feature-supercall-cb

Step 4. Push the result of the merge to GitLab

git push origin develop

Tip: You can also checkout merge requests locally by following these guidelines.

TracerLee commented 6 years ago

初始化git项目(流程来自gitlab)

Command line instructions

Git global setup

git config --global user.name "name"
git config --global user.email "email"

Create a new repository

git clone git@git.kgidc.cn:webteam/hifi.git
cd hifi
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Existing folder

cd existing_folder
git init
git remote add origin git@git.kgidc.cn:webteam/hifi.git
git add .
git commit
git push -u origin master

Existing Git repository

cd existing_repo
git remote add origin git@git.kgidc.cn:webteam/hifi.git
git push -u origin --all
git push -u origin --tags
TracerLee commented 6 years ago

TortoiseGit 配置 SSH KEY

http://blog.csdn.net/liyuanbhu/article/details/50953377

TracerLee commented 6 years ago

git merge / git merge -no-ff / git merge -squash

根据这张图片可以看出: git merge –no-ff 可以保存你之前的分支历史。能够更好的查看 merge 历史,以及 branch 状态。 git merge 则不会显示 feature,只保留单条分支记录。

–no-ff 指的是强行关闭 fast-forward 方式。

fast-forward 方式就是当条件允许的时候,git 直接把 HEAD 指针指向合并分支的头,完成合并。属于“快进方式”,不过这种情况如果删除分支,则会丢失分支信息。因为在这个过程中没有创建 commit。

git merge –squash 是用来把一些不必要 commit 进行压缩,比如说,你的 feature 在开发的时候写的 commit 很乱,那么我们合并的时候不希望把这些历史 commit 带过来,于是使用 –squash 进行合并,此时文件已经同合并后一样了,但不移动 HEAD,不提交。需要进行一次额外的 commit 来“总结”一下,然后完成最终的合并。

总结: –no-ff :不使用 fast-forward 方式合并,保留分支的 commit 历史。 –squash :使用 squash 方式合并,把多次分支 commit 历史压缩为一次。

TracerLee commented 6 years ago

HEAD

在Git中,用HEAD表示当前版本,也就是最新的提交3628164...882e1e0(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。

TracerLee commented 6 years ago

git reset

参考:http://classfoo.com/ccby/article/j4HZbSN

TracerLee commented 6 years ago

7.8 Git 工具 - 高级合并

参考:https://git-scm.com/book/zh/v2/Git-工具-高级合并

TracerLee commented 6 years ago

Git Diff 显示中文

git config --global core.quotepath false