本文记录了在Git学习中纪录的一些要点,主要涉及Git日常使用的一些基础操作和简单使用。
首先,我们需要熟悉Git的几个操作区域:
Workspace: 工作区
Index / Stage: 暂存区
Respository: 本地仓库
Remote: 远程仓库
git init
命令。git add <file>
,注意,可反复多次使用,添加多个文件,若需添加所有,则可使用git add .
或git add -A
;git commit -m '<message>'
,完成。git commit -m 'message'
git commit <file1> <file2> ... -m '<message>'
git commit -a
git commit -v
git commit --amend -m '<message>'
git status
命令。git status
告诉你有文件被修改过,用git diff
可以查看修改内容。HEAD
指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id
。git log
可以查看提交历史,以便确定要回退到哪个版本。git reflog
查看命令历史,以便确定要回到未来的哪个版本。git checkout -- file
。git reset HEAD file
,就回到了场景1,第二步按场景1操作。git rm
用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。git remote add origin git@server-name:path/repo-name.git
;git push -u origin master
第一次推送master分支的所有内容;git push origin master
推送最新修改。git log --graph
命令可以看到分支合并图。git branch
git branch -r
git branch -a
git branch <branch name>
git checkout -b <branch>
git branch --track <branch name> <remote-branch>
git checkout <branch name>
git checkout -
git merge <branch name>
git branch -d <name>
git log --graph --pretty=oneline --abbrev-commit
git stash
一下,然后去修复bug,修复后,再git stash pop
,回到工作现场。git branch -D <name>
强行删除。git tag <name>
用于新建一个标签,默认为HEAD
,也可以指定一个commit id;git tag -a <tagname> -m "blablabla..."
可以指定标签信息;git tag -s <tagname> -m "blablabla..."
可以用PGP签名标签;git push origin <tagname>
可以推送一个本地标签;git push origin --tags
可以推送全部未推送过的本地标签;git tag -d <tagname>
可以删除一个本地标签;git push origin :refs/tags/<tagname>
可以删除一个远程标签。ssh-keygen
,此时需要输入两次密码,如果不需要设置密码,直接回车,默认生成的两个文件是:cat id_rsa.pub
,然后将公钥内容添加到服务器的~/.ssh/authorized_keys 文件中。