Git常用命令总结/Git基础命令总结

时间:2022-12-10 00:23:59


个人原创总结.虽然平时主要用sourcetree了,基础命令还是要知道的.


1.参数配置
git config --global user.email xxxx@qq.com
git config --global user.name bobshute

2.添加到提交
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
git init
git add
git add *.html
git commit

git status
git log

3.常见问题:
1.warning: LF will be replaced by CRLF
$ rm -rf .git 
$ git config --gobal core.autocrlf false 


4.分支
4.1创建分支
$ git branch branch2;
4.2创建分支
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ git branch
* master
  testing
4.3切换分支
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ git checkout testing;
Switched to branch 'testing'
4.4创建并切换分支
$ git checkout -b branch01
Switched to a new branch "branch01“
相当于下面这两条命令:
$ git branch branch01
$ git checkout branch01
4.5合并分支
在master分支下合并branch01的内容
$ git checkout master
$ git merge branch01
4.6删除分支
$ git branch -d branch01
Deleted branch branch01(3a0874c).
1)##在branch2分支下删除branch2分支
$ git branch  -d branch2;
error: Cannot delete branch 'branch2' checked out at 'D:/javatools/cygwin64/gittest'
2)##在其它分支(未合并过branch2分支)下删除branch2分支
$ git branch -d branch2;
error: The branch 'branch2' is not fully merged.
If you are sure you want to delete it, run 'git branch -D branch2'.
3)##在合并过branch2分支的分支下操作删除分支
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ git checkout master;
Switched to branch 'master'
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ git merge branch2;
Already up-to-date.
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ git branch -d branch2;
Deleted branch branch2 (was f2d59f7).
4.7修改文件在不提交的情况下不能切换分支
1)新修改(没有执行add)的情况下不能切换
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ git checkout testing;
error: Your local changes to the following files would be overwritten by checkout:
        a.html
Please commit your changes or stash them before you can switch branches.
Aborting
2)执行了add但是没有执行commit的情况下也不能切换
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ git add a.html

shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ git checkout testing;
error: Your local changes to the following files would be overwritten by checkout:
        a.html
Please commit your changes or stash them before you can switch branches.
Aborting
4.8 stash 储藏
1) 解决4.7的问题,如果修改了但是没有commit;那么可以stash之后在切换分支
2)stath储藏
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ git stash
Saved working directory and index state WIP on testing: 70cf1fc commit html
HEAD is now at 70cf1fc commit html

$ git stash;
Saved working directory and index state WIP on master: f2d59f7 a.html commit
HEAD is now at f2d59f7 a.html commit


3)stash list查看当前的stash
$ git stash list;
stash@{0}: WIP on testing: 70cf1fc commit html
stash@{1}: WIP on master: f2d59f7 a.html commit
4)恢复stash
A)不存在的stash或其它分支的stash;
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ git stash apply 70cf1fc;
'70cf1fc' is not a stash-like commit
B)


5.原理:
5.1 分支地址:
.git\refs\heads
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest/.git/refs/heads
$ ls
master  testing

6.远程命令
6.1. 配置账户
1)账户网址:
https://bitbucket.org/
2)配置账户
git config --global user.name "bobshute";
git config --global user.email "xx@qq.com"
3)生成密钥
ssh-keygen -t rsa -C "xx@qq.com"
生成过程:
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ ssh-keygen -t rsa -C "xx@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/shubb/.ssh/id_rsa):
Created directory '/home/shubb/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/shubb/.ssh/id_rsa.
Your public key has been saved in /home/shubb/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:i5/m9CWI3vpMvjaTpvxYxDE1BERX1PB3bjBkuoLU11M xx@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|       o+o=o+oo E|
|         + . *.. |
|        + . o * o|
|       o + . . *.|
|        S . .   o|
|       + o .   . |
|      o *.. .    |
|     o XBo o     |
|      *X@+.      |
+----[SHA256]-----+

shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$
4)密钥位置
A)cygwin:
生成的默认位置:D:\javatools\cygwin64\home\shubb\.ssh
B)Git Bush
复制到:C:\Users\shubb\.ssh
C)乌龟Git生成方式请见截图
使用"D:\Program Files\TortoiseGit\bin\puttygen.exe"生成

Git常用命令总结/Git基础命令总结

\Git常用命令总结/Git基础命令总结






5)测试密钥是否可用
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/gittest
$ ssh git@bitbucket.org
Warning: Permanently added the RSA host key for IP address '104.192.143.2' to the list of known hosts.
PTY allocation request failed on channel 0
logged in as bob.

You can use git or hg to connect to Bitbucket. Shell access is disabled.
Connection to bitbucket.org closed.


6.2 clone 工程
git clone git@bitbucket.org:bob/bob.git

6.3 推送工程
shubb@shubb-PC /cygdrive/d/javatools/cygwin64/git_tools/bitbucket/gitproject_bob/bob
1)完整写法
#u追踪,origin远程分支,master本地分支.可以查看.git目录下的config
$ git push -u origin master;
Counting objects: 3, done.
Writing objects: 100% (3/3), 215 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@bitbucket.org:bob/bob.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
2)简写
#简写用的就是.git/config中[remote "origin"]和 [branch "master"]
$git push

6.4主动将本地(master[当前所在]分支)推送到远程仓库中
$git remote add origin git@mygitlabold.systes.net:root/testhtml2013.git

7.多人协作
7.1版本不一致时解决

#将远程记录获取到本地(不覆盖工作区)
A)git fetch origin
#查看远程日志
B)git log --no-merges origin/master
#合并版本[默认是本地的分支,将origin/master分支的内容合并到本地分支]
C)git merge origin/master
#解决冲突
D)如果有冲突,解决充足,冲突中的HEAD是本地的, origin/master是远程的.
#解决冲突假如暂存区
E)git add index.html
#解决冲突后的提交
F)git commit -m "提交" index.html
#推送
G)git push -u origin master

7.2获取最新文件
$git fetch

7.3其它常用命令
A)查看当前远程仓库
$git remote –v
B)如何查看本地分支与远程分支的联系
$git branch –vv
C)如果本地有个master 和远程的 origin/master分支没有建立跟踪关联
$git branch --set-upstream master origin/origin