Git命令

打印 上一主题 下一主题

主题 870|帖子 870|积分 2610



git config

全局设置用户名、密码、邮箱需要用 --global参数
不加--global可以某个git堆栈单独设置用户名、密码、邮箱
  1. # 1.查看git所有的配置信息
  2. git config --list
  3. # 2.查看git用户名、密码、邮箱的配置
  4. git config user.name
  5. git config user.password
  6. git config user.email
  7. # 3.设置git用户名、密码、邮箱的配置
  8. git config user.name "myname"
  9. git config user.password "123456"
  10. git config user.email "1234321@qq.com"
  11. # 3.设置git用户名、密码、邮箱的配置(全局配置)
  12. git config --global user.name 用户名
  13. git config --global user.name littleCat
  14. git config --global user.password 密码
  15. git config --global user.password abc123
  16. git config --global user.password 邮箱
  17. git config --global user.email "1234321@qq.com"
  18. # 4.修改git用户名、密码、邮箱的配置
  19. # 覆盖法
  20. git config user.name "xiaoming"
  21. git config --global user.name "xiaobai"   #修改全局配置
  22. # 替换法
  23. git config --global --replace-all user.name "xiaohong"
  24. # 5.删除git用户名、密码、邮箱的配置
  25. git config --global --unset user.name "xiaoming"
  26. git config --global --unset user.email "1234321@qq.com"
  27. git config --global --unset user.password "abc123"
复制代码
git init

初始化一个git堆栈,是在本地新建了一个git堆栈
  1. git init #在当前目录下初始化一个git仓库
  2. git init <file_directory> #在当前目录下新建file_directory目录,然后在该目录下初始化一个git仓库
复制代码
git clone

克隆一个git堆栈,是从远程堆栈克隆到本地的git堆栈
  1. git clone <url> #在当前目录下拉取远程仓库的代码
  2. 如:git clone git@gitee.com:huoshanmao/test.git,会在当前目录下建一个test的文件夹,里面有代码文件以及git的代码修改提交历史
复制代码
git add、git rm、git mv

  1. git add <file1> <file2> ... #添加多个指定文件到暂存区
  2. git add <directory_name> ... #添加指定目录到暂存区
  3. git add . #添加当前目录下所有文件到暂存区
  4. git add --all #添加当前目录下所有文件到暂存区
  5. git rm <file1> <file2> ... #删除工作区指定文件,并将这次删除多个指定文件放到暂存区
  6. git mv [file-original] [file-renamed] #改名文件,并且将这个改名放入暂存区
复制代码
git commit

  1. git commit -m <message> #添加暂存区到本地仓库区,并且添加message的提交说明
  2. git commit <file1> <file2> ... -m <message> #添加暂存区的指定文件到本地仓库区
  3. git commit --amend -m <message1> #使用新的commit替代原来的commit,如果文件没变化,则只是修改提交说明
  4. git commit --amend <file1> <file2> ... #使用新的commit替代原来的commit
复制代码
git push

  1. git push #推送到远程仓库,默认是origin源的当前分支
  2. git push <remote_name> <branch_name> #推送到远程仓库的branch_name分支
  3. 如:git push -u origin "master",第一次添加时加上-u的参数
  4. git push <remote_name> <commit_id>:<branch_name> #推送到远程仓库的branch_name分支,只是提交某一次commit_id的记录,而不会推送所有的commit记录到远程
  5. 如:git push origin 8c180a9:master git push <remote_name> --force #强行推送当前分支到远程仓库,即使有冲突 git push <remote_name> --all #推送所有分支到远程仓库
复制代码
git pull

  1. git pull #取回远程仓库origin的变化,并于当前分支合并,如果有冲突会标记出来
  2. git pull <remote_name> <branch_name> #取回远程仓库的变化,并与本地分支合并,相当于git fetch和git merge的结合
复制代码
git fetch

  1. git fetch <remote_name> #下载远程仓库的所有变动(远程新增或删除分支都能显示)
复制代码
git status

  1. git status #查看状态,包括所有变更的文件,工作区、暂存区、仓库区的状态变化
复制代码
git branch

  1. git branch #列出所有本地分支
  2. git branch -r #列出所有远程分支
  3. git branch -a #列出所有本地分支和远程分支
  4. git branch <branch_name> #新建一个分支,但依然停留在当前分支
  5. git branch --track <branch_name> <remote_name/branch_name> #新建一个分支,与指定的远程分支建立追踪关系,但还处于当前分支
  6. 如:git branch --track feat2 origin/feat2
  7. git branch --set-upstream <branch_name> <remote_branch_name> #建立追踪关系,在现有分支与指定的远程分支之间
  8. git branch -d <branch_name> #删除本地分支
  9. git push origin --delete <branch_name> #删除远程分支
  10. git branch -dr <remote_name/branch_name> #删除远程分支
复制代码
git checkout

  1. git checkout -b <branch_name> #以当前分支为基底,新建一个分支,并切换到该分支,要跟踪远程分支还需要pull命令或者--set-upstream
  2. git checkout <branch_name> #切换到指定分支,并更新工作区
  3. git checkout - #切换到上一个分支
复制代码
git switch

  1. git switch -c <branch_name> #新建一个分支,并切换到该分支
  2. git switch <branch_name> #切换到指定分支,并更新工作区
  3. git switch - #取消刚刚新建一个分支
复制代码
git merge

  1. git merge <branch_name> #合并指定分支branch_name到当前分支
复制代码
git cherry-pick

  1. git cherry-pick <commit_id> #选择一个commit,合并进当前分支
复制代码
git remote

  1. git remote -v #查看远程关联的仓库
  2. git remote add <remote_name> <url> #添加远程名为remote_name的源,并且对应url地址
  3. git remote set-url --[push|fetch] <remote_name> <new_url> #设置远程名为remote_name的源的url
  4. git remote get-url --[all|push] <remote_name> #查看远程名为remote_name的源的url
  5. 如:git remote get-url --all origin,可以获得远程仓库origin源的URL地址
  6. git remote show <remote_name> #显示某个远程仓库的信息
复制代码
git log

  1. git log #查看每一次提交的版本及commit_id
复制代码
git reflog

  1. git reflog #查看所有分支的所有操作记录,包括commit、reset操作,也可以看到被删除的commit记录
复制代码
git diff

  1. git diff  #比较工作区和暂存区的差异
  2. git diff <file_name>  #比较指定文件工作区和暂存区的差异
  3. git diff --cached  #比较暂存区和上一次提交(commit)的差异
  4. git diff --cached [file]
  5. git diff --staged [file]  #比较指定文件暂存区和上一次提交(commit)的差异
复制代码
git reset

  1. git reset #回退到上一次的commit,并且将差异保存在工作区
  2. git reset --hard <commit_id> #回退到某一次提交的版本,该commit之后的提交内容,工作区和暂存区的内容都被抹掉
  3. git reset --soft <commit_id> #回退到某一次提交的版本,该commit之后的提交内容,保留工作目录,并把重置 HEAD 所带来的新的差异放进暂存区
  4. git reset 或 git reset --mixed <commit_id> #不带参数,或带参数–mixed(默认参数),与git reset --soft 不同,它将会把差异放到工作区
  5. git reset --hard origin/master #回退到远程的master分支的最新版本
复制代码
git revert

  1. #git revert 命令意思是撤销某次提交,它会产生一个新的提交,虽然代码回退了,但是版本依然是向前的
  2. git revert HEAD #撤销最近一次提交
  3. git revert HEAD~1 #撤销上上次的提交,注意:数字从0开始
  4. git revert 0ffaacc #撤销0ffaacc这次提交
复制代码
git rebase 

  1. git rebase master feat1 #将feat1拉取分支的基底改成master分支最新提交记录为基底,并且合并feat1分支后面提交的记录到新基底上
  2. #相当于 git checkout feat1 和 git rebase master 组合使用
复制代码
git restore

  1. git restore <file> #丢弃指定文件file在工作区的修改
  2. git restore --staged <file>... #丢弃暂存区的文件修改,不暂存到暂存区,也就是恢复到工作区
复制代码
git tag

  1. git tag #列出所有tag
  2. git tag <tag_name> #新建一个tag在当前commit
  3. git tag <tag_name> <commit_id> #新建一个tag在指定commit
  4. git tag -d <tag_name> #删除本地tag
  5. git push origin:refs/tags/<tag_name> #删除远程tag
  6. git show <tag_name> #查看tag信息
  7. git push <remote_name> <tag_name> #提交指定tag
  8. git push <remote_name> --tags #提交所有tag
  9. git checkout -b <branch_name> <tag_name> #新建一个分支,指向某个tag
复制代码
git stash

git stash 会缓存:跟踪的但未添加到暂存区的文件(unstaged changes)、已经添加到暂存区的文件(staged changes)
不会缓存:新建的未跟踪的文件(untracked files)、被忽略的文件(ignored files)
  1. git stash list   #列出当前所有的储存文件记录
  2. git stash  #保存当前的工作区与暂存区的状态,把当前的修改的保存到git栈,等以后需要的时候再恢复,git stash 这个命令可以多次使用,每次使用都会新加一个stash@{num} num是编号
  3. git stash save '注释'  #作⽤等同于git stash,区别是可以加⼀些注释, 执⾏存储时,添加注释,⽅便查找
  4. 如:git stash save 'test'
  5. git stash pop  #默认恢复git栈中最新的一个stash@{num},建议在git栈中只有一条的时候使用,以免混乱,该命令将堆栈中最新保存的内容删除
  6. git stash apply  #将堆栈中的内容恢复到当前分支下。这个命令不同于 git stash pop。该命令不会将内容从对堆栈中删除,也就是该命令能够将堆栈的内容多次运用到工作目录,适合用与多个分支的场景
  7. 如:git stash apply stash@{2}
  8. git stash drop  #从堆栈中移除指定的stash
  9. 如:git stash drop stash@{3}
  10. git stash clear  #移除全部的stash
  11. git stash show  #查看堆栈中最新保存的stash和当前⽬录的差异
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

怀念夏天

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表