Git笔记-git命令篇

221 阅读3分钟

Git命令

1. 仓库初始化git init

2. 追踪文件 git add 文件名

3. 查看状态 git status  /  git status -s #精简显示

4. 提交到仓库git commit -m '描述内容'

5. 跳过暂存区 git commit -a -m '描述内容'

6. 一次性添加到暂存区 git add .

7. 撤销对文件的修改 git checkout -- 文件名

8. 移除暂存文件 git reset HEAD 文件名get reset HEAD . #移除全部文件

9. 删除文件 

   #从Git仓库和工作区中同时移除 index.js文件 git rm -f index.js

   #只从Git仓库中移除index.css , 但保留工作区中的index.css git rm --cached index.css

10. 查看历史记录

   #按时间先后顺序列出所有的提交历史,最近的提交在最上面 git log

   #只展示最新的两条提交历史,数字可以按需进行填写 git log -2

   #在一行上展示最近两条提交历史的信息 git log -2 --pretty=oneline

   #在一行上展示最近两条提交历史信息,并自定义输出的格式,  &h 提交的简写哈希值 %an 作者名字 %ar 作者修订日志 %s 提交说明 git log -2 --pretty=fromat:"%h | %an | %ar | %s"

11.回退到指定版本

   #在一行上展示所有的提交历史    git log --pretty=oneline

   #使用 git reset --hard 命令,根据指定的提交 ID 回退到指定版本      git reset --hard

   #在旧版本中使用 git reflog --pretty=oneline 命令, 查看命令操作历史     git reflog

   #再次根据最新的提交 ID,跳转到最新的版本      git reset --hard <CommitID>

12. 上传到云仓库

   #1.查看当前的状态 git status #必须是 nothing状态 

   #2.创建链接关系 git remote add origin https://gitee.com/zzweb-61.git

   #3.推送代码 git push -u origin  master

   #4.再次推送代码  git push

13. 移除链接 git remote remove origin

13. 重新设置远程仓库地址 git remote set-url origin https://gitee.com/zzweb-61.git

14. 生成ssh-key  ssh-keygen -t rsa -b 4096 -C "your_email@example.com" #敲3次回车

15.测试ssh是否配置成功  ssh -T git@gitee.com

16. 克隆代码   git clone Url地址

17. 克隆指定分支   git clone -b 分支名  地址

18. 查看分支列表  git branch

19 创建分支 git branch 分支名字

20. 切换分支 git checkout 分支名字

21. 快速创建并切换分支 git checkout -b 分支名字

22. 合并分支 git merge 分支 #ps 注意一定要是nothing状态才可以合并

23. 解决合并冲突   git add .   git commit -m '解决了冲突'

24. 将本地分支推送到远程仓库

   # -u 表示把本地分支和远程分支进行关联,只在第一次推送的时候需要带 -u 参数 

git push -u 远程仓库的别名 本地分支名称:远程分支名称

# 实际案例 git push -u origin userinfo:info

   # 如果希望远程分支的名称和本地分支名称保持一致,可以对命令进行简化 

git push -u origin userinfo

25. 查看远程仓库分支列表

git remote show 远程仓库名字 # 一般都是origin

26.拉取最新的代码

git pull #拉取当前分支最新的代码

git pull origin 分支名字 #拉取指定分支的最新代码

27. 删除远程分支

   # 删除远程仓库中,指定名称的远程分支  git push 远程仓库名称 --delete 远程分支名称

   # 示例   git push origin --delete pay

28. 删除本地分支

git branch -d 分支名称

git branch -D 分支名称 #强制删除

29.强制推送代码(慎用!!!)

git push -f #会覆盖远程仓库的代码

1.打开用户目录, 创建 .bashrc文件   

touch  ~/.bashrc  ~ 当前文件根目录

2. 在 .bashrc 文件中输入内容

3. 执行 source ~/.bashrc

4. 解决GitBash乱码问题 

git config --global core.quotepath false

${git_home}/etc/bash.bashrc  文件最后加入下面两行

export LANG = 'zh_CN.UTF-8'
export LC_ALL = "zh_CN.UTF-8"

error: failed to push some refs to 'https://gitee.com.git'  
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

30 本地提交后更新被拒绝