git命令学习
git帮助
git help [command]
本地创建repository, 推送到远端
git init
git remote add [remote repository name] [remote repository url]
git remote add origin https://github.com/BlockLink/blocklink_crosschain_privatekey.gitgit push -u [remote repository name] [remote branch name]
git push -u origin master
分支相关
跳转分支
git checkout <branch>
> 跳转到
创建本地分支并且推送到远端
创建本地分支
git checkout -b [branch_name]
推送到远端
git push <remote_repository_path> <branch_name>[:<remote_branch_name>]
设置跟踪分支
git branch -u <remote_repository_path/remote_branch_name>
设置跟踪分支
git checkout [-b [branch]] [--track] <remotename>/<branch>
> 用于对未创建的分支建立跟踪分支; 并且跳转到
git branch -u <remotename>/<branch>
or git branch --set-upstream-to=origin/<branch> <localbranch>
> 为当前所在的分支创建跟踪分支
一般如果你直接通过clone命令拉取一个仓库, 则git会自动设置一个本地master到origin/master的跟踪 分支. * master 3cf359f [origin/master] Site updated: 2015-12-18 11:55:19
查看本地的所有跟踪分支情况
git branch -vv
git branch [--list]
#列出本地的分支 git branch [--list] -r
#列出repository上的分支情况
删除本地分支或远程分支
本地分支
git branch -D/--delete [branchname]
远程分支
git push origin --delete [branchname]
orgit push <remote_name> :<branch_name>
> 基本上这个命令做的只是从服务器上移除这个指针。 Git 服务器通常会保留数据一段时间直到垃圾回收运行,所以如果不小心删除掉了,通常是很容易恢复的
查看/设置远程repository
git config --list
#可以查看到当前本地对应的远程仓库git remote add [remote_repository_name] [remote_repository_path]
设置tag
git tag [tagname]
创建一个taggit tag -d [tagname]
删除一个taggit checkout [tagname]
更新到tag的状态
submodule相关
add submodule to a repository
git submodule add [-b branch_name] [git repository url] [the path to put the submodule]
update submodule
git submodule update --init --recursive
第一次init并更新 git submodule update [--remote]
更新submodule git submodule set-branch [-b branch_name] submoule_path
更新submoule_path上的submodule的branch 或者 直接更改.gitmodules添加branch配置 1
2
3
4
5
6
7
8
9
10[submodule "src/xtopcom/xvm"]
path = src/xtopcom/xvm
url = [email protected]:telosprotocol/xvm.git
branch = dev/featute_new_contract
[submodule "src/xtopcom/xdepends"]
path = src/xtopcom/xdepends
url = [email protected]:telosprotocol/xdepends.git
[submodule "src/xtopcom/xbase"]
path = src/xtopcom/xbase
url = [email protected]:telosprotocol/xbase.git
rm submodule
- 删除.gitmodules中该submodule相关的行
- 删除.git/config中该submodule相关的行
- 删除该submodule的路径
git rm --cached [the path to put the submodule]
can not put a trailing slash as the command will fail.
历史或状态
查看历史
git log --graph --pretty=oneline --abbrev-commit
git log --graph --pretty=format:"%h%x09%an%x09%ad%x09%s" --abbrev-commit
%h = abbreviated commit hash %x09 = tab (character for code 9) %an = author name %ad = author date (format respects --date= option) %s = subject
查看commit id
git rev-parse HEAD
修改commit信息或作者信息
最近的一条commit记录
git commit --amend [--reset-author]
任意的某条记录
git rebase -i HEAD~N
--N表示倒数第几条commit信息git commit --amend [--reset-author]
git rebase --continue
删除untracked files
git clean -n
--remove untracked files from the working tree
更新gitignore并生效(删除cached index, 重新建立cached index)
1 | git rm -rf --cached . |
git clone big repos
第一种增加buffer
git config --global http.postBuffer 10000000000
第二种
1
2
3
4
5
6
7
8
9
10
11# (optional) turn off compression
git config --local/--global core.compression 0
# do a partial clone to truncate the info
git clone --depth 1 <reop_url>
# go to the rep dir & retrieve the rest of the clone
git fetch --unshallow
# now do regular pull
git pull --all
git stash
1 | # stash changes that not added to the index |
设置git配置
设置用户名(默认为local)
git config [--global/--local/--system] user.name "your user name"
设置密码(现在windows中credential.helper默认为manager, 是以前wincred的替代品)
git config [--global/--local] credential.helper store
git config [--global/--local] credential.helper cache
git处理文件中line ending
git config core.autocrlf
会输出三种值: true, false, input
- true: turn all CRLF line ending to LF
- false: no line ending conversation will performed
设置成你需要的值 git config core.autocrlf [true/false/input]
回滚
撤销本地的commit记录
- git reset [--mixed|--soft|--hard] HEAD[num]|commit-id (在windows系统,^在linux系统)(back num commits)
- Mixed - the default strategy. When this option is selected, the index is reset while the working tree is not, which means that changed files are preserved but not marked for commit. You are presented with a report of what has not been updated.
- Soft - when this option is selected, the index and the working tree are not affected, only the HEAD pointer is moved to the specified commit. Your current state with any changes remains different from the commit you are switching to. All the changes are "staged" for committing.
- Hard - when this option is selected, both the working directory and the index are changed to the specified commit.
撤销push后的commit记录
- 撤销本地的commit记录
git push --force
强制提交
设置代理
针对所有的URL
1 | git config --global http.proxy socks5://127.0.0.1:1080 |
针对某个url
1 | git config --global http.https://github.com.proxy socks5://127.0.0.1:1080 |