git学习

资源

关于diff -u

diff -u old new
注意顺序,新与旧对比,-是少了什么, +是多了什么
也可以理解成-是旧,+是新
@@ -1,7 +1,7 @@
-,+意义同上
a,b表示从第a行开始的b行

划分版本原则

两个基本无联系的改动,应分两次git
一次性开到了三个错,应一次全改完再git
相邻两次git,不宜过久,不宜过短

git repository(仓库)

将有一定关联性的文件(改一个另外一个也和可能改这种)放一起
里面的文件会一起git

git log(日志,记录)

列出commit记录 (越靠上越新) (最新的commit会被标为HEAD)
一开始只能显示部分(长度限制)
上下箭头一行行移(网上的话只能到一开始长度限制的那个位置)
pageup pagedown可以大段大段移
按q退出log
--all会显示完整的图, 否则只能显示从当前checkout点往祖先边走能够到达的地方
--graph会画分支图,注意分支点不一定是commit点,只是图不好画。*才是commit点
--oneline会使commit显示更简便,一行一个
--stat会显示修改了哪些文件+了多少处,-了多少处
--grep xxx可以搜索messagel里的内容(不能是日期什么的)
git log folder/搜索该文件夹内的
git log -- file搜索该文件的
git log branch --搜索该分支的

git clone(复制)

git clone <url>

git config(配置)

git config --global color.ui auto获得彩色的 diff 输出
git config --global user.name "yourname"设置author
参考了1 2优化了一下log显示方式
可能有缺陷吧,暂时先这样,以后有问题再说
配置文件存放在~/.gitconfig

[alias]
    lg = log --graph --abbrev-commit --decorate --date=format:'%Y-%m-%d %H:%M:%S' --format=format:'%C(bold black)%h%C(reset) - %C(bold cyan)%ad%C(reset) %C(dim white)- %an%C(reset) %C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) ' -10 --all

git checkout

去到其他版本(这时HEAD会到其他地方

撤销:
1)部分。 git checkout -- file或dir
2) 完整。 git reset --hard HEAD

git init

在目录下输git init
生成.git
需要ls -a才能看到
init后没有进行commit
没有加入staging area
所有文件属于untracked files

手贱在不想建git的地方init了的话,删除目录下.git文件夹即可

git status

未跟踪过的文件untracked files
修改后未载入缓存changes no staged for commit
载入缓存而未commit显示changes to be commited

git add

git add <file>将文件载入缓存
如果修改后add后又对已经add的文件进行了修改
那么该文件会既出现在not staged for commit又出现在changes to be committed
因为add进去后,再修改原文件,是不会改变stage里的那个的

git diff(比较)

git diff比较显示方式同diff -u

working directory -> staging area -> repository
1)git diff ID1 ID2比较两个commit ID(这里ID不一定要完整,只打头几位能分辩即可)
2)git diff比较working directory和staging area
3)git diff --staged比较staging area和latest commit
4)git diff ID比较working directory和commit ID

git commit(提交)

git commit -m "message"右分号未打之前,可换行些message
message样式指南

git branch

git branch查看分支状态
git branch name新建分支
git checkout branchname移到某个分支的末尾(就像master标记,分支的标记再各分支的末尾)
git checkout -b name新建分支并移过去
git log --graph会显示分支图
git branch -D name删除分支(已merge则只删标签,未merge则删整个分支(真要这么做时先把head移开,否则会游离))
另外如果在一条链的中间节点上修改然后commit,他会出去一个假分支,你checkout到其它地方他就没了

git merge

git checkout branch1 + git merge <branch2> (<branch345...>) 可将branch1和其他分支合并,并延长branch1分支至合并点,其他branch名称保留原位
如果合并出错冲突git reset --merge

git push

github建空repository。clone那里获得地址

git remote add origin url
git push -u origin master

已有要修改则
git remote remove origin

修改commit message

git commit --amend修改最近一次commit。 ^X y enter保存

git不小心commit了大文件且commit了多次,而那个大文件没啥用可以删那种

git filter-branch --index-filter 'git rm --cached --ignore-unmatch <your-file-name>'
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git fsck --full --unreachable
git repack -A -d
git gc --aggressive --prune=now
git push --force origin master

git 在 vs中使用注意事项

设项目名为A
需要.gitignore
.vs/ 用户配置文件夹后面得加/记得
A/.vs用户配置(如上一次关闭时在哪里之类的
A/x64debug文件
这些文件都是随时可删不影响别人用vs打开的
但如果不删掉,git就push不了

posted @ 2019-06-13 11:07  _zwl  阅读(178)  评论(0编辑  收藏  举报