git-note

Ignore Some Files

1
2
3
4
vim .gitignore
git rm -r --cached .
git add .
git commit -m "ignore some files"

Merge

If you are in dev branch.

  • merge local master

    • git merge master
  • git pull origin master

    • this command will merge dev with origin/master automatically!
    • So it is very importanct to look out which branch your are in when pulling!
  • git push origin master

    • this command will perform not like pull, it won’t push local dev to origin/master. Insteadly, it will push local master to origin/master even if you are on dev branch now.

git remote

  • 很多时候, git clone 比 git remote add 方便
    • 以下为其使用的示例
1
git clone https://github.com/DaiDongyang/jfinal.git

gitclone

  • 删除远程分支
1
git remote rm origin
  • 有时候碰到冲突时非常烦,一怒之下会强行push。
1
2
git push -f <remote> <branch>
git push --force <remote> <branch>

git reset

  • 以下说明及代码来自这里,有些我并没有验证。

    • git reset [–-hard|soft|mixed|merge|keep] [commit或HEAD],根据–-soft –-mixed –-hard,会对working directory、index、HEAD进行重置。
    • A). –-hard:除了“Untracked files”,其他变更都被重置。即:重设index和working directory,自从commit以来在working directory和index中的任何改变都被丢弃,并把HEAD指向commit。
    • B). –-soft:这个模式的效果是,执行完毕后,自从commit以来的所有改变都会显示在git status的”Changes to be committed”中,当然,还有“Untracked files”。即:恢复commit以来的index和working directory内容。即:not commited
    • C). –-mixed:这个模式是默认模式。这个模式的效果是,自从commit以来的修改都会被保留,但会被标记成”Changes not staged for commit”。即:仅重置index,文件修改被转移到working directory中。即:not added

    • git reset HEAD file…会将git index中file内容删除,重新放回working directory中,即未被add的状态。

    • 一个示例:假如你想要丢弃你所有的本地改动与提交,可以到服务器上获取最新的版本并将你本地主分支指向到它。

1
2
git fetch origin
git reset –-hard origin/master

git commit

  • 有时候想改上一次commit的内容(我经常会遇到这种问题),用 git commit –amend, 然后可以用vim等编辑器修改上次commit内容。参考于这里

GitHub pull request

  1. Child-reposity is forked from parent-reposity.
  2. Commit and push in child-reposity.
  3. Create pull request in child-reposity(on github webpage).
  4. Auto merge, or solve conflict and merge.

Solve conflict:

  1. Create a new branch in parent-reposity, and pull from child-reposity to the new branch in parent-reposity

    1
    2
    git checkout -b child-master master
    git pull https://github.com/DaiDongyang/TestPullRequest.git master
  1. The second command may be confused, the “master” means a branch in https://github.com/DaiDongyang/TestPullRequest.git. According to git document, the usage of git pull is : git pull [options] [<repository> [<refspec>…​]]. git pull command does two things: git fetch and merge.
  2. Solve conflict
  3. Merge and commit in parent-reposity.

    1
    2
    3
    git checkout master
    git merge --no-ff DaiDongyang-master
    git push origin master