Git常用命令

2018-08-09  本文已影响12人  CarlosLynn

打标签

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

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
C:\ZxnWork\zhonghangyitong\aifuke>git tag -a v1.0 -m 'hkzjv1.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'
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 push origin :refs/tags/标签名
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
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
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 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 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"
修改自己的邮箱地址


多账号切换

常用命令

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/查看代码的修改状态
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

上一篇下一篇

猜你喜欢

热点阅读