Git学习笔记

http://www.backlog.jp/git-guide/ 入门教程网址(日语)
http://rogerdudler.github.io/git-guide/index.zh.html  简单入门教程(中文)

基础篇

概念

1.Repository:保存文件和目录的状态的场所。分为Remote 和Local两种。

2.新建Local Repository方法有两种:克隆远程Repository和Local新建Repository

3.Work Tree:用户实际操作的目录

4.Index:向Repository Commit作准备的存储区域。可以把部分想要Commit的更改存入Index然后向Repository Commit。


下载安装与初期设定

Git下载地址http://git-scm.com/

查看Git版本

$ git --version

登录用户名和邮箱

$ git config --global user.name "<ユーザ名>"
$ git config --global user.email "<メールアドレス>"
设置Git输出颜色

$ git config --global color.ui auto
新建Repository

新建目录然后进入目录执行下列command

$ git init

基本操作

查看Work Tree和Index的状态,查看当前的文件状态(Untracked,unmodified,modified,staged)

$ git status

Git Add作用:1 向Index中加入指定文件,即将文件集快照放到暂存区域(index) 2 将文件加入跟踪集合

$ git add <file>..

git rm:将要删除的文件从暂存区(index)移除

$ git rm  <file>..                  -f 强制性从index中删除, --cached 从index中删除但是在working tree中保留
git mv:文件改名,先当以下三个操作

$ mv README.txt README

$ git rm README.txt

$ git add README


Commit  : -a 可以省略使用git add将文件加入Index的步骤,将所有以跟踪的发生改变的文件提交。

$ git commit -m "<コメント>"   直接在命令行中指定Commit的Comment  
$ git commit -a -m "<Comment>"  省略git add 直接将unstaged文件Commit
查看Log

$ git log       --stat 参数 显示简要的变更行数统计   --graph 图表形式显示log
在GUI中查看Git变更历史

$ gitk

追加远程Repository

$ git remote add <name> <url>
例子:

$ git remote add origin https://[your_space_id].backlog.jp/git/[your_project_key]/tutorial.git
Push

$ git push <repository> <refspec>...
<Repository>远程Repository名  

<Refspec>远程Branch名

$ git push -u origin master
Username: <ユーザ名>
Password: <パスワード>
Counting objects: 3, done.
Writing objects: 100% (3/3), 245 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://nulab.backlog.jp/git/BLG/tutorial.git
 * [new branch]      master -> master
-u 参数 下次Push的时候可以省略Branch名


Clone

$ git clone <repository> <directory>
例子

$ git clone https://nulab.backlog.jp/git/BLG/tutorial.git tutorial2
Cloning into 'tutorial2'...
Username: <ユーザ名>
Password: <パスワード>
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.

应用篇

Branch :分为Master分支和Topic分支

checkout 切换分支,现将元分支的最后Commit的内容复制到WorkTree,然后,Checkout后的Commit将面向新的分支。

HEAD:现在使用分支的头部,Head的移动代表着现在的分支进行了变更。默认的Head是master分支的头部。


Stash:文件的变更内容暂时存储的位置。


MERGE和REBASE

MERGE:在master branch 生成新的Merge 履历E


REBASE:先Rabase master Branch到D 然后Merge X’ Y'的修改。



为了保持变更履历简洁,一般Topic获得Master的变更用Rebase,Master获得Topic的变更先用rebase后merge。


新建Branch

$ git branch <branchname>
查看现有Branch,带星号的是现在的Branch

$ git branch
  issue1
* master

删除branch

$ git branch -d <branchname>
现在新建的Branch在本地,除非你将它Push到远程否则他是对其他用户不可见的

$ git push origin <branchname>


Checkout :-b的意思是 新建Branch并切换到新建的Branch

$ git checkout <branch>
$ git checkout -b <branch>

替换本地改动:此命令会用最后的Commit的内容更换掉本地的WorkTree的内容,且保持加入到Index暂存区的修改不变

$ git checkout -- <filename>
如果想要丢弃本地的改动与提交,可以到远程仓库中获取最新的版本历史,并将你的本地主分支指向它。

$ git fetch origin
$ git reset --hard origin/master


Merge

$ git merge <commit>
将Topic Branch的变更内容Merge到Master先要切换Branch到Master

$ git checkout master
Switched to branch 'master'
然后从Topic Branch向Master进行Merge

$ git merge issue1
Updating 1257027..b2b23c4
Fast-forward
 myfile.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

发生了冲突Conflict时可以用diff查看差异

$ git diff <source_branch> <target_branch>
$ git diff     不加任何文件名可以查看工作目录中未添加到缓存区的变更文件和暂存区域快照之间的差异,添加 --cached 或 --staged 参数可以对比暂存区快照和最后一次提交时的快照之间的差异 
推出 git diff 结果的方法  键入q

删除Branch

$ git branch -d <branchname>
Rebase 

$ git checkout issue3
Switched to branch 'issue3'
$ git rebase master
rebase出现Conflict 人工修正后Continue Rebase

$ git add myfile.txt
$ git rebase --continue
回到Master重新fast forward Merge

$ git checkout master
Switched to branch 'master'
$ git merge issue3
Fetch:取得远程的变更历史

Pull:Fetch加Merge


Tag

追加轻量级Tag

$ git tag <tagname>  
$ git tag <tagname> <commitname> 可以指定提交ID的头几位,例如头十位,只要没有重复就可以
表示所有Tag

$ git tag
显示包含Tag的Log

$ git log --decorate
追加带注释的tag

$ git tag -a <tagname>
-m 指的是追加注释

$ git tag -am "サルでもわかるGit" banana
$ git tag -n
apple           first commit
banana          サルでもわかるGit
git tag -n 查看带注释的Tag


修正之前的Commit

$ git add sample.txt 将需要Commit的文件加入Index
$ git commit --amend 修正之前的Commit

REVERT:创建一个变更历史取消目前的Commit的变更


$ git revert HEAD
RESET:使用Reset可以删除变更历史回到之前的状态


$ git reset --hard HEAD~~
回到Reset之前的状态,错误的Reset恢复用。

$ git reset --hard ORIG_HEAD

git reset 取消某个文件的staged状态

$ git reset HEAD <filename>...

Cherry-pick:顾名思义,摘樱桃,即从别的分支摘取变更到目前的分支。

$ git checkout master 切换分支
Switched to branch 'master'
$ git cherry-pick 99daed2 

rebase -i :将过去的变更履历合并

$ git rebase -i HEAD~~
上文的命令是合并最后的变更到倒数第三个变更之间的变更历史,不包括倒数的三个。上述命令合并的是HEAD和HEAD~

再打开的Editor中把第二行的Pick改为squash 保存

pick 9a54fd4 commitの説明を追加
pick 0d4a808 pullの説明を追加

# Rebase 326fc9f..0d4a808 onto d286baa
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
rebase -i :修正过去的变更历史

$ git rebase -i HEAD~~
pick 9a54fd4 commitの説明を追加
pick 0d4a808 pullの説明を追加

# Rebase 326fc9f..0d4a808 onto d286baa
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
把第一行的pick改为edit,保存后显示下面的Message

Stopped at d286baa... commitの説明を追加
You can amend the commit now, with

        git commit --amend

Once you are satisfied with your changes, run

        git rebase --continue
然后可以打开文件编辑Commit中想要修正的地方,然后重新Commit

$ git add sample.txt
$ git commit --amend
最后继续Rebase 

$ git rebase --continue
 中途取消Rebase

$ git rebase --abort
Rebase后想取消之前的Rebase,可以用   git reset  -- hard ORIG_HEAD


merge --squash:将其他的Branch的变更合并,然后读取到当前的Branch

$ git checkout master
Switched to branch 'master'
$ git merge --squash issue1
Auto-merging sample.txt
CONFLICT (content): Merge conflict in sample.txt
Squash commit -- not updating HEAD
Automatic merge failed; fix conflicts and then commit the result.
如果发生了Conflict 就打开文件进行修正然后重新Commit

$ git add sample.txt
$ git commit
[master 0d744a7] Conflicts:   sample.txt
 1 files changed, 4 insertions(+), 0 deletions(-)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值