初识Git


安装git后,配置基本信息

git config --global user.name 'PerthCharles'  
git config --global user.email 'zhongbincharles@gmail.com'  
git config --global -l //查看全局配置信息

从github上将远端仓库clone至本地

git clone https://github.com/PerthCharles/perthcharles.github.com.git  

将新建件加入到仓库

git add new_file  

提交一次更新

git commit -a -m 'some note for this commit'  

将本地更新后的仓库提交到github

首先需要生成一个rsa key,然后在github的账号中添加好,之后才能提交更新。

ssh-keygen -C 'zhongbincharles@gmail.com' -t rsa  //一直ENTER即可  
git push origin master  

其他常用命令可以参考git基本命令参考

Git Real 1 from Code School


1.diff

//查看当前与最近一次提交的不同
#git diff  
//查看当前staged状态与最近一次提交的不同
#git diff --staged
//查看两个branch的差别
#git diff branch-name master
//查看上两次commit的不同
#git diff HEAD~2

2.amend

//将这次提交合并到上一次提交
#git commit --amend -m 'something'

3.reset

//将被staged的文件变成未staged状态
#git reset somefile
//撤销最近一次的commit, soft表示撤销commit,但是保留文件的更改
#git reset --soft HEAD^ 
//撤销最近一次的commit, hard表示撤销commit,同时放弃文件的更改
#git reset --soft HEAD^ 

4.checkout

//undo changes done to files, --用于保证后面跟的是文件,而不是分支名
#git checkout -- afile bfile
//切换到一个新的分支
#git checkout branch-name
//切换并新建一个分支
#git checkout -b branch-name
//回滚到之前的tag版本
#git checkout tag-name

5.remote

//添加remote的共享repository地址,origin 是名字
#git remote add origin git://example.com/git.git
//显示所有的remote列表
#git remote -v
//显示某个remote的详细信息
#git remote show origin

6.push/pull/fetch

//添加-u选项记住push的目标地址
#git push -u origin master
//获取远端更新
#git pull origin master
//提交更新到一个分支
#git push origin branch-name
//删除一个remote branch
#git push origin :weasel
//push tags 到 repository
#git push --tags

pull与fetch的不同可以用下面的等式表示

git pull = git fetch + git merge

7.branch

//新建branch
#git branch new-branch
//删除一个本地branch
#git branch -d branch-name

8.merge

//合并分支到master
#git merge branch-name master

9.tag

//显示tag
#git tag
//添加tag
#git tag -a tag-name -m 'message'

10.rebase
这部分的命令解释,最好还是看看官方文档来理解的好

//合并master新的更新到branch-name中
#git checkout branch-name
#git rebase master
//如果中途出现冲突,则要继续rebase
#git rebase --continue
//进入rebase编辑状态
#git rebase -i HEAD~4

11.log

//一行显示Log
#git log --pretty=oneline
//显示log的同时显示diff
#git log -p

12.blame

//查看某文件的更改历史,主要看被谁改动的
#git blame file-name

13.编辑gitignore文件

//忽略logs文件夹下的.log文件
logs/*.log

14.config

//配置某一个repo的email
#git config user.email xxx@ggg.com
//配置别名,将commit改成somethingnew
#git config alias.somethingnew commit

Git Real 2 from Code School


1.stash

//暂时保存文件更改,但不是commit
#git stash
//显示stash列表
#git stash list
//显示更加详细的stash列表信息
#git stash list --stat
//显示某个stash更详细的信息
#git stash show --patch stash@{2}
//切回某个stash
#git stash apply
//删除上一个stash
#git stash drop
//将unstaged changes加到最近的一个stash中
#git stash --keep-index
//将未被track的文件加入到stash中
#git stash --include-untracked
//给stash添加说明
#git stash save 'dfdfd'
//提交stash到一个新建的branch中
#git stash branch new-branch

2.config

// If you’re on a Windows machine, set it to true — this converts LF  
//endings into CRLF when you check out code:
#git config --global core.autocrlf ture
//You can tell Git to convert CRLF to LF on commit
#git config --global core.autocrlf input
//关于line endings的问题可以通过配置.gitattributes文件来完成,  
//更多可查看官方文档

参考资料

Git from the inside out