git常用命令
新建一个文件夹test
1、git init
新建一个空的仓库
MrzhuangdeMacBook-Pro:test mrzhuang$ git init
Initialized empty Git repository in /Users/mrzhuang/Downloads/test/.git/
在本地文件夹中连接拉取远程仓库的代码时,需要新建一个本地仓库
2、 git status
查看状态
MrzhuangdeMacBook-Pro:test mrzhuang$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
3、 git add .
添加文件
4、 git commit -m '注释'
提交添加的文件并备注说明
MrzhuangdeMacBook-Pro:test mrzhuang$ git commit -m "first commit"
[master (root-commit) 2f48bf2] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 _个人学习计划打印20220312.pdf
5、 git remote add origin 仓库地址
连接远程仓库
这个origin可以是其他的单词如test,说明这次的建立连接的远程仓库名字是test
MrzhuangdeMacBook-Pro:new_rpc mrzhuang$ git remote add origin https://github.com/zhuang0112/new_rpc.git
https://github.com/zhuang0112/new_rpc.git
是远程仓库地址
6、 git push -u origin master
将本地仓库文件推送到远程仓库
7、 git log
查看变更日志,包含所有的提交版本
8、 git reset --hard
版本号前六位 回归到指定版本
- log中的1a7278为提交版本号的前六位
MrzhuangdeMacBook-Pro:new_rpc mrzhuang$ git log
commit 1a7278f372c44dd53e21d5ca112f8512e5a7b2b7 (HEAD -> main, origin/main)
Author: mrzhuang <862627527@qq.com>
Date: Thu Mar 31 16:52:56 2022 +0800
12、 git branch
查看分支
13、 git branch newname
创建一个叫newname的分支
14、 git checkout newname
切换到叫newname的分支上
15、 git branch -d branchname
删除本地分支
16、git branch -D branchname
强制删除本地分支
17、git push -d origin branchname
删除远程分支
18.、git merge newname
把newname分支合并到当前分支上
19、 git branch -m oldName newName
重命名本地分支
20、git push origin newName
推送新的分支到远程仓库
21、 git pull -p
同步远程分支到本地
22、git pull origin master
将master分支上的内容拉到本地上
23、 git pull origin master --allow-unrelated-histories
解决:
fatal: refusing to merge unrelated histories
24、git show commit-id
查看指定提交版本的修改信息
25、git remote set-url origin https://<your_token>@github.com/<USERNAME>/<REPO>.git
给当前本地仓库设置token登录,下次就不需要输入token了
MrzhuangdeMacBook-Pro:test mrzhuang$ git remote set-url origin https://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@github.com/zhuang0112/new_rpc.git
MrzhuangdeMacBook-Pro:test mrzhuang$ git push origin master
To https://github.com/zhuang0112/new_rpc.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://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@github.com/zhuang0112/new_rpc.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.
26、 git pull --rebase origin master
将本地分支代码与远程分支master代码合并与git pull origin master
目的都是代码合并,就是合并方式不同。
解决:
error: failed to push some refs to 'https://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@github.com/zhuang0112/new_rpc.git'
遇见错误是本地仓库缺少readme.md文件,需要远程拉取。
MrzhuangdeMacBook-Pro:test mrzhuang$ git pull --rebase origin master
warning: no common commits
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 17 (delta 0), reused 14 (delta 0), pack-reused 0
Unpacking objects: 100% (17/17), done.
From https://github.com/zhuang0112/new_rpc
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
First, rewinding head to replay your work on top of it...
Applying: first commit
27、git config user.name
查看用户名
MrzhuangdeMacBook-Pro:renren-fast-vue mrzhuang$ git config user.name
mrzhuang
28、git config user.password
查看用户密码
MrzhuangdeMacBook-Pro:renren-fast-vue mrzhuang$ git config user.password
zhuang123
29、git config user.email
查看邮箱
MrzhuangdeMacBook-Pro:renren-fast-vue mrzhuang$ git config user.email
862627527@qq.com
30、git config --global user.name newName(新的用户名)
设置新的用户名
31、git config --global user.password newPassword(新的密码)""
设置新的密码
32、git config --global user.email newEmail(新的邮箱地址)
设置新的邮箱
33、git config --list
查看所有的git设置
问题:
在进行提交的时候,有时提交的是git clone的文件,文件上会出现白色的箭头,比如clone的文件是test。解决方法是:
首先去掉test文件里的.git文件。
rm -rf .git
然后运行一下的代码:
git rm --cached test
清除缓存的test
git add test
添加入缓存
git commit -m "解决白色箭头"
提交到本地仓库
git push test(默认origin) master
推送到远程仓库
33、 git push --set-upstream origin newbranch
To push the current branch and set the remote as upstream
34、gitk git排查工具