git的一些简单操作,自己留档。
基本操作
- 初始化版本库
git init
- 添加文件
git add <yourFile>
- 把添加的文件提交到版本库中
git commit -m "your comment"
commit可一次提交多个文件
- 查看commit记录
git log
- 版本回退
git reset --hard <yourVersionNumber>
- 查看版本库状态
git status
- 将版本库中文件换到工作区(回退单个文件到指定版本)
git checkout <commit-num> <file>
执行此命令后git status
会显示这个文件你没有提交。不想对该文件做任何修改,则应立刻提交
- 创建新分支
git branch <branchName>
- 切换到指定分支
git checkout <branchName>
- 关联远程库
git remote add origin git@github.com:yy2261/hello-world.git
- 推送本地内容到远程库
git push git@github.com:yy2261/hello-world.git <remoteBranch>:<localBranch>
- 获取远程库分支内容并合并到本地
git pull origin git@github.com:yy2261/hello-world.git <remoteBranch>:<localBranch>
- 移除对某文件的版本控制
git rm -f test.py
移除对该文件的版本控制,并删除该文件。
git rm --cached test.py
移除对该文件的版本控制,在本地保留该文件。
上述两种操作后都需对操作进行提交(commit)。
“Could not read from remote repository.Please make sure you have the correct access rights.”解决办法
问题:
出现这个问题是因为没有在github账号添加SSH key
解决办法:
- 在终端输入
ssh-keygen -t rsa -C "username"
,username为你git上的用户名 - 一路回车就生成了SSH key,存在id_rsa.pub文件中,cat一下显示出内容
- 登录自己的github帐号,在settings中选择SSH Keys选项,选择Add SSH Keys
- Title瞎写,Key复制id_rsa.pub中的内容
- 每次重装系统后需要重新进行这一步骤
“Please tell me who you are”解决办法
直接按照提示走就可以,输入你注册github时的用户名和邮箱
git config --global user.email "你的邮箱"
git config --global user.name "你的名字"