Git基础补充

Git Basics【Git基础补充】

Getting a Git Repository【创建Git仓库】
一般有两个典型的方式创建一个Git仓库:

  1. 初始化一个目录作为一个新仓库;
  2. 克隆一个远程仓库;

Short Status【简洁状态】

If you run git status -s or git status --short you get a far more simplified output from the command:
在这里插入图片描述
左边字母代表暂存区状态,右边字母代表工作区状态。
图中各个字母说明如下:

  1. A-代表已经放入暂存区的新增文件;
  2. M-代表已经修改的文件,左边绿色的M代表修改已经暂存,右边红色的M代表未暂存的已修改;
  3. ?-代表未追踪的新增文件;

git status查看如下:
在这里插入图片描述
进一步说明:

  1. readme是执行了git add后的新文件;
  2. test1是修改后执行了git add,然后又修改过的文件;
  3. test2是修改后未执行git add的文件;
  4. readme2是未追踪的新建文件。

Ignoring Files【忽略文件】
在项目根目录下,添加.gitignore文件,里面按照规则编写需要被Git忽略的文件格式。例如编写如下内容:

*.[oa]
*~

#忽略自身??
.gitignore

Undoing Things【撤销】

1)git commit --amend指令

有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了。 此时,可以运行带有 --amend 选项的提交命令尝试重新提交。

git commit --amend

执行这个命令之前,我们可以git add等操作,然后重新提交,相当于在上一次提交基础上追加一些内容。注意此操作不可恢复,也没有历史记录。本次提交会覆盖上一次提交。

2)Unstaging a Staged File【撤销暂存区】

git reset HEAD <file>...

It’s true that git reset can be a dangerous command, especially if you provide the
–hard flag.

3)Unmodifying a Modified File【撤销工作区】

git checkout -- <file>...

这个撤销是基于暂存区。

Working with Remotes【远程操作】

1)远程仓库管理

git remote [-v]
git remote add <shortname> <url>
git remote rename old new
git remote rm <shortname>

2)Fetching and Pulling from Your Remotes【拉取代码】

git fetch <remote>
git pull <remote>

3)Pushing to Your Remotes【推送代码】

git push <remote> <branch>

4)Inspecting a Remote【查看某个远程仓库】

git remote show <remote>

Tagging【标签操作】

1)Listing Your Tags【查询标签】

git tag
git tag -l "v1.8.5*"

2)Creating Tags【创建标签】
Git supports two types of tags: lightweight and annotated.
轻量标签和附注标签

#1.Annotated Tags 
git tag -a v1.0 -m "my version v1.0"

#2.Lightweight Tags
git tag v1.0-lw

#3.查看标签信息
git show v1.0

3)Tagging Later【追加标签】

git tag -a v1.2 9fceb02

4)Sharing Tags【共享标签】

git push <remote> <tagname>
git push <remote> --tags

5)Deleting Tags【删除标签】

git tag -d <tagname>

两种方法同步到远程仓库:

git push <remote> :refs/tags/<tagname>
git push <remote> --delete <tagname>

6)Checking out Tags【检出标签】

git checkout <tagname>
git checkout -b <branchname> <tagname>

Git Aliases【Git别名】

1)可能会需要的别名
If you don’t want to type
the entire text of each of the Git commands, you can easily set up an alias for each command using
git config. Here are a couple of examples you may want to set up:

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

2)取消暂存

$ git config --global alias.unstage 'reset HEAD --'

此时,以下两个命令等同

$ git unstage fileA
$ git reset HEAD -- fileA

3)查看最后一次提交信息

$ git config --global alias.last 'log -1 HEAD'

如此只需要输入
$git last

猜你喜欢

转载自blog.csdn.net/weixin_43298913/article/details/104228691