git command

Table of Contents

1 git branch and merge

git branch branchname
git checkout branchname
git push -u origin branchname
#do something
git merge master
#merge into master
git checkout master
git merge branchname
#delete the branch
git branch -d branchname

#other fetch
git fetch origin
git checkout --track origin/branchname

2 merge conflict resolution

$ git merge experiment
Auto-merging lib/simplegit.rb
CONFLICT (content): Merge conflict in lib/simplegit.rb
Automatic merge failed; fix conflicts and then commit the result.

#When you’re done fixing it, all you have to do is run ‘git add’ on the file to re-stage it, which marks it as resolved. Then commit the merge.
$ git add lib/simplegit.rb
$ git commit
[master]: created 6d52a27: "Merge branch 'experiment'"

3 tag

git tag 1.0.0 1b2e1d63ff

使用如下命令获取提交 ID:
git log

4 替换本地改动

使用如下命令替换掉本地改动:
git checkout -- <filename>
#此命令会使用 HEAD 中的最新内容替换掉你的工作目录中的文件。已添加到缓
#存区的改动,以及新文件,都不受影响。


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

git fetch origin
git reset --hard origin/master

5 Creating Project Pages manually

$ git checkout --orphan gh-pages
# Creates our branch, without any parents (it's an orphan!)
# Switched to a new branch 'gh-pages'

git rm -rf .
# Remove all files from the old working tree

$ echo "My GitHub Page" > index.html
$ git add .
$ git commit -a -m "First pages commit"
$ git push origin gh-pages

#To unpublish a project page, delete the remote gh-pages branch:
git push origin --delete gh-pages

6 Setting up a custom domain with Pages

echo 'example.com' >>  CNAME

6.1 Top-level domain (TLD)

For a TLD like example.com, you should use an A record pointing to 204.232.175.78:1

在Linux终端下检查:

dig example.com +nostats +nocomments +nocmd
# Look up DNS record for example.com

# ;example.com.                    IN      A
# example.com.             3259    IN      A       204.232.175.78

6.2 Subdomains

When working with a subdomain it is best to set a CNAME record pointing to your user pages subdomain.

在Linux终端下检查:

 dig www.example.com +nostats +nocomments +nocmd
# ;www.example.com.                 IN      A
# www.example.com.          3592    IN      CNAME   username.github.com.
# username.github.com.      43192   IN      A       204.232.175.78

7 public and private.

Consider how a common Git workflow falls apart.

Create a branch off Master, do work, and merge it back into Master when you’re done

Most of the time this behaves as you expect because Master changed since you branched. Then one day you merge a feature branch into Master, but Master hasn’t diverged. Instead of creating a merge commit, Git points Master to the latest commit on the feature branch, or “fast forwards.”Picture 2

So you add a new rule: “When you merge in your feature branch, use –no-ff to force a new commit.”

7.1 Short lived work

git checkout -b private_feature_branch
touch file1.txt
git add file1.txt
git commit -am "WIP"

When I’m done, instead of a vanilla git merge, I’ll run:

git checkout master
git merge --squash private_feature_branch
git commit -v

Then I spend a minute writing a detailed commit message.

7.2 Larger work

If my checkpoint commits followed a logical progression, I can use rebase’s Interactive Mode.

Interactive mode is powerful. You can use it to edit an old commits, split them up, reorder them, and in this case squash a few.

On my feature branch:

git rebase --interactive master

By default, each commit uses “pick,” which doesn’t modify the commit.

pick ccd6e62 Work on back button
pick 1c83feb Bug fixes
pick f9d0c33 Start work on toolbar

I change the operation to “squash,” which squashes the second commit into the first.

pick ccd6e62 Work on back button
squash 1c83feb Bug fixes
pick f9d0c33 Start work on toolbar

When I save and close, a new editor prompts me for the commit message of the combined commit, and then I’m done.

7.3 Declaring Branch Bankruptcy

It’s easiest to grab the raw diff create a clean branch.

git checkout master
git checkout -b cleaned_up_branch
git merge --squash private_feature_branch
git reset

7.4 The intended workflow is:

  1. Create a private branch off a public branch.
  2. Regularly commit your work to this private branch.
  3. Once your code is perfect, clean up its history.
  4. Merge the cleaned-up branch back into the public branch.

8 staging

9 other

内建的图形化 git: gitk 彩色的 git 输出: git config color.ui true 显示历史记录时,只显示一行注释信息: git config format.pretty oneline 交互地添加文件至缓存区: git add -i

Footnotes:

Author: Shi Shougang

Created: 2015-03-05 Thu 23:19

Emacs 24.3.1 (Org mode 8.2.10)

Validate