Git 常用命令使用

 
概述:
     工作区:workspace ,即 .git 目录的父目录,文件所在目录
     暂存库:index ,.git目录中的一块区域,用于记录所有工作区的变换,并暂存,(即:git add 提交修改到git,未 commit 到分支)
     当前分支:repository,.git目录中的一块区域,本地仓库,存放版本
 
一 配置
git config --global user.email "sl"
git config --global user.name "sl"
 
二 本地仓库
 
1)创建版本库
 
    在本地创建目录,进入该目录,调用git init 即创建本地仓库,完成后文件夹下会出现 .git 目录
[root@gittest ~]# mkdir gittest
[root@gittest ~]# cd gittest/
[root@gittest gittest]# git init
Initialized empty Git repository in /root/gittest/.git/
 
2)增加文件,提交
(a)在工作区添加修改文件,直接编辑修改即可
echo ’testgit’ >> README.md
 
(b)添加到暂存库(index): git add
git add README.md
 
(c)查看工作区情况: git status
[root@gittest gittest]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#    new file:   README.md
#
 
(d)删除添加: git rm —cached 
[root@gittest gittest]# git rm --cached README.md
rm 'README.md'
[root@gittest gittest]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    README.md
nothing added to commit but untracked files present (use "git add" to track)
 
(e)提交 : git commit 
[root@gittest gittest]# git add README.md
[root@gittest gittest]# git commit README.md
[master (root-commit) fdfa139] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
 
    期间会出现vi编辑器,添加comment,wq即可
 
3)工作区回退到最近一次 commit 或 add 修改: git checkout -- 
[root@gittest gittest]# cat README.md
gittest
[root@gittest gittest]# echo 'test redo' >> README.md
[root@gittest gittest]# cat README.md
gittest
test redo
[root@gittest gittest]# git checkout -- README.md
[root@gittest gittest]# cat README.md
gittest
 
注:撤销的两种情况:
    (a)修改还没有被放到暂存区,现在,撤销回到版本库的状态
    (b)修改已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
 
注2:必须带 -- 否则是切换分支命令
 
4)回退到某个提交的版本:git reset --hard 版本号
 
注:版本可用 HEAD 替代 :上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100
 
(a)查看命令操作日志:git reflog
[root@gittest gittest]# git reflog
3ecc7a9 HEAD@{0}: commit: add date
fdfa139 HEAD@{1}: commit (initial): first commit
 
(b)回退
[root@gittest gittest]# git reset --hard fdfa139
HEAD is now at fdfa139 first commit
[root@gittest gittest]# git reflog
fdfa139 HEAD@{0}: reset: moving to fdfa139
3ecc7a9 HEAD@{1}: commit: add date
fdfa139 HEAD@{2}: commit (initial): first commit
 
5)删除文件提交: git rm
在工作区删除后文件,需要通知git 在仓库中删除,方法 git rm 
 
[root@gittest gittest]# rm delete.file
rm: remove regular file 'delete.file'? y
[root@gittest gittest]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    deleted:    delete.file
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gittest gittest]# git rm delete.file
rm 'delete.file'
[root@gittest gittest]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    deleted:    delete.file
#
[root@gittest gittest]# git commit
[master 184c5b3]  Please enter the commit message for your changes. Lines starting
1 file changed, 1 deletion(-)
delete mode 100644 delete.file
[root@gittest gittest]# git status
# On branch master
nothing to commit, working directory clean
 
三 远程仓库操作
 
在gitlib上创建项目,完成后会自动提示相关命令:
 
1)clone远程仓库
git clone [email protected]:xxx/gittest.git
cd gittest
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
 
2)关联远程
      关联:git remote add origin git@server-name:path/repo-name.git;
      提交分支全部:git push -u origin master
cd existing_folder
git init
git remote add origin [email protected]:xxx/gittest.git 
git add .
git commit
git push -u origin master 
 
    配置ssh 方法略;
 
3)从远程拉取最新数据:git fetch origin master
 
四 分支
1)创建分支:git branch dev
 
2)切换分支:git chechout dev
[root@gittest gittest]# git checkout dev
Switched to branch 'dev'
 
3)查看分支:git branch
[root@gittest gittest]# git branch
* dev
  master
 
4)merge 分支到主干: git merge dev
切换到 master : git checkout master , git marge dev
[root@gittest gittest]# git branch
* dev
  master
[root@gittest gittest]# echo '20170729' >> README.md
[root@gittest gittest]# git commit README.md
[dev 405d48e] add date
1 file changed, 1 insertion(+)
[root@gittest gittest]# git checkout master
Switched to branch 'master'
[root@gittest gittest]# git branch
  dev
* master
[root@gittest gittest]# cat README.md
gittest
[root@gittest gittest]# git merge dev
Updating 184c5b3..405d48e
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
[root@gittest gittest]# cat README.md
gittest
20170729
 
5)解决冲突
[root@gittest gittest]# git merge dev
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
[root@gittest gittest]# 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:      README.md
#
no changes added to commit (use "git add" and/or "git commit -a")
 
(a)编辑冲突文件手动解决
[root@gittest gittest]# vi README.md
gittest
<<<<<<< HEAD
201707291632
=======
201707291631
>>>>>>> dev
 
(b)提交:git add + git commit
[root@gittest gittest]# 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:      README.md
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gittest gittest]# git add README.md
[root@gittest gittest]# git commit README.md
fatal: cannot do a partial commit during a merge.
[root@gittest gittest]# git commit -i README.md
[master f3cf6f9] Merge branch 'dev'
 
注:出现error: fatal: cannot do a partial commit during a merge.
        使用:git commit -i
 
(c)查看分支合并图:git log --graph --pretty=oneline
[root@gittest gittest]# git log --graph --pretty=oneline
*   f3cf6f9742af4ba69a956a10e1a63cd43a5a51ab Merge branch 'dev'
|\
| * 6bded575d923bfbfd9d57211f0006ddf3bbe7b5e add time
* | cc33cbf542f16e4fc2cbb79d78573967fb9115e7 add time
|/
* 405d48e85df83aa686dd00d4de01cc44aa12f5d5 add date
* 184c5b3bef0be97eb483e414424e8d25c29e0090  Please enter the commit message for
* 0f7346b632bd645b73646a4d2b03f413a1dfdf8c delete
* fdfa139bc11a1c105c2ac7fa166b98797174a2f8 first commit
 
(d)合并完,分支可以删除了:git branch -d dev
 
注:开发分支使用策略(略)
 
五 保存现场
 
[root@gittest gittest]# git status
# On branch master
# 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:   tmp.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gittest gittest]# git checkout dev
error: Your local changes to the following files would be overwritten by checkout:
    tmp.txt
Please, commit your changes or stash them before you can switch branches.
Aborting
 
注: 当工作区有关联的文件修改,未提交时不能切换分支,此时要不提交完成,要不回退,也可以使用’git  stash’暂存
 
1)暂存:git stash
[root@gittest gittest]# git stash
Saved working directory and index state WIP on master: 1caf27f tmp file
HEAD is now at 1caf27f tmp file
[root@gittest gittest]# git status
# On branch master
nothing to commit, working directory clean
 
2)查看暂存列表:git stash list
[root@gittest gittest]# git stash list
stash@{0}: WIP on master: 1caf27f tmp file
 
3)暂存后的两种恢复操作
 
(a)恢复并删除:git stash apply恢复,用git stash drop删除;
[root@gittest gittest]# git stash apply
# On branch master
# 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:   tmp.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gittest gittest]# git status
# On branch master
# 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:   tmp.txt
#
no changes added to commit (use "git add" and/or "git commit -a”)
[root@gittest gittest]# git stash drop
Dropped refs/stash@{0} (6288235160baf1e05c2f954428377bc6e4642b0a)
 
(b)不恢复直接删除:git stash drop
 
六  管理Tag:
1)创建:git tag
[root@gittest gittest]# git tag v0.0.1
[root@gittest gittest]# git tag
v0.0.1
 
注:在某个commit 上打版本:git tag v0.0.1 commit_id
[root@gittest gittest]# git reflog
1caf27f HEAD@{0}: commit: tmp file
cade529 HEAD@{1}: commit (initial): first commit
[root@gittest gittest]# git tag v0.0.1 1caf27f
[root@gittest gittest]# git tag
v0.0.1
2)删除:git tag -d
[root@gittest gittest]# git tag -d v0.0.1
Deleted tag 'v0.0.1' (was 1caf27f)
[root@gittest gittest]# git tag
 
3)推送tag 到远程:git push origin v0.0.1

猜你喜欢

转载自shihlei.iteye.com/blog/2387623