Git常用命令


打标签实例

小精灵商户通创建:

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:password@github.com/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 <zhangxiaoning@avicare.cn>
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 "zhang721588@163.com"
    查看修改后的账号:
    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 "zhangxiaoning@avicare.cn "
  • 切换账号:zxn721588
    git config --global user.name "zxn721588"
    git config --global user.email "zhang721588@163.com"
    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 <file>..." to update what will be committed)
  (use "git checkout -- <file>..." 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 <file>/暂存需要提交的文件
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 git@github.com:xiayongchao/redis.git
   a939a1e..13e60ac  master -> master

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

推荐阅读更多精彩内容