Git 学习

来源:互联网 发布:mac无法切换大小写 编辑:程序博客网 时间:2024/06/05 14:46

  Git 是一款出色的源码版本控制工具. 不论是团队协作还是个人开发,都推荐使用这个简单高效的工具.

  这里是权威的 Git 参考手册 .

  

Git特点

  Git与其他版本管理工具的最大不同在于, 其他如SVN将所有数据视为"文件",而Git将数据视为 "快照".

Git不会为相同文件制作副本,而仅仅是创建一条链接.这种策略带来了高效.


Git 学习笔记

Git 有命令行工具,也有部分图形化界面,具体安装信息详见Git 参考手册 

使用git,你将需要在本地和远程各创建一个仓库(repository)来存放数据.

(repository相当于一块git专属管理的磁盘空间,若不需要与他人共享代码也可不创建远程仓库)

建议:

1. 熟练使用git reset(主要用来撤销)很有帮助,点此链接 git reset

2. 不要rebase任何你已经推送push的东西.

In general the way to get the best of both worlds is to rebase local changes you’ve made but haven’t shared yet before you push them in order to clean up your story, but never rebase anything you’ve pushed somewhere. 


配置相关

git config --list
#显示当前配置

git help config 
#will open a browser to show html help.

git config --global alias.last 'log -1 HEAD'
#为命令配置一个别名,命令git last就等同于log -1 HEAD 显示最后一次提交日志


git命令

  • git add <file>
  • git commit -m 'comment of this commit'
#这两行命令的作用
(1)track a new file; 
(2)stage file (if you have changed a file which is added to the commit, you will only need to add it again.)
(3)merge-conflicted file.
注: (使用git commit -a 可替代以上两行命令)#add all tracked file to commit.即 git add + git commit
  • git rm <file>
    #both remove file from working directory and staging area.
    If you modified the file and added it to the index already, you must force the removal with the -f option.
  • git rm --cached temp/\*.log
#删除已缓存的文件
  • git mv README.md README
#相当于三条指令:硬盘移动文件,git add, git remove

  • git diff
  • git diff --staged <--cached>
    #compares what is in your working directory with what is in your staging area. 
  • git difftool --tool
#使用对比工具
为已经commit添加新文件,或者修改提交备注
$ git commit -m 'initial commit'$ git add forgotten_file$ git commit --amend

标签
  • git tag -l "3.0*" #列出说有匹配的标签
  • git tag -a 3.0.1 -m "version 3.8.1" #create a tag with its msg
  • git show 3.8.1 #显示指定标签
  • git tag 3.8.2 #create a simple tag
  • git push origin [tagname] #不会默认在远程repository:origin上创建标签
  • git checkout testing
#switch to branch "testing" 把 HEAD指向这个branch,注意branch之间是完全独立的.
  • git log
#打印提交日志,可以添加参数来控制显示.比如git log --author Tom 就是显示所有Tom的提交日志.

Git remote

git remote    #show your remote repo
git remote -v#list servers
git remote show [remote-name]  #show detailed info 

git remote rename remote_repo1  remote_repo2
git remote rm     remote_repo1

git remote add [shortname] [url]  #add a remote repository
git fetch <remote repo>          #fetch all files you don't have from remote repo to local repo but won't auto merge
git pull#fetch from remote and auto merge
git clone#fetch from remote and auto merge ( master only )
git push [remote-name] [branch-name]#推送本次的commit到远程仓库下的指定分支

0 0