Learn Git

Git Book读书笔记: https://git-scm.com/book/en/v2

Get git repository

两种方法:

1 从本地目录创建

1) cd e:/mynotes

2) git init

3) git add *.c

4) git add LICENSE

5) git commit -m ‘initial project version’

删除文件用 git rm。 从git版本库中删除文件但不在磁盘上删除:

1
2
git rm -r public/ --cached
git commit -m "remove public folder from git"

清除目录下未跟踪的目录及文件:

1
git clean -df

2 从远程clone一个仓库到本地

Others

git add is a multipurpose command. git add是一个多功能的命令:

  • to begin tracking new files

  • to stage files

  • to do other things like marking merge-conflicted files as resolved

It may be helpful to think of it more as “add precisely this content to
the next commit” rather than “add this file to the project”.

忽略某些文件的方法,添加.gitignore文件:

https://github.com/github/gitignore

 # ignore all .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, 
#not subdir/TODO
/TODO

# ignore all files in any directory named build
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory 
# and any of its subdirectories
doc/**/*.pdf

git log

1
2
3
4
5
6
7
8
git log
git log -p
git log -p -2
git log --stat
git log --pretty=oneline
git log --pretty=format:\"
git log --since=2.weeks
git log S function\_name

Undo

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend

You end up with a single commit — the second commit replaces the
results of the first.

remote

1
2
git remote
git remote -v

Adding Remote Repositories

$ git remote
origin
$ git remote add pb https://github.com/paulboone/ticgit
$ git remote -v
origin  https://github.com/schacon/ticgit (fetch)
origin  https://github.com/schacon/ticgit (push)
pb  https://github.com/paulboone/ticgit (fetch)
pb  https://github.com/paulboone/ticgit (push)

Fetching and Pulling from Your Remotes

git fetch remote

The command goes out to that remote project and pulls down all the data
from that remote project that you don’t have yet.\
It’s important to note that the git fetch command only downloads the
data to your local repository: it doesn’t automatically merge it with
any of your work or modify what you’re currently working on. You have to
merge it manually into your work when you’re ready.

If your current branch is set up to track a remote branch (see the next
section and Git Branching for more information), you can use the git
pull
command to automatically fetch and then merge that remote branch
into your current branch.

Pushing to Your Remotes

  • git push remote branch

  • git push origin master

Inspecting a Remote

  • git remote show remote

  • git remote show origin

$ git remote show origin
* remote origin
Fetch URL: https://github.com/schacon/ticgit
Push  URL: https://github.com/schacon/ticgit
HEAD branch: master
Remote branches:
master                               tracked
dev-branch                           tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)

Renaming and Removing Remotes

You can run git remote rename to change a remote’s shortname. For
instance, if you want to rename pb to paul, you can do so with git
remote rename:

$ git remote rename pb paul
$ git remote
origin
paul

remove a remote:

  • git remote remove paul

  • git remote rm

Listing your tags(显示tag)

1
2
3
git tag
git tag -l
git tag -list

Creating tags

Git 支持两种 tags: lightweight and annotated.

A lightweight tag is very much like a branch that doesn’t change — it’s just a pointer to a specific commit.

Annotated tags, however, are stored as full objects in the Git database. 一般使用annotated tag.

  • 创建Annotated tag:

    1
    2
    3
    git tag -a v1.4 -m "my version 1.4"
    //查看annotated tag:
    git show v1.4
  • 创建Lightweight Tags

    1
    2
    git tag v1.4-lw
    git show v1.4-lw

以上是对当上次commit进行打tag。


若要对之前某次commit打tag:

首先假设如下是之前的提交历史记录:

1
2
3
4
5
6
7
8
9
10
11
$ git log --pretty=oneline
15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support
0d52aaab4479697da7686c15f77a3d64d9165190 one more thing
6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment'
0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc added a commit function
4682c3261057305bdd616e23b64b0857d832627b added a todo file
166ae0c4d3f420721acbb115cc33848dfcc2121a started write support
9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
8a5cbc430f1a9c3d00faaeffd07798508422908a updated readme

为了在message为”updated rakefile”上打tag,只需要在git tag -a v1.2 后面加上校验和的(前面的)一部分即可。如下:

1
git tag -a v1.2 9fceb02

把tag推送到remote server

默认情况下,git push不会把tag推送到服务器,推送方法:git push origin

1
2
3
git push origin v1.5
//或者
git push origin --tags // 把所有tag推送到remote server

Delete tags 删除tags

删除本地tag:git tag -d <tagname>

1
git tag -d v1.4-lw

删除remote server上的tags:

1
2
3
git push origin --delete <tagname>
// 或者
git push origin :refs/tags/v1.4-lw

查看远程所有的分支和tag:

1
git ls-remote

Git alias

1
2
3
4
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

也可用于自定义命令:

1
$ git config --global alias.unstage 'reset HEAD --' //添加unstage命令

所以以下两条命令就等价了:

1
2
$ git unstage fileA
$ git reset HEAD -- fileA

另外,可以添加一个last命令,查看上一次的commit情况:

1
$ git config --global alias.last 'log -1 HEAD'

之后,想查看上次的commit情况,只需输入:git last

Creating a New Branch

1
git branch newbranchname

HEAD是一个特殊的指针,指向当前工作的branch. 可以用git log可查看其指向:

1
git log --oneline --decorate

Switching Branches

1
git checkout abranchname // HEAD指针会指向abranchname分支