Tips & Tricks

Archive The Repository 存档

git archive master --format=zip --output=..//website-08-01-2021.zip
该命令将项目代码打包成.zip文件(没有.git目录, 没有版本控制信息, 只保留代码)
image.png
如果是unix系统打包成tar文件
git archive master —format=tar —output=..//website-08-01-2021.tar

Bundle the Repository

git bundle create ../repo.bundle master
将项目打包成一个.bundle文件, 但是保留了版本控制信息, 你可以克隆它

  1. cd ..
  2. git clone repo.bundle repo-copy -b master
  3. cd repo-copy
  4. git log
  5. cd ../my-git-repo

Ignore a File

将要排除的文件名写入到.gitignore文件里
可以使用*通配符排除特定后缀的文件

  1. *.o
  2. *.out
  3. *.exe

Stash(隐藏) Uncommitted Changes

使用命令git stash将未提交的更改隐藏起来
使用命令git stash apply恢复stashed的内容到当前分支
如果你发现你在错误的分支上修改了代码, 可以先git stash, 切换到正确的分支后, 再git stash apply

View Diffs Between Commits

git diff HEAD~2..HEAD~1查看两个commit之间的不同
git diff master..john/pink-page 查看两个分支之间的不同
git diff 查看工作区未添加到staging area 的更改
git diff --cached查看已经添加到staging area 的更改

Reset and Checkout Files

git reset HEAD blue.html 把staging area的文件恢复成指定commit里的一样, 相当于变成unstaged
git checkout HEAD blue.html 把工作区的文件恢复成指定commit里的一样, 相当于撤销更改

Aliases and Other Configurations

  1. git config --global alias.co checkout
  2. git config --global alias.ci commit
  3. git config --global alias.br branch

全局配置文件在~/.gitconfig, 项目配置文件在.git/config
如果换了电脑,把配置文件拷贝出来替换掉就行了

补充

**git rm --cached <file>**
从项目中删除文件,且不再track, 但是该文件仍保留在工作区

**git pull origin/master** git pull 是git fetch和git merge的合并
This fetches the origin’s master branch, and then merges it into the currentbranch in one step. You can also pass the —rebase option to use git rebase
instead of git merge.

Plumbing

blob objects are how Git stores our file data, tree objects combine blobs and other trees into a directory listing, then commit objects tie trees into a project history.

The index is Git’s term for the staged snapshot.

git cat-file <type> <object-id>
Display the specified object, where is one of commit , tree , blob , or tag .
git cat-file -t <object-id>
Output the type of the specified object.
git ls-tree <tree-id>
Display a pretty version of the specified tree object.
git gc
Perform a garbage collection on the object database.
git update-index [--add] <file>
Stage the specified file, using the optional —add flag to denote a new untracked file.
git write-tree
Generate a tree from the index and store it in the object database. Returns the ID of the new tree
object.
git commit-tree <tree-id> -p <parent-id>
Create a new commit object from the given tree object and parent commit. Returns the ID of the
new commit object.