获取git仓库两种方式:
(1)本地创建
git init
git add *.c
git add LICENCE
git commmit -m "new git respository"
(2)克隆已存在的仓库
git clone <url>
记录变更到仓库中:
在工作目录中,每个文件只能存在两种状态:tracked和untracked。tracked状态包装modified, unmodifed和staged.
(3)检查文件状态
git status
git status -s
Tracking新文件
(4)git add newFile
Staging已修改文件
(5)git add modifiedFile
忽略文件
编辑.gitignore文件
提交修改
(6)git commit -m "comment"
跳过staging状态
(7)git commit -a -m "comment"
移除文件
从git中移除文件,首先从tracked文件中然后提交它。
(8)git rm <filename>
在git中重命名文件
(9)git mv oldfile newfile
-------------------------------------------------------------------------------------------------
查阅提交历史:
(10)git log
-p --patch: 显示每次提交引入的差异。
git log -p -2: 显示最后两次提交差异内容
--pretty: 格式化的内容显示,可选的格式为oneline, short, full, fuller
format选项
git log --pretty=format=:"%h - %an, %ar : %s"
(11)限制日志输出
git log --since=2.weeks
-S: 用于过滤,只显示匹配的内容
放弃修改
(12)git commit -amend ##一般用在提交内容时少提交了某些内容,想重新提交;
Unstage文件
(13)git reset HEAD <stagedFile>
将已修改文件还原
(14)git checkout -- <modifiedFile>
--------------------------------------------------------------------------------------------------
远程管理
显示配置的远程服务器
git remote //显示远程指定的句柄
git remote -v //显示URL
新增远程仓库
git remote add pb https://github.com/paulboone/ticgit
远程拉取
git fetch <remote>
推送remote
git push <remote> <branch> //推送master分支到origin服务器
探查远程
git remote show origin
对remote重命名
git remote rename pb paul
git remote rm
---------------------------------------------------------------------------------------------------
标签:
列出标签:
git tag
git tag -l <regexpr>
创建标签
git tag -a v1.4 -m "my version 1.4" //-a:标签名 -m:标签消息
git show <tag>
分享标签
git push origin <tagname>
删除标签
git tag -d <tag>
checkout标签
git checkout 2.0.0
---------------------------------------------------------------------------------------------------
分支:
git中默认名称是master,当你开始提交修改,没提交一次,master向前移动一次;
创建分支:
git branch newBranch
切换分支:
git checkout <branch>
基础分支与合并
基础分支master上已经commit几次
git checkout -b iss53 //创建新分支并切换到新分支iss53上
git merge hotfix //合并hotfix分支到当前分支上
删除分支:
git branch -d branchName
冲突合并:
在合并的过程中发现文件存在冲突
git status
手动解决冲突或者使用git mergetool图形化工具解决冲突
--
分支管理
列出当前分支
git branch
-v :显示最后提交的注释
--merged:显示已合并的分支
--no-merged:
分支工作流:
主题分支:
主题分支在任何规模的项目上都可用,topic-branch存在时间短暂,主要是为特殊的功能存在;
远程分支:
远程引用是指向远程仓库中分支、标签。
git ls-remote [remote] 或者 git remote show [remote] //获取远程引用
远程跟踪分支名称的形式如[remote]/[branch]
推送:
git push <remote> <branch>
git push origin serverfix:awesomebranch //本地推送到远程awesomebranch分支上
拉取:
相比git pull而言,git fetch && git merge更推荐使用
删除远程分支
git push <remote> --delete <branch>
---------------------------------------------------------------------------------------------------
git中存在两种方式将一个分支合并到另一个分支上,分别是merge和rebase
git checkout experimental //
git rebase master
---------------------------------------------------------------------------------------------------
在服务器上使用git
在服务器上运行git
1) 将已有的仓库导出到新的空仓库中。为了在克隆仓库过程中创建一个新的仓库,使用--bare选项
git clone --bare my_project my_project.git
cp -Rf my_project/.git my_project.git
2) 将仓库放置到服务器上
scp -r my_project [email protected]:/srv/git
3) 克隆到本地
git clone [email protected]:/srv/git/my_project.git
在my_project.git目录中执行
git init --bare --shared
4) 生成ssh公钥
cd ~/.ssh 列出目录下的文件,以.pub结尾的文件是公钥文件
ssh-keygen -o
添加到服务器~/.ssh/authorized_keys文件中
5) 启动服务器
# adduser git
# su git
# cd
# mkdir .ssh && chmod 700 .ssh
# touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
# cat ~/xxx_ras.pub >> ./ssh/authorized_keys
# cd /srv/git
# mkdir project.git
# cd project.git
# git init --bare
6) git守护进程
$ git daemon --reuseaddr --base-path=/srv/git /srv/git
创建git-daemon.service文件,添加如下内容
------------------------------------------------------------------
[Unit]
Description=Start Git Daemon
[Service]
ExecStart=/usr/bin/git daemon --reuseaddr --base-path=/srv/git /srv/git
Restart=always
RestartSec=500ms
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=git-daemon
User=git
Group=git
[Install]
WantedBy=multi-user.target
------------------------------------------------------------------
移动到/etc/systemd/system目录中
# systemctl enable git-daemon
---------------------------------------------------------------------------------------------------