git基本使用

1.本地安装git: https://jingyan.baidu.com/article/9f7e7ec0b17cac6f2815548d.html

2.申请git账号: https://github.com/

3.基本配置: git config --global username "your name"

      git config --global useremail "your email address"

4.基本使用

  

  => workspace: 工作区

  => index/stage: 暂存区

  => repository: 仓库区(或本地仓库)

  => Remote: 远程仓库 

(1)新建git代码库:

  git init:在已有项目的目录下新建git代码库

  git init [project name]:新建一个项目,并初始化为git代码库

  git clone [url]:下载一个项目(默认为仓库名称) 

  git clone [url] [name]   下载项目并命名

( ! )查看信息

  git status  查看工作区变更的文件

  git log   显示当前分支的版本(历史)记录

  git log -p  显示当前分支的版本记录及详情

  git log --oneline  一行显示一个记录 

  git log --oneline --graph --all   图形查看所有分支情况

  git diff  显示暂存区与工作区的代码差异

  git diff --staged  显示代码库与暂存区的代码差异

  git diff  --cached [file]  显示暂存区与上一次commit的代码差异

  git diff HEAD  显示工作区当前分支最新commit的代码差异

(2)添加文件到暂存区

  git add [file1] [file2]...:添加指定文件到git暂存区

  git add [dir]:添加指定目录到git暂存区,包含其子目录

  git add .  :添加当前目录下的所有文件到暂存区

(3)提交代码

  git commit -m "message"  提交暂存区到git代码库

  git commit [file1] [file2]... -m "message"  提交指定文件到git代码库

  git commit -a  提交工作区自上次commit之后变化,直接到git代码库

(4)标签

  git tag  列出所有tag

  git tag [tagName]  新建一份tag在当前的commit

  git tag [tagName] [commit ID]  新建一个tag到指定的commit

  git tag -d [tagName]  删除本地tag

  git push origin :refs/tags/[tagName]  删除远程tag

(5)回到历史版本

  git checkout [commit ID]  回到第commit ID版本

  git checkout [tagName]  回到第tagname 版本

  git checkout -    回到上一个版本

(6)分支

  git branch [branchName]  在当前版本创建分支

  git merge [branchName]  将branchName并入当前分支

  git checkout [branchName]  切换到branchName分支

 (7)远程仓库

  git remote add [name] [url]  添加一个远程仓库并命名

  git push -u [远程仓库名] [branchName]  上传代码到指定远程仓库和分支

  git pull    取回远程仓库代码

  git pull [remote] [branchName]  

 关于git pull报错:refusing to merge unrelated histories

解决办法:git pull origin master --allow-unrelated-histories

error: failed to push some refs to “...” 问题

git pull --rebase origin master

git push -u origin master

别人的总结

原文地址:https://www.cnblogs.com/lianchenxi/p/9617864.html