`

git使用

 
阅读更多
创建 本地repo
当远程服务器已有Git版本库,只需要在本地克隆一份
git clone url/project.git [foldername] (可省略,默认为project名)
或者,
当你本地创建了一个工作目录,你可以进入这个目录,使用'git init'命令进行初始化;Git以后就会对该目录下的文件进行版本控制,这时候如果你需要将它放到远程服务器上,可以在远程服务器上创建一个目录,并把可访问的URL记录下来,此时你就可以利用'git remote add'命令来增加一个远程服务器端(即起别名)
    git init
    git init --bare 无工作目录,服务器版本一般这样操作

A standard Git repository is different from a remote Git repository. A standard Git repository contains the working directory (single checkout of one version of the project) and the Git repository.
Remote repositories do not contain working copies of the files. They only contain repository files. To create such a repository, set the --bare flag.

为远程 repo 起别名
    git remote add origin url/project.git, 之后用 origin 操作远程repo, e.g. git push origin master

git pull remote-repo
git pull origin master

git add
git diff
git status

取消对已修改文件添加的index
    git reset HEAD filename

撤销对文件的修改(还未add to index)
    git checkout filename

对于新添加的文件进行 codereview:
1. 不是用 git add, Untracked files,表明未加入版本控制,git diff 无法检测出来,因此无法 codereview
2. 使用 git add, Changes to be committed,也无法使用 git diff,但稍作修改,即可codereview

codereview时,在git add filename之前,对其审查


修改原文件后,
1. git diff,查看修改内容, 进行 codereview
2. git checkout filenname,还未加index,撤销修改
3. git add filename,将修改的文件加入索引
4. git reset HEAD filename, 取消索引

5. git status
   Changes to be committed  文件已加 index
   Changes not staged for commit   文件未加 index
   Untracked files   未加入本地 repo

6. git commit -a -m ""  将修改内容提交至本地 repo
   或者是,每次修改后, git add filename,修改添加至索引
                   git commit -m ""

7. git push origin master 将本地 repo 提交到远程 repo,即 origin de master分支中
      git push <remote-name> <local branch>:<remote branch>
      本地分支推送更新到远程仓库的指定分支

      git pull <remote-name> <local branch>:<remote branch>


昨晚出现的问题
   在 git commit -m "" 提交时, message写错,一直找不到修改 message的命令,使用了 git revert commit-id, 撤销刚刚提交的内容,回滚到上一次 commit,本次所有的修改全部删除,同时,本次revert操作作为一次commit提交,记入log,可通过 git log 查询。 所以可以再执行一次 revert 取消本次 revert(但具体后面的效果,发现修改还是没有恢复,之后就没管了)

  git reflog, 查看所有记录,包括 commit, reset, revert

  git reset [--mode] commit-id/HEAD
  1.  --hard:重设(reset) index和working directory,自从<commit>以来在working directory中的任何改变都被丢弃,并把HEAD指向<commit>,不可恢复,取消了commit ,取消了add,取消源文件修改
  2. --soft:index和working directory中的内容不作任何改变,仅仅把HEAD指向<commit>。这个模式的效果是,执行完毕后,自从<commit>以来的所有改变都会显示在git status的"Changes to be committed"中。 取消了commit
  3. --mixed:仅reset index,但是不reset working directory。这个模式是默认模式。这个模式的效果是,working directory中文件的修改都会被保留,不会丢弃,但是也不会被标记成"Changes to be committed",但是会打出什么还未被更新的报告。 取消了commit ,取消了add

   之后,我又连续操作两次 reset --soft,中间的一些修改全部丢失,不得不使用 git pull origin master。 遇到, pull is not possible, unmerged files. 手动解决冲突,add, commmit

git reset 是把HEAD向后移动了一下,而git revert是HEAD继续前进,只是新的commit的内容和要revert的内容正好相反,能够抵消要被revert的内容。
revert 只撤销某一个commit-id,而 reset 将自 commit-id到最近的所有commit撤销
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics