190101-Git常用命令

git常用操作命令

创建本地分支与远程分支

例如现在有两个分支,master和develop

git checkout master //进入master分支

git checkout -b frommaster //以master为源创建分支frommaster

git checkout develop //进入develop分支

git checkout -b fromdevelop //以develop为源创建本地分支fromdevelop

git push origin fromdevelop //将本地fromdevelop分支作为远程fromdevelop分支

总结:一般我们就用git push –set-upstream origin branch_name来在远程创建一个与本地branch_name同名的分支并跟踪;利用git checkout –track origin/branch_name来在本地创建一个与branch_name同名分支跟踪远程分支。

拉取指定的远程分支

直接拉取(指定远程分支名)

1
2
git clone -b ants git@github.com:Ants-double/CareerJava.git
git clone -b 远程分支名 仓库地址

git使用用户名密码clone

1
git clone http://username:password@remote

本地已经有相关的仓库代码

1
2
3
4
5
6
7
8
//查看远程分支
git branch -r
//创建本地分支并关联
git checkout -b 本地分支 origin/远程分支
//已有本地分支创建关联
git branch --set-upstream-to origin/远程分支名 本地分支名
//拉取
git pull

git拉取远程分支并创建本地分支

1
2
3
4
5
6
7
8
9
10
11
# 使用如下git命令查看所有远程分支:
git branch -r

# 拉取远程分支并创建本地分支
# 方式一:使用该方式会在本地新建分支x,并自动切换到该本地分支x。采用此种方法建立的本地分支会和远程分支建立映射关系。

git checkout -b 本地分支名x origin/远程分支名x

# 方式二:该方式会在本地新建分支x,但是不会自动切换到该本地分支x,需要手动checkout。采用此种方法建立的本地分支不会和远程分支建立映射关系。

git fetch origin 远程分支名x:本地分支名x

远程仓库和本地代码同步

1
2
3
git remote add origin https://github.com/miaoihan/weibo.git  
git fetch origin //获取远程更新
git merge origin/master //把更新的内容合并到本地分支

修改远程分支名称

1
2
3
4
5
6
7
8
#创建新分支
git branch -m 旧分支名 新分支名
#删除远程分支
git push --delete origin 旧分支名
#提交新分支
git push origin 新分支名
#将新本地分支和远程相连
git branch --set-upsteam-to origin/新分支名

git merge 是出现Merge remote-tracking branch ‘ups/master’

git push

从远程仓库克隆到本地后,

远程仓库有人提交,本地仓库commit了1次,

目标:把本地仓库push到远程仓库

做法:1,git fetch xxx 2,git merge xxx/master 3,git push

Q&A

问题 : git merge 是出现Merge remote-tracking branch ‘ups/master’

原因:远程仓库有人提交,本地仓库commit了1次

解决方案: git rebase -i HEAD~2

如果不想让他出现怎么办

做法:

  1. 在修改之前做如下操作

    • git fetch 远程仓库
    • git merge 远程仓库分支
  2. 在本地仓库commit

  3. push到远程仓库

    rebase 1,合并 2,修改记录

删除本地仓库以及远程仓库记录

环境:

  1. git add file //file到暂存区

  2. git commit -m “alter” //file提交到本地仓库

  3. git push //file push到远程仓库

问题1:执行1后,把file从暂存区回退到工作区
git reset hash

问题2:执行1,2后,把file从本地仓库回退到暂存区
git reset –soft hash

问题3:执行1,2,3后,把file从本地和远程仓库全部删除
git reset –hard hash 直接把记录删除了,暂存区和工作区都没有了。
git push -f 远程仓库 分支

Git回滚代码到某个commit

git reset –hard HEAD^ // 回退到上个版本
git reset –hard HEAD~3 // 回退到前3次提交之前,以此类推,回退到n次提交之前
git reset –hard commit_id // 退到/进到 指定commit的sha码
git push origin HEAD –force //强推到远程

git rebase -i 丢弃指定提交

针对想撤销中间某次commit的情况,可以使用如下的命令:
git rebase -i HEAD~2

tips:

1
2
`rebase -i`是 `rebase --interactive` 的缩写;
`git rebase -i` 不仅可以删除commit, 还可以修改commit。 具体的可以查看rebase 中提示的参数

输入git rebase -i HEAD~2命令后,会出现一个编辑页面如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ git rebase -i HEAD~2

drop e47fa58 提交11
pick 338955c 提交12

# Rebase 7f83da3..338955c onto 7f83da3 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~
  1. 回滚最新的提交 :git reset 和 git rebase 命令都可以
  2. 回滚中间某次提交: git rebase 可以, git reset 不可以
  3. 如果提交已经同步到远程仓库,需要使用git push origin -f branch(分支名) 来将回滚也同步到远程仓库(master 分支谨慎使用 -f)

Git如何合并某一次commit的内容到指定分支

1
2
git switch xxxbranch
git cherry-pick commit_id

参考资料

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×