切换并追踪分支
git checkout -b gh_data_v1.2.6 --track origin/gh_data_v1.2.6
克隆指定分支
git clone -b gh_web git@git01.xxx.cn:NETxN/zx_cloud.git ./gh_web
创建版本库
git init
查看状态
git status
查看修改
git diff
可以查看工作区和版本库里面最新版本的区别
git diff HEAD -- <filename>
查看提交历史记录,以便确定要回退到哪个版本
git log
git log --pretty=oneline
查看分支合并图
git log --graph --pretty=oneline --abbrev-commit
回退版本
git reset --hard HEAD^
git reset --hard HEAD^^
git reset --hard <commit>
git reset --hard 3f4741f
查看命令历史记录,以便确定要回到未来的哪个版本
git reflog
版本库、工作区和暂存区概念
- 这个文件夹就是工作区
- .git目录是版本库
- git add 命令添加的文件进入暂存区
- git commit 命令提交的文件将当前暂存区内容提交到当前分支
添加文件,将文件添加到暂存区
git add
提交更改,将当前暂存区内容提交到当前分支
git commit
撤销工作区修改
git checkout -- <filename>
- file 自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态
- file 已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态
- 总之,就是让这个文件回到最近一次git commit或git add时的状态
撤销缓存区修改
git reset HEAD <filename>
- HEAD 表示最新的版本
- git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区
删除一个文件
git rm <filename>
创建SSH Key
ssh-keygen -t rsa -C "myemail@example.com"
- 生成 .id_rsa 私钥
- 生成 .id_rsa.pub 公钥
关联一个远程库
git remote add origin git@server-name:path/repo-name.git
推送master分支
git push origin master
push -u 参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,第一次使用
查看分支
git branch
创建分支
git branch <name>
切换分支
git checkout <name>
创建并切换分支
git checkout -b <name>
合并某分支到当前分支
git merge <name>
删除分支
git branch -d <name>
合并分支禁用fast-forward
git merge --no-ff
保存工作现场
git stash
查看保存的工作现场
git stash list
恢复工作现场
git stash apply
删除工作现场
git stash drop
恢复工作现场,并删除工作现场
git stash pop
强行删除分支
git branch -D <name>
查看远程库信息
git remote
-v 查看更详细信息
本地创建和远程分支对应的分支
git checkout -b branch-name origin/branch-name
本地分支和远程分支的关联
git branch --set-upstream branch-name origin/branch-name
打标签
git tag <name>
git tag -a <tagname> -m "blablabla... 可以指定标签信息
git tag -s <tagname> -m "blablabla... 可以用PGP签名标签
查看标签信息
git show <tagname>
查看所有标签
git tag
推送一个本地标签
git push origin <tagname>
推送本地全部标签
git push origin --tags
删除本地一个标签
git tag -d <tagname>
删除一个远程标签
git push origin :refs/tags/<tagname>
Git显示颜色
git config --global color.ui true
revert
错误提交或者push 使用revert 删除错误提交或push 并会产生一个新的commit 如果要删除revert 可以revert revert 即可
git revert <版本号>
e.g git revert 5a2328b
git pull
Merge into the current branch the remote branch next:
$ git pull origin next
This leaves a copy of next temporarily in FETCH_HEAD, but does not update any remote-tracking branches. Using remote-tracking branches, the same can be done by invoking fetch and merge:
$ git fetch origin
$ git merge origin/next
git push
git push origin master
Find a ref that matches master in the source repository (most likely, it would find refs/heads/master), and update the same ref (e.g. refs/heads/master) in origin repository with it. If master did not exist remotely, it would be created.
git push origin HEAD
A handy way to push the current branch to the same name on the remote.
git push mothership master:satellite/master dev:satellite/dev
Use the source ref that matches master (e.g. refs/heads/master) to update the ref that matches satellite/master (most probably refs/remotes/satellite/master) in the mothership repository; do the same for dev and satellite/dev.
This is to emulate git fetch run on the mothership using git push that is run in the opposite direction in order to integrate the work done on satellite, and is often necessary when you can only make connection in one way (i.e. satellite can ssh into mothership but mothership cannot initiate connection to satellite because the latter is behind a firewall or does not run sshd).
After running this git push on the satellite machine, you would ssh into the mothership and run git merge there to complete the emulation of git pull that were run on mothership to pull changes made on satellite.
git push origin HEAD:master
Push the current branch to the remote ref matching master in the origin repository. This form is convenient to push the current branch without thinking about its local name.
git push origin master:refs/heads/experimental
Create the branch experimental in the origin repository by copying the current master branch. This form is only needed to create a new branch or tag in the remote repository when the local name and the remote name are different; otherwise, the ref name on its own will work.