git命令小记

作为一个程序猿,不会用git是不是显得有点low?答案是肯定的。
为了不那么low,我决定开始学习git,在这里做笔记

git初始化的几个命令

1
2
3
4
5
6
7
8
9
10
11
12
cd repo
#进入要操作的文件夹
git init
#初始化GIT环境
git add .
#添加当前目录的文件到git
git commit -m "message"
#提交文件
git remote add origin git@github.com:user/repo.git
#关联远程仓库
git push origin master
#将本地代码推送到远程仓库

廖雪峰的git教程
七个你无法忽视的Git使用技巧

如何创建一个新的空的分支

方法1

  • 1.首先,从仓库中clone一份代码出来
1
2
git clone git@git.com:user/repo.git
cd repo
  • 2.创建一个新的分支并切换过去
1
2
--orphan 标识这个分支是一个独立分支,不包含任何提交记录
git checkout --orphan newBranch
  • 3.删除原有数据
1
2
git rm -rf .
rm .gitignore
  • 4.提交新的分支
1
2
3
4
5
echo "this is a new branch" >> README
git add README
#没有文件不能提交
git commit -a -m "newbranch first commit"
git push origin newbranch
  • 5.一个全新的 没有任何东西的分支,到这里就算完工了。

方法2

1
2
3
4
5
6
7
git symbolic-ref HEAD refs/heads/newbranch
rm .git/index
git clean -fdx
<do work>
git add your files
git commit -m 'Initial commit'
git push origin newbranch

git删除远程分支

  • 1.查看所有远程分支

    git branch -a

  • 2.删除

    git branch -r -d origin/branch_name_todel
    git push origin :branch_name_todel

git从远程分支还原

  • 1.查看提交日志
1
git log
  • 2.还原到指定版本
1
2
3
4
5
6
7
8
9
##还原有五种选项 --mixed | --soft | --hard | --merge | --keep 
##可通过以下命令查看
git reset --hard

##重置 HEAD、索引和工作区
git reset --hard [ver-hash]

##将远程仓库中的版本,也回退到这次提交
git push -f

git remote 远程仓库

详情请戳

1
2
3
4
5
6
7
8
9
10
11
12
## 查看当前的远程库
git remote

##查看远程库的URL
git remote -v | --verbose

##添加(关联)远程仓库
git remote add [name] [url]
##查看远程分支信息,可以看到不同步的地方
git remote show origin
##清除远程已经被删除,但是本地还保留的的远程分支
git remote prune origin

git branch

git branch用法总结

1
2
## 从head指向的提交创建分支, 并切换过去
git checkout [head] -b [branchname]

github fork

官方文档
github 的fork按钮,用于拷贝一份代码到自己的仓库中
使用git clone git@github.com:lgfng/my-fork.git 将代码拷贝到本地之后,我们会有一个默认的远程仓库 origin。

1
2
3
4
5
#新增一个remote 来跟踪原始库。
git remote add upstream https://github.com/origin_owner/origin_remote.git
#拉取原始代码的更新
git fetch upstream
git merge upstream/master

如果希望将自己的代码贡献到原始库中, 可以参考pull_request

github 仓库重命名

在github/要重命名的项目主页中,找到setting, 然后点进去,可以修改名称,然后点rename重命名.

git 恢复本地被修改的文件

  • 恢复到最后一次提交的改动
    git checkout [filename]
  • 恢复指定文件到指定提交
    git checkout [commit id] [filename]
  • 如果已经add到暂存队列中, 则以上命令不生效,需要先让这个文件取消暂存
    git reset HEAD [filename]
    然后在执行第一条名

git 命令别名

设置一个别名,用于取消文件暂存状态
git config --global alias.unstage 'reset HEAD --'
使用方法:
git unstage [filename]

git commit

clone github仓库的某个子目录

[GitHub克隆源码仓库中的单个子目录](https://blog.kuoruan.com/64.html)
github支持svn checkout, 所以可以使用 `svn co` 来获取某个子目录, 而不用拉取整个仓库。
1
2
3
4
#获取master分支代码
svn co https://github.com/[user]/[respo]/trunk/[***]
#获取branch分支代码
svn co https://github.com/[user]/[respo]/branches/[branch_name]/[***]
  • -v

git submodule

参考链接

1
2
3
4
5
6
7
8
9
10
# 添加
git submodule add [repos path] [rela path]
# 删除
删除 .gitmodules中对应配置,
然后 `git rm --cached` 删除文件.
# 初始化
git clone git@github.com:jjz/pod-project.git --recursive
git submodule update --init --recursive
# 更新
git submodule foreach git pull

git rm [file]

1
2
3
4
#将文件从git中删除, 物理文件也会被删掉
git rm [filename]
#将文件从git中删除, 物理文件会被保留, 只删除git 索引
git rm --cached [filename]

git tag

1
2
3
4
5
6
7
8
git tag -a -m "message" tag_name
git tag show tag_name
//推送到远端
git push origin tag_name
git push origin --tags
//删除
git tag -d tag_name
git push origin :refs/tags/tag_name

好用的git alias

1
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

git lfs

lfs 用于管理大文件的git, 每次对大文件修改时,版本库里就会多上对应的大小。会使版本库急剧增长。
lfs 可以将这些文件的管理放在服务器端, 客户端只拉下来需要使用的部分。从而使客户端仓库变得更小。但服务端没有什么变化。

1
2
3
4
5
6
7
8
9
# mac
brew install git-lfs

git lfs install

# 配置大文件规则
git lfs track "*.png"
# 提交配置文件
git add .gitattributes

字符集

git 中文文件名乱码

git config –global core.quotepath false 让git不对中文进行转义,显示就会正常

常用ignore 文件汇总

unity ignore

命令参数

git log –name-status 每次修改的文件列表, 显示状态
git log –name-only 每次修改的文件列表
git log –stat 每次修改的文件列表, 及文件修改的统计
git whatchanged 每次修改的文件列表
git whatchanged –stat 每次修改的文件列表, 及文件修改的统计
git show 显示最后一次的文件改变的具体内容
git show -5 显示最后 5 次的文件改变的具体内容
git show commitid 显示某个 commitid 改变的具体内容


git命令小记
https://leafflow.top/2015/07/22/other/git命令小记-2015-07-22/
作者
vollen
发布于
2015年7月22日
许可协议