Git入门及其workflow

Git的基本使用方法:

PART1  Basic

1  >> git init

用来初始化。创建一个git仓库来管理文件。注意这个命令可以在不同文件夹下创建,但是在子文件或者父文件下提交的规则会有所不同。

在父文件中提交commit之前要在子文件提交commit

 

2  >> git statusus

用来查看当前状态,就像linux里面用>> ls 一样

 

3  >> git add

用来添加文件,添加单个文件就是

>> git add <filename>

当然也可以添加多个文件,只需要这些文件之间用空格隔开就可以了。

>> git add <filename1> <filename2>

用来添加所有改变的文件就用

>> git add .

 

4  >> git diff

首先要用>> git add 追踪了某个文件,然后再用git diff可以显示出不同的地方。

还可以用

>> git diff <file1> <file2>

来进行对比,其中file可以是origin, master, branch.

也可以是两个commit的哈希。

 

5  >> git commit

在添加文件后用来提交,注意这里提交后相当于改变了本地的存储。和提交到远程仓库比如github是不一样的。

一般的用法是

>> git commit -m “comment here” 

 

6  >> git log

最后是用git log查看自己提交的一些详情,而且还会显示每次提交的一个哈希值,通过这个值可以进行回退。

 

7  >> git show HEAD

HEAD commit一般来说是最近一次提交的commit,通过这条命令可以显示最近一次提交。

 

8  >> git checkout HEAD <filename>

Checkout结合HEAD(HEAD是最近一次提交)很有用,比如在一次迭代测试中,感觉自己哪里错了或者删除错了哪次文件,就可以回退到上一次的状态。当然,我们更喜欢回退到某一个具体的状态,这样就需要将HEAD变为具体一次commit的哈希的前六位。注意恢复某一次的文件并不会把那一次commit没有的文件删掉,但是会恢复那一次commit中存在的文件。

注意这里<filename>可以用  . 来代替,这样就可以恢复那一次的所有文件。

 

 

PART2 Teamwork

>> git clone <remote_location> <clone_name>

<remote_location>可以是github给出的web网址,也可以是一个文件的路径。也就是说可以在本地操作git复制项目。

 

>> git remote -v

这也是展示信息,显示远程服务器的信息。这里的v是指verbose,详细版本的意思。

注意这要在clone了来自远程服务器或者某个路径的时候用

 

>> git fetch

这也是在有remote信息下进行,这个操作会把远程服务器更新的文件取到本地,但是不会马上改变本地文件,而是会在一个origin/master的branch。注意当git status提示你behind origin/master就说明远程服务器有更新。

 

如果再fetch完之后,想要把本地的文件替换成远程服务器更新后的内容,就需要用到merge了。

>> git merge origin/master

意思就是把origin/master这个branch融合到主branch中。

注意在merge后不可回退了,整个git log就变成了远程服务器的git log了。

 

>> git branch <branch_name>

显示branch,知道了不同branch的名字,可以用

>> git checkout <branch_name><branch_name>

来切换branch,切换不同branch提交之后,再切换会master会改变文件夹的内容。

注意如果不提交commit,再切换回master可能会出现错误。所以一次只操作一次,如果修改了branch,记得一定要提交。

 

>> git push <origin> <your_branch_name>

把你创建的分支融合到远程地址(origin)。注意如果你把你的master而不是分支push上去,在github中,如果你是账号的使用者,会直接修改master。也就是是说除非你是那个账号的使用者,否则你是不能直接修改master的。

 

Git workflow

Now that you've merged  origin/master  into your local  master  branch, you're ready to contribute some work of your own. The workflow for Git collaborations typically follows this order:

 

  1. Fetch and merge changes from the remote
  2. Create a branch to work on a new project feature
  3. Develop the feature on your branch and commit your work
  4. Fetch and merge from the remote again (in case new commits were made while you were working)
  5. Push your branch up to the remote for review

 

Steps 1 and 4 are a safeguard against merge conflicts, which occur when two branches contain file changes that cannot be merged with the git merge command. Step 5 involves git push, a command you will learn in the next exercise

 

 

注意git里面的origin是fetch取过来的,是可以离线存在的。

意思是说离线状态可以使用

>> git diff master origin

或者说git status提示超前或落后的origin离线保存下来的。

 

 

 

 

猜你喜欢

转载自blog.csdn.net/shenpanzhimao/article/details/81160057