GIt 基础操作如图所示:
1.追踪文件
方式1 (追踪当前目录下所有不被.gitignore文件)
git add .
方式2 (追踪指定文件 )
git add <fileName>
2.形成一个提交
方式1
git commit //然后在vim中输入提交信息
方式2
git commit -m <commitMessage>
3.修改上次提交
git add <fileName>
git commit --amend //追加提交
4.重置
软回退(修改内容还在暂存区)
git reset --soft <commit-Id>
硬回退(所有修改被清除)
git reset --hard</font> <commit-Id>
5.分支操作
git branch -v //查看分支
git branch <branchName> //创建新的分支
git checkout <branchName> // 切换到某一分支
git checkout -b <branchName> // 切到一个新的分支
git branch -D <branchName> // 删除分支,不可恢复
git checkout -b new_feature master // 基于master分支切出一个新分支
git checkout -b new_feature origin/master // 基于 远程master分支切出一个新分支
6.推送到代码服务器上
方式1
git push origin HEAD:<remoteBranch>
HEAD 本地当前分支
remote branch 远程仓库分支名
方式2 (同时设置push的默认分支)
git push --set-upstream origin <remote branch>
7 origin 远程仓库别名
添加一个新的远程仓库
git remote add <repertoryName> <url>
重新设置服务器地址
git remote set-url <repertoryName> <url>
8.变基
git pull --rebase origin master
如果在变基过程中遇到冲突:
解决冲突的步骤
-
修改冲突文件
-
追踪冲突文件
git add <confilct file>
-
继续变基
git rebase --continue
-
重复上面过程
变基过程如图所示
以后我们会说明,这个图的理解是不对的
9 合并时解决冲突
-
修改冲突文件
-
追踪冲突文件
git add <confilct file>
-
形成新的提交
git commit
10 stash用法
-
未提交到本地仓库时,如果修改的代码和远程仓库冲突,需要先stash.
-
拉取代码之后再pop出来。
git stash//隐藏 git stash pop//弹出之前的隐藏 git stash clear//清除
stash 的使用场景
开发到一半 需要打包
11 删除远程分支
git push origin --delete <BranchName>
12 设置track的远程分支
git branch --set-upstream-to=origin/<remoteBranchName> <localBranchName>
13 标签
查看所有标签
git tag
打标签
git tag <tagname>
打标签并添加具体内容
git tag -a <tagname> -m "标签信息"
查看标签内容
git show <tagname>
推送标签到远程仓库上
git push origin <tagname>
删除本地标签
git tag -d <tagname>
删除远程标签
git push origin :refs/tags/<tagname>
14. 查看commit 中的内容
查看当前commit 中的内容
git show
查看指定commit id 中的内容
git show <commit-hash-id>
仅显示文件名
git show <commit-hash-id> --name-only
15 git --help
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
grow, mark and tweak your common history
branch List, create, or delete branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects