git 代码管理工具 命令总结

版权声明:lie_sun版权所有 https://blog.csdn.net/weixin_43260760/article/details/85122470

个人习惯 一般情况下现在远程创建代码库,然后克隆到本地,这样的好处是直接了当编辑器命令行推送,简单方便,所以就不讨论 git init 了,想要了解的自行上网查阅资料;

git clone
git clone -b 分支名  git项目地址
git cone git项目地址  //默认克隆的是主分支的项目

clone下来的repo会以url最后一个斜线后面的名称命名,创建一个文件夹,如果想要指定特定的名称,可以git clone [url] newname指定.

git status
 查询repo的状态.
 git status -s: -s表示short, -s的输出标记会有两列,第一列是对staging区域而言,第二列是对working目录而言.
git log
 show commit history of a branch.
 git log --oneline --number: 每条log只显示一行,显示number条.
 git log --oneline --graph:可以图形化地表示出分支合并历史.
 git log branchname可以显示特定分支的log.
 git log --decorate会显示出tag信息.
 git log --author=[author name] 可以指定作者的提交历史.
 git log --since --before --until --after 根据提交时间筛选log.
 --no-merges可以将merge的commits排除在外.
 git log --grep 根据commit信息过滤log: git log --grep=keywords
git add
 在提交之前,Git有一个暂存区(staging area),可以放入新添加的文件或者加入新的改动
 git add .
 会递归地添加当前工作目录中的所有文件.
git commit
 提交已经被add进来的改动.
 git commit -m “the commit message"
 git commit -a 会先把所有已经track的文件的改动add进来,然后提交(有点像svn的一次提交,不用先暂存).
 git commit --amend 增补提交. 会使用与当前提交节点相同的父节点进行一次新的提交,旧的提交将会被取消.
git revert
 反转撤销提交.只要把出错的提交(commit)的名字(reference)作为参数传给命令就可以了.
 git revert HEAD: 撤销最近的一个提交.
 git revert会创建一个反向的新提交,可以通过参数-n来告诉Git先不要提交.
git rm
 git rm file: 从staging区移除文件,同时也移除出工作目录.
 git rm --cached: 从staging区移除文件,但留在工作目录中.
 git rm --cached从功能上等同于git reset HEAD,清除了缓存区,但不动工作目录树.
git clean
 git clean是从工作目录中移除没有track的文件.
 通常的参数是git clean -df:
 -d表示同时移除目录,-f表示force,因为在git的配置文件中, clean.requireForce=true,如果不加-f,clean将会拒绝执行.
git mv
 git rm - - cached orig; mv orig new; git add new
git stash
 把当前的改动压入一个栈.
 git stash将会把当前目录和index中的所有改动(但不包括未track的文件)压入一个栈,
 然后留给你一个clean的工作状态,即处于上一次最新提交处.
 git stash list会显示这个栈的list.
 git stash apply:取出stash中的上一个项目(stash@{0}),并且应用于当前的工作目录.
 也可以指定别的项目,比如git stash apply stash@{1}.
 如果你在应用stash中项目的同时想要删除它,可以用git stash pop
 删除stash中的项目:
 git stash drop: 删除上一个,也可指定参数删除指定的一个项目.
 git stash clear: 删除所有项目.
git branch
 git branch可以用来列出分支,创建分支和删除分支.
 git branch -v可以看见每一个分支的最后一次提交.
 git branch: 列出本地所有分支,当前分支会被星号标示出.
 git branch (branchname): 创建一个新的分支(当你用这种方式创建分支的时候,分支是基于你的上一次提交建立的). 
 git branch -d (branchname): 删除一个分支.
 删除remote的分支:
 git push (remote-name) :(branch-name): delete a remote branch.
 这个是因为完整的命令形式是:
 git push remote-name local-branch:remote-branch
 而这里local-branch的部分为空,就意味着删除了remote-branch
git checkout

git checkout (branchname)
切换到一个分支.
git checkout -b (branchname): 创建并切换到新的分支.
这个命令是将git branch newbranch和git checkout newbranch合在一起的结果.
checkout还有另一个作用:替换本地改动:
git checkout –
此命令会使用HEAD中的最新内容替换掉你的工作目录中的文件.已添加到暂存区的改动以及新文件都不会受到影响.
注意:git checkout filename会删除该文件中所有没有暂存和提交的改动,这个操作是不可逆的.

git merge
 把一个分支merge进当前的分支.
 git merge [alias]/[branch]
 把远程分支merge到当前分支.

 如果出现冲突,需要手动修改,可以用git mergetool.
 解决冲突的时候可以用到git diff,解决完之后用git add添加,即表示冲突已经被resolved.
git remote
 list, add and delete remote repository aliases.
 因为不需要每次都用完整的url,所以Git为每一个remote repo的url都建立一个别名,然后用git remote来管理这个list.
 git remote: 列出remote aliases.
 如果你clone一个project,Git会自动将原来的url添加进来,别名就叫做:origin.
 git remote -v:可以看见每一个别名对应的实际url.
 git remote add [alias] [url]: 添加一个新的remote repo.
 git remote rm [alias]: 删除一个存在的remote alias.
 git remote rename [old-alias] [new-alias]: 重命名.
 git remote set-url [alias] [url]:更新url. 可以加上—push和fetch参数,为同一个别名set不同的存取地址.
git fetch
 download new branches and data from a remote repository.
 可以git fetch [alias]取某一个远程repo,也可以git fetch --all取到全部repo
 fetch将会取到所有你本地没有的数据,所有取下来的分支可以被叫做remote branches,
 它们和本地分支一样(可以看diff,log等,也可以merge到其他分支),但是Git不允许你checkout到它们. 
git pull
 fetch from a remote repo and try to merge into the current branch.
 pull == fetch + merge FETCH_HEAD
 git pull会首先执行git fetch,然后执行git merge,把取来的分支的head merge到当前分支.
 这个merge操作会产生一个新的commit.    
 如果使用--rebase参数,它会执行git rebase来取代原来的git merge.
git rebase
 --rebase不会产生合并的提交,它会将本地的所有提交临时保存为补丁(patch),放在”.git/rebase”目录中,
 然后将当前分支更新到最新的分支尖端,最后把保存的补丁应用到分支上.
 rebase的过程中,也许会出现冲突,Git会停止rebase并让你解决冲突,在解决完冲突之后,用git add去更新这些内容,
 然后无需执行commit,只需要:
 git rebase --continue就会继续打余下的补丁.
 git rebase --abort将会终止rebase,当前分支将会回到rebase之前的状态.
git push
 push your new branches and data to a remote repository.
 git push [alias] [branch]
 将会把当前分支merge到alias上的[branch]分支.如果分支已经存在,将会更新,如果不存在,将会添加这个分支.
 如果有多个人向同一个remote repo push代码, Git会首先在你试图push的分支上运行git log,
 检查它的历史中是否能看到server上的branch现在的tip,如果本地历史中不能看到server的tip,
 说明本地的代码不是最新的,Git会拒绝你的push,让你先fetch,merge,之后再push,这样就保证了所有人的改动都会被考虑进来.

猜你喜欢

转载自blog.csdn.net/weixin_43260760/article/details/85122470