Git常用命令


找回来未提交的版本代码

git reflog

8db5801 (HEAD -> xjl_settlement) HEAD@{0}: reset: moving to 8db580115fcee900dbf6c2201c30d8b75419c010
8db5801 (HEAD -> xjl_settlement) HEAD@{1}: reset: moving to 8db580115fcee900dbf6c2201c30d8b75419c010
8db5801 (HEAD -> xjl_settlement) HEAD@{2}: reset: moving to 8db580115fcee900dbf6c2201c30d8b75419c010
bfea408 HEAD@{3}: commit: <8C><96><84><81>
8db5801 (HEAD -> xjl_settlement) HEAD@{4}: reset: moving to 8db580115fcee900dbf6c2201c30d8b75419c010
8db5801 (HEAD -> xjl_settlement) HEAD@{5}: reset: moving to 8db580115fcee900dbf6c2201c30d8b75419c010
8db5801 (HEAD -> xjl_settlement) HEAD@{6}: reset: moving to 8db580115fcee900dbf6c2201c30d8b75419c010
8db5801 (HEAD -> xjl_settlement) HEAD@{7}: reset: moving to 8db580115fcee900dbf6c2201c30d8b75419c010
644ca6a HEAD@{8}: reset: moving to 644ca6a7602243580bd7338d2b06fdb7d0c3473a
8db5801 (HEAD -> xjl_settlement) HEAD@{9}: reset: moving to 8db580115fcee900dbf6c2201c30d8b75419c010
ebe1f4b HEAD@{10}: commit: bug fix
644ca6a HEAD@{11}: commit: <88><87><86><87><91><9D>
8db5801 (HEAD -> xjl_settlement) HEAD@{12}: commit: <85><8D><96><84><89><88><9C>1.40
c3af696 (origin/xjl_settlement) HEAD@{13}: commit: <8D><87>androidx:<8F><8A><8C><96><84><89><88><9C>1.40
5651f53 HEAD@{14}: commit: <9E><8A><8A><83><9D>.

我们要找回我们最晚的提交,只需要做如下操作:

git reset --hard ebe1f4b 

撤销已提交未推送的版本

方法一


查看未推送的提交版本号

git cherry -v

执行后效果

+ 8db580115fcee900dbf6c2201c30d8b75419c010 配置善版本1.40
+ 644ca6a7602243580bd7338d2b06fdb7d0c3473a 到账过滤了金额
+ ebe1f4b1a74760604c21ff37eadf33c757fc7cb7 bug fix

打标签实例

小精灵商户通创建:

git tag -a v1.26.3 -m '小精灵商户通v1.26.3'
git push origin v1.26.3
git tag

删除标签.

git tag -d v1.26.2
git push origin --delete v1.26.2

常见错误解决办法

  • 如果push遇到在输入密码是熟错后,就会报这个错误fatal: Authentication failed for
    解决办法:
git config --system --unset credential.helper
  • git命令每次提交都要输入用户名和密码的解决办法
    1,删除当前远程仓库
    git remote rm origin
    2,携带用户密码添加远程仓库
    git remote add origin https://username:[email protected]/username/test.git
    3,提交代码到主干
    git push origin master
    4,提交成功
D:\ZxnCode\demos\LoadingDemo>git push origin master
Enumerating objects: 81, done.
Counting objects: 100% (81/81), done.
Delta compression using up to 4 threads
Compressing objects: 100% (42/42), done.
Writing objects: 100% (60/60), 18.36 KiB | 2.04 MiB/s, done.
Total 60 (delta 9), reused 0 (delta 0)
remote: Resolving deltas: 100% (9/9), completed with 7 local objects.
To https://github.com/zhang721688/LoadingDemo.git
   fba3f30..7a5430d  master -> master

打标签

https://blog.csdn.net/qq_35495763/article/details/85524175
同大多数 VCS 一样,Git 也可以对某一时间点上的版本打上标签。人们在发布某个软件版本(比如 v1.0 等等)的时候,经常这么做。本节我们一起来学习如何列出所有可用的标签,如何新建标签,以及各种不同类型标签之间的差别。

  • 列出已有的标签
    git tag
C:\ZxnWork\zhonghangyitong\aifuke>git tag
v1.0
  • 列出指定版本系列标签
$ git tag -l 'v1.4.2.*'
v1.4.2.1
v1.4.2.2
v1.4.2.3
v1.4.2.4
  • 新建标签附注的
    git tag -a v1.4 -m 'xxxx1.4'
C:\ZxnWork\zhonghangyitong\aifuke>git tag -a v1.0 -m 'hkzjv1.0'
  • 查看相应标签的版本信息
    git show v1.0
C:\ZxnWork\zhonghangyitong\aifuke>git show v1.0
tag v1.0
Tagger: zxn 
Date:   Mon Feb 25 14:59:21 2019 +0800
'hkzjv1.0'
  • 把标签传送到远端服务器上
    git push origin v1.5
C:\ZxnWork\zhonghangyitong\aifuke>git push origin v1.0
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 161 bytes | 161.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To http://47.94.38.6/chengongshun/aifuke.git
 * [new tag]         v1.0 -> v1.0
  • 删除远程库中标签
git tag -d v1.21
git push origin --delete v1.21
  • 获取到tag对应的代码
    就可以获取到tag对应的代码了,但因为tag相当于一个快照,它的代码是不能被修改的。
git checkout tag_name

分支操作

  • 查看所有远程分支
git branch -r
  • 分支的新建
    方式一:
    执行命令
git checkout -b afk-walk

控制台输出结果

C:\MyWork\zhonghangyitong\aifuke>git checkout -b akf-walk
Switched to a new branch 'akf-walk'

方式二:

git branch afk-walk
git checkout afk-walk

提交分支

git commit -a -m 'added a new footer [issue 53]'

将分支推送到远程仓库

git push --set-upstream origin akf-walk

提交后的结果

remote: Resolving deltas: 100% (2727/2727), done.
remote:
remote: To create a merge request for akf-walk, visit:
remote:   http://47.94.38.6/chengongshun/aifuke/merge_requests/new?merge_request%5Bsource_branch%5D=akf-walk
remote:
To http://47.94.38.6/chengongshun/aifuke.git
 * [new branch]      akf-walk -> akf-walk
Branch 'akf-walk' set up to track remote branch 'akf-walk' from 'origin'.
  • 分支的切换
    切换到主分支
git checkout master

切换到akf-walk分支

git checkout akf-walk
  • 分支的合并
    接下来我们将akf-walk分支合并到master分支
    首先切换到master分支
    git checkout master
C:\MyWork\zhonghangyitong\aifuke>git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

合并
git merge akf-walk

C:\MyWork\zhonghangyitong\aifuke>git merge akf-walk
Updating 4b353f9..c264a65
Fast-forward
 .idea/caches/build_file_checksums.ser              | Bin 694 -> 694 bytes
 app/src/main/AndroidManifest.xml                   |   7 ++--
 .../afuke/home/aviation/sports/StepActivity.java   |  36 +++++++++++++++++++++
 app/src/main/res/layout/activity_step.xml          |   9 ++++++
 4 files changed, 49 insertions(+), 3 deletions(-)
 create mode 100644 app/src/main/java/com/afk/afuke/home/aviation/sports/StepActivity.java
 create mode 100644 app/src/main/res/layout/activity_step.xml

查看项目的分支(包括本地和远程)
git branch -a
可以看到当前分支处于akf-walk分支上

C:\MyWork\zhonghangyitong\aifuke>git branch -a
* akf-walk
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/akf-walk
  remotes/origin/master
  • 分支删除
    删除本地分支akf-walk
    git branch -d akf-walk
    删除失败,因为我们处于akf-walk上
C:\MyWork\zhonghangyitong\aifuke>git branch -d akf-walk
error: Cannot delete branch 'akf-walk' checked out at 'C:/MyWork/zhonghangyitong/aifuke'

切换到master分支上
git check out master

C:\MyWork\zhonghangyitong\aifuke>git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

继续删除本地分支akf-walk
git branch -d akf-walk

C:\MyWork\zhonghangyitong\aifuke>git branch -d akf-walk
Deleted branch akf-walk (was c264a65).

删除远程分支
git push origin --delete akf-walk

C:\MyWork\zhonghangyitong\aifuke>git push origin --delete  akf-walk
To http://47.94.38.6/chengongshun/aifuke.git
 - [deleted]         akf-walk

查看删除后分支情况
git branch -a
可以看到当前只剩下master分支

C:\MyWork\zhonghangyitong\aifuke>git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  • 分支的合并遇到冲突

https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6

  • 分支重命名
//git branch -m 旧分支名称 新分支名称
git branch -m aifke-step aifuke-step

切换远程仓库地址

  • 方式一:修改远程仓库地址
    【git remote set-url origin URL】 更换远程仓库地址,URL为新地址。
git remote set-url origin https://gitee.com/qqjd/wdy_android.git

【git remote -v 】查看远程仓库的地址

git remote -v 
  • 方式二:先删除远程仓库地址,然后再添加
    【git remote rm origin】 删除现有远程仓库
    【git remote rm oschina】 删除现有远程仓库
    【git remote add origin url】添加新远程仓库
git remote rm origin
git remote rm oschina
git remote add origin https://gitee.com/qqjd/wdy_android.git

【git remote -v 】查看远程仓库的地址

git remote -v 

查看当前账号以及修改

当使用不同的git账号来更新或提交代码的时候需要切换git的用户.
git config user.name
查看自己的用户名
git config user.email
查看自己的邮箱地址
git config --global user.name "xxx"
修改自己的用户名
git config --global user.email "xxx"
修改自己的邮箱地址


多账号切换

  • 切换账号:zxn721588
    查询用户信息,输入命令后连续回车.
    git config --list
    查看当前账号:
    git config user.name
    git config user.email
    修改当前账号:
    git config --global user.name "zxn721588"
    git config --global user.email "[email protected]"
    查看修改后的账号:
    git config user.name
    git config user.email
    如果push遇到在输入密码是熟错后,就会报这个错误fatal: Authentication failed for
    解决办法:
    git config --system --unset credential.helper
  • 切换账号:zxn
    git config --global user.name "zxn"
    git config --global user.email "[email protected] "
  • 切换账号:zxn721588
    git config --global user.name "zxn721588"
    git config --global user.email "[email protected]"
    git config user.name
    git config user.email

常用命令

git init
建立git仓库
git add .
将项目的所有文件添加到仓库中
git commit -m “the commit message"
提交已经被add进来的改动.
git commit -a
会先把所有已经track的文件的改动add进来,然后提交(有点像svn的一次提交,不用先暂存). 对于没有track的文件,还是需要git add一下.
git commit --amend
增补提交. 会使用与当前提交节点相同的父节点进行一次新的提交,旧的提交将会被取消.


git push
push your new branches and data to a remote repository.
git push [alias] [branch]
将会把当前分支merge到alias上的[branch]分支.如果分支已经存在,将会更新,如果不存在,将会添加这个分支.
如果有多个人向同一个remote repo push代码, Git会首先在你试图push的分支上运行git log,检查它的历史中是否能看到server上的branch现在的tip,如果本地历史中不能看到server的tip,说明本地的代码不是最新的,Git会拒绝你的push,让你先fetch,merge,之后再push,这样就保证了所有人的改动都会被考虑进来.
git push --set-upstream origin master
如果报错则指定分支推送
git push --set-upstream origin master

修改文件内容执行命令操作

1、查看代码的修改状态
git status

On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   src/main/java/com/xyc/redis/RedisApplication.java

no changes added to commit (use "git add" and/or "git commit -a")

2、git add /暂存需要提交的文件
git add .

$ git add src/main/java/com/xyc/redis/RedisApplication.java
warning: LF will be replaced by CRLF in src/main/java/com/xyc/redis/RedisApplication.java.
The file will have its original line endings in your working directory.

3、git commit/提交已暂存的文件
git commit

[master warning: LF will be replaced by CRLF in src/main/java/com/xyc/redis/RedisApplication.java.
The file will have its original line endings in your working directory.
13e60ac] 添加注释
warning: LF will be replaced by CRLF in src/main/java/com/xyc/redis/RedisApplication.java.
The file will have its original line endings in your working directory.
 1 file changed, 11 insertions(+)

4、git pull/先同步代码到本地
git pull

Already up-to-date.

5、git push origin <本地分支名>/再同步到服务器
git push origin 3am

$ git push origin master
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 904 bytes | 0 bytes/s, done.
Total 9 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To [email protected]:xiayongchao/redis.git
   a939a1e..13e60ac  master -> master

原文:https://blog.csdn.net/xyc_csdn/article/details/72976660

你可能感兴趣的:(Git常用命令)