git命令学习

git帮助

git help [command]

本地创建repository, 推送到远端

  1. git init
  2. git remote add [remote repository name] [remote repository url] git remote add origin https://github.com/BlockLink/blocklink_crosschain_privatekey.git

  3. git push -u [remote repository name] [remote branch name] git push -u origin master

分支相关

跳转分支

git checkout <branch> > 跳转到分支:将HEAD指向, 并 更新index和文件.

创建本地分支并且推送到远端

  1. 创建本地分支 git checkout -b [branch_name]

  2. 推送到远端 git push <remote_repository_path> <branch_name>[:<remote_branch_name>]

  3. 设置跟踪分支 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上的分支情况

删除本地分支或远程分支

  1. 本地分支 git branch -D/--delete [branchname]

  2. 远程分支 git push origin --delete [branchname] or git push <remote_name> :<branch_name> > 基本上这个命令做的只是从服务器上移除这个指针。 Git 服务器通常会保留数据一段时间直到垃圾回收运行,所以如果不小心删除掉了,通常是很容易恢复的

查看/设置远程repository

  1. git config --list #可以查看到当前本地对应的远程仓库
  2. git remote add [remote_repository_name] [remote_repository_path]

设置tag

  1. git tag [tagname] 创建一个tag
  2. git tag -d [tagname] 删除一个tag
  3. git 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

  1. 删除.gitmodules中该submodule相关的行
  2. 删除.git/config中该submodule相关的行
  3. 删除该submodule的路径 git rm --cached [the path to put the submodule] can not put a trailing slash as the command will fail.

历史或状态

查看历史

  1. git log --graph --pretty=oneline --abbrev-commit
  2. 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

  1. git rev-parse HEAD

git处理文件中line ending

git config core.autocrlf 会输出三种值: true, false, input

  1. true: turn all CRLF line ending to LF
  2. false: no line ending conversation will performed

设置成你需要的值 git config core.autocrlf [true/false/input]

设置git配置

  1. 设置用户名(默认为local) git config [--global/--local/--system] user.name "your user name"

  2. 设置密码(现在windows中credential.helper默认为manager, 是以前wincred的替代品) git config [--global/--local] credential.helper store git config [--global/--local] credential.helper cache

回滚

撤销本地的commit记录

  1. 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记录

  1. 撤销本地的commit记录
  2. git push --force 强制提交

设置代理

  1. 针对所有的URL

    1
    2
    3
    4
    5
    6
    7
    8
    git config --global http.proxy  socks5://127.0.0.1:1080
    #git config --global https.proxy socks5://127.0.0.1:1080


    git config --global --unset http.proxy
    #git config --global --unset https.proxy


  2. 针对某个url

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    git config --global http.https://github.com.proxy  socks5://127.0.0.1:1080
    #git config --global https.https://github.com.proxy socks5://127.0.0.1:1080


    git config --global --unset http.proxy
    git config --global --unset http.https://github.com.proxy
    #git config --global --unset https.proxy

    git config --global --get http.proxy
    #git config --global --get https.proxy

修改commit信息或作者信息

  1. 最近的一条commit记录 git commit --amend [--reset-author]

  2. 任意的某条记录 git rebase -i HEAD~N --N表示倒数第几条commit信息 git commit --amend [--reset-author] git rebase --continue

删除untracked files

  1. git clean -n --remove untracked files from the working tree

更新gitignore并生效(删除cached index, 重新建立cached index)

1
2
3
git rm -rf --cached .
git add .
git commit -m "you commid info to udpate new index"
-------------本文结束感谢您的阅读-------------