git命令行使用教程

基础使用

Linux服务器安装git服务端

首先root用户登录到Linux

[root@bd003 ~]# yum install git
[root@bd003 ~]# groupadd git
[root@bd003 ~]# useradd git -g git
[root@bd003 ~]# passwd git
更改用户 git 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
注:此时的密码即为后面clone时要用的密码

初始化项目路径

想要使用git存储项目,首先需要在Git的服务端初始化的路径。

[root@bd-07 ~]# mkdir -p /home/git/repos/first.git
[root@bd-07 ~]# cd /home/git/repos/first.git/     
[root@bd-07 test.git]# ls
[root@bd-07 test.git]# git init --bare /home/git/repos/first.git/
初始化空的 Git 版本库于 /home/git/repos/first.git/
[root@bd-07 test.git]# ls
branches  config  description  HEAD  hooks  info  objects  refs

可以见到此时已经初始化完成

注:一定要更改目录的属组和用户,否则push的时候会报如下错误

G:\b\nifiDefined>git push
[email protected]'s password:
Counting objects: 340, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (283/283), done.
remote: fatal: Unable to create temporary file '/home/git/repos/first.git/./objects/pack/tmp_pack_XXXXXX': ????
fatal: sha1 file '<stdout>' write error: Broken pipe
error: remote unpack failed: index-pack abnormal exit
error: failed to push some refs to '[email protected]:/home/git/repos/first.git'

更改属组和用户命令如下:

[root@bd-07 repos]# chown -R git:git /home/git/repos/first.git

常用git命令

在执行代码提交命令之前,首先需要设置本地的用户名和邮箱

git config --global user.name wu
git config --global user.email [email protected]

否则commit的时候会报如下错误

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@bd003.(none)')

以下命令如果需要输入密码,则是上面添加git用户时设置的密码

# 首次获取版本库中的代码需要使用如下命令
# 其中10.1.2.44为版本库所在的IP地址,其后为服务器上的目录地址
1、git clone [email protected]:/home/git/repos/first.git
# 新添加文件时,需要使用下面的命令将文件放到暂存区中
2、git add *
# add之后或是修改文件之后,则需要用下面的命令将改动提交到暂存区中
# 执行commit的时候需要输入备注,即执行完commit的时候,会跳到一个新的页面
# 此时的操作和vim很像,如果想输入备注信息,按i或a即可进入编辑状态,输入完成Esc退出编辑状态
# 然后wq保存,即可完成commit
# 快捷命令git commit -a -m '备注内容'
3、git commit *
# 下面的命令则是将暂存区中的内容放到版本库中
4、git push
# 如果想要拉取版本库中的代码,则使用下面的命令
5、git pull
# 强制覆盖本地拉取远程代码
6、git fetch --all && git reset --hard origin/master && git pull

分支使用

常用命令

# 添加名为b1的新分支
1、git branch b1
# 切换到名为b1的分支
2、git checkout b1
# 上面两条命令的简写
3、git checkout -b b1
# 创建远程分支,并将本地分支提交到这个远程分支上
4、git push --set-upstream origin b1
# 查看当前分支的状态
# 位于分支 b1
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#	修改:      a.txt
#
# 修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
5、git status
# 查看所有分支,带*号的为当前所在分支
#	[root@bd003 first]# git branch -a
#	* b1
#	  master
#	  remotes/origin/master
6、git branch -a
# 删除名为b1的分支,未合并的分支不允许删除
7、git branch -d b1
# 强制删除未合并的分支
8、git branch -D b1
# 绑定本地分支和远端分支的关联关系
# git branch --set-upstream-to=远端分支名称 本地分支名称
9、git branch --set-upstream-to=origin/test test

合并分支

合并b1的分支

扫描二维码关注公众号,回复: 10434796 查看本文章

无冲突

git merge b1

如果当前分支和b1分支只有一个有改动的话,可以直接merge成功

[root@bd003 first]# git merge b1
更新 a0b9e4c..50bb297
Fast-forward
 a.txt | 2 ++
 c.txt | 5 ++++-
 2 files changed, 6 insertions(+), 1 deletion(-)

有冲突

[root@bd003 first] git merge b1
	Auto-merging index.html
	CONFLICT (content): Merge conflict in index.html
	Automatic merge failed; fix conflicts and then commit the result.
[root@bd003 first] git status
	On branch master
	You have unmerged paths.
	  (fix conflicts and run "git commit")
	
	Unmerged paths:
	  (use "git add <file>..." to mark resolution)
	
	    both modified:      index.html
	
	no changes added to commit (use "git add" and/or "git commit -a")

此时说明index.html有冲突问题,那么就需要打开这些文件然后手动解决冲突。
git对于有冲突的文件的冲突部分会做特殊标记,如下:

<<<<<<< HEAD:index.html
<div id="footer">contact : [email protected]</div>
=======
<div id="footer">
 please contact us at [email protected]
</div>
>>>>>>> b1:index.html

这表示 HEAD 所指示的版本(也就是 master 分支所在的位置,因为在运行 merge 命令的时候已经检出到了这个分支)在这个区段的上半部分(======= 的上半部分),而 b1 分支所指示的版本在 ======= 的下半部分。 为了解决冲突,必须选择使用由 ======= 分割的两部分中的一个,或者也可以自行合并这些内容。例如,可以通过把这段内容换成下面的样子来解决冲突:

<div id="footer">
please contact us at [email protected]
</div>

上述的冲突解决方案仅保留了其中一个分支的修改,并且 <<<<<<< , ======= , 和 >>>>>>> 这些行被完全删除了。 在解决了所有文件里的冲突之后,对每个文件使用 git add 命令来将其标记为冲突已解决。 一旦暂存这些原本有冲突的文件,Git 就会将它们标记为冲突已解决。然后就可以输入git commit来完成提交。

下面的连接有更为详细的介绍
https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6

发布了26 篇原创文章 · 获赞 1 · 访问量 6975

猜你喜欢

转载自blog.csdn.net/qq_39800434/article/details/84982684