git的基本操作

Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的 项目,在我们的日常开发中,经常要与人合作写项目,这时git是非常的重要,熟练掌握 git的基本命令成为开发的基本功。以下主要介绍了git的基本操作命令以及与github的交互命令。

Git基本操作

1. 创建一个文件夹test,并在test下创建一个a.md文件,然后在git bash下用cd命令进入到刚才创建的test文件夹,
2. 初始化 git 仓库
	git init
3. 查看状态
	git status
4. 把a.md文件添加到本地Git仓库
	git add a.md
5. 设置下自己的用户名与邮箱
	git config —global user.name "JasonLi"   
	git config —global user.email "lijingxuan92@126.com"  
6. 正式提交文件
	git commit -m ‘first commit’
		-m 代表是提交信息
7. 查看所有产生的 commit 记录
	git log
8、1)把本地test仓库与GitHub上的任意仓库进行关联(切换到 test 目录)
		git remote add origin git@github.com:JasonLi-cn/test.git 
		git@github.com:JasonLi-cn/test.git    远程仓库的ssh地址
		origin 								  远程仓库ssh地址的别名,可以是任意名字
		一个本地仓库可以多个远程仓库进行关联,但每个远程仓库的别名不能一样

	2) 查看我们当前仓库与那些远程仓库关联
	git remote -v
	9、1)向远程仓库进行代码提交(前提是你已经配置好公钥和密钥)
	git push origin master
		origin     相关联远程仓库的别名
   
	2)提交时,可能出现的问题:
	$ git push origin master  
	To github.com:JasonLi-cn/test.git  
	! [rejected]        master -> master (fetch first)  
	error: failed to push some refs to 'git@github.com:JasonLi-cn/test.git'  
	hint: Updates were rejected because the remote contains work that you do  
	hint: not have locally. This is usually caused by another repository pushing  
	hint: to the same ref. You may want to first integrate the remote changes  
	hint: (e.g., 'git pull ...') before pushing again.  
	hint: See the 'Note about fast-forwards' in 'git push --help' for details.

	3)说明在远程仓库中存在本地仓库没有的文件,所以需要先pull操作
	git pull origin master 
		此时可能会遇到的问题:
	$ git pull origin master  
	From github.com:JasonLi-cn/test  
	* branch            master     -> FETCH_HEAD  
	fatal: refusing to merge unrelated histories

	4)解决方法
	git pull origin master --allow-unrelated-histories  
		然后就可以 push了