美文网首页
git常用命令

git常用命令

作者: 韦恩时代 | 来源:发表于2016-04-25 11:13 被阅读258次

分支操作

1、同步远程分支到本地,并切换到该分支
git checkout -b local_branch orgin/remote_branch
2、删除本地分支
git branch -d branch_name

注意:在当前分支下是不能删除当前分支的,需要切换到别的分支。

3、删除远程分支

在Git v1.7.0 之后,可以使用这种语法删除远程分支:

git push origin --delete <branchName>

也可以使用这种语法,推送一个空分支到远程分支,其实就相当于删除远程分支:

git push orgin  :develop/hotfix  #我的分支名叫develop/hotfix
4、本地创建分支并提交远程

1、先切换到要拉分支的分支上,比如现在有一个develop分支,我想拉一个develop/hotfix分支:

git checkout develop

2、创建一个本地分支并切换到该分支:

git checkout -b develop/hotfix

这样写可能会有问题,报错:

  fatal: cannot lock ref 'refs/heads/develop/buxfix': 'refs/heads/develop' exists; cannot create 'refs/heads/develop/buxfix'

不应该包含当前分支develop的名字,改成feature/bugfix就可以了。
3、将此分支推到远程去:

git push origin develop/hotfix:develop/hotfix

4、这时候通过git remote show origin 查看发现本地还没有跟远程建立track信息,所以还需要:

git branch --set-upstream-to=origin/develop/hotfix  develop/hotfix

这样就创建完成了。

5、撤销本次操作

1、只修改了某些文件,但是没有commit

git checkout .  # 后面跟某个文件,或者跟 "."是全部文件

2、添加了某些新的文件,但是没有commit
如果我们直接用checkout的话,修改的文件是可以撤销的,但是新添加的文件还是无法撤销,git status 查看如下所示

Changes to be committed:
    (use "git reset HEAD <file>..." to unstage)
        new file:   BFSports/BFSports/Common/Util/BFSKeychain.h
        new file:   BFSports/BFSports/Common/Util/BFSKeychain.m

根据提示我们可以使用下面git reset HEAD <file>命令来撤销

git reset HEAD  #如果后面什么都不跟的话 就是上一次add 里面的全部撤销了 
git reset HEAD BFSports/BFSports/Common/Util/BFSKeychain.h   #就是对某个文件进行撤销了

注意:reset后,文件还是存在的,需要手动删除一下文件,这样就彻底删除了。

3、撤销已经在本地commit的修改,但是没有push到远端
假如最近一次commit 哈希值是hash2,上一次哈希值是hash1,我想撤销掉最近的这次commit,则操作如下:

git reset hash1 #这时 git status 查看会有如下信息
Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)

这时候文件的状态都变成了modified了,注意modified前面没有“M”。这个时候就是出于文件刚被修改的最初状态了,即还没有add之前的。然后再:

git checkout <file> #然后再git status查看发现已经修改已被完全撤销了。

关于reset的讲解,有篇文章不错,讲了git维护的代码分为三个部分:当前工作目录,index,git仓库。如下图:

工作区示意图

工作区的代码修改了,执行add操作后就添加到了index,再commit之后才到版本库。可以通过reset命令的 --mixed、--soft、--hard来控制回到哪个位置。默认是--mixed直接由git仓库回到工作区最初的修改状态;--soft是由工作区回到index,也就是add之后的状态;--hard是直接撤销到修改之前的状态。
文章链接:http://blog.csdn.net/codectq/article/details/50777934

5、修改用户名密码

当登录的用户名、邮箱、密码发生修改时,需要修改本地的git配置,如果你本地所有的仓库都是用的同一个账号密码可以这样修改

git config  --global  user.name   xiaoming
git config --global   user.email   xiaoming@163.com
git config --global   user.password  1234

如果有多个仓库且每个仓库对应的账号密码不一样,则只修改某一个仓库就可以了,首先进入到你要修改的某个仓库目录里面,然后执行

git config   user.name   xiaoming
git config   user.email   xiaoming@163.com
git config   user.password  1234

其实就是没有 --global 参数。

小问题

1、比较两个版本所有修改的文件列表

git diff branch1 branch2 --stat

2、在gitlab上删除某个分支后,在本地执行

git pull 
git branch -r

发现还有已删除的分支名称,执行以下代码就可以了
``

git fetch -p

``

3、今天在提交代码的时候,有如下错误提示:

fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use

git push origin HEAD:hotfix/start-2016-10-12

To push to the branch of the same name on the remote, use

git push origin hotfix

导致出现这个问题的原因是,我在将远程分支check到本地后,起了一个不同的名字:

git branch -b hotfix orgin/hotfix/start-2016-10-12

因为rename的原因git失去了hotfix分支跟远程分支的track.解决此问题一般有两个方式:
1、就是本地分支跟远程分支名字保持一致。
2、给rename后的分支重新跟远程分支建立track:

git checkout hotfix
git branch -u orgin/hotfix/start-2016-10-1

相关文章

网友评论

      本文标题:git常用命令

      本文链接:https://www.haomeiwen.com/subject/kuafrttx.html