Git笔记

Git

账户配置

配置全局的用户名和邮箱,命令分别为

1
2
git config --global user.name "username"
git config --global user.email "email@example.com"

全局的用户名和邮箱会写到C:\Users\XXX下的 .gitconfig文件中。

查看全局的用户名和邮箱,命令分别为

1
2
git config --global user.name
git config --global user.email

注意这里的配置是全局配置,表示这台机器上所有的Git仓库默认都会使用这个配置。

为单一的仓库配置用户名和邮箱,命令分别为

1
2
git config user.name "username"
git config user.email "email@example.com"

查看单一的仓库配置的用户名和邮箱,命令分别为

1
2
git config user.name
git config user.email

配置完后,单一的仓库的用户名和邮箱会写到该仓库.git下的config文件中。

初始化

1
git init

加载全部文件到缓存区

1
git add .

加载指定文件到缓存区

1
git add index.html,test.html

提交文件

1
git commit -m "message"

推送到远程服务器

首次推送需要添加远程的仓库到配置

1
git remote add origin https://github.com/jiabisheng/jiabisheng.github.io.git

推送代码

1
git push origin master

然后会要求输入 github 的帐号和密码(不可见)

可能遇到的问题

! [rejected] master -> master (non-fast-forward)

原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去。

强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容

1
git push -f

update were rejected because the tip of your current branch is behind ….

原因是远程的的代码库比我本地的代码库要新。所以需要在远程代码库 pull 一次下来。再修改代码。再 push。

git master branch has no upstream branch

原因是没有将本地的分支与远程仓库的分支进行关联。

其他

拉取远程仓库到本地

1
git pull

查看指向的仓库

1
git remote -v

https://www.cnblogs.com/merray/p/6006411.html

https://blog.csdn.net/junli_chen/article/details/52623350

https://blog.csdn.net/hebbely/article/details/51858938

1.找个空目录执行git init

2.git add .添加全部文件

3.commit -m '版本信息'

4.git log查看提交记录

5.版本退回

git reset --hard HEAD 当前版本

git reset --hard HEAD^上一个版本

  • HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id
  • 穿梭前,用git log可以查看提交历史,以便确定要回退到哪个版本。
  • 要重返未来,用git reflog查看命令历史,以便确定要回到未来的哪个版本。