git常用命令及报错记录

全局设置:

git config —global user.name “your name”
git config —global user.email [email protected]

初始化:

git init

将档案加入git 中(track files):

git add <filename>

当前目录全部track:

git add .
git commit -m “信息”

分支部分:

新建:

git checkout -b <branch_name>

合并:

git checkout master
git merge <branch_name>

重命名:

  • 非当前分支:

    git branch -m <oldname> <newname>
    
  • 当前分支:

    git branch -m <newname>
    

删除:

git branch -D <branch_name>

查看所有分支:

git branch

Origin更换:

本地项目新建后,去除旧链接,建立新链接,在终端输入:

git remote -v
git remote rm origin
git remote -v
git remote add origin [email protected]:你的github用户名/your-project
git push -u origin master

其他:

取消stage:

git reset HEAD <file>

撤销commits :

git revert HEAD

修改commits:

git commit --amend

对于需要删除的commit,遵循向前的原则,继续修改,然后commit,而不是删除之前的commit

取消git init:

rm -rf .git

报错记录:

Updates were rejected

将本地的repos push到GitHub上时,使用git push origin master,报错:

error: failed to push some refs to 'https://github.com/XXX/YYY.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.

这时,在终端执行如下代码:

git push -f origin master
error: src refspec master does not match any.

新建了new project后,与GitHub上的origin相关联,运行如下代码后,报错:

git remote add origin https://github.com/XXX/YYY.git
git push -u origin master

error: src refspec master does not match any.
error: failed to push some refs to 'https://github.com/XXX/YYY.git

这是由于目前的project中,并没有git add任何文件,也没有任何commit,类似git版本控制中没有任何东西,是空的,所以会报错,解决方法也很简单,终端运行如下代码:

git add .
git commit -m "new project created"
git push -u origin master