git常用命令

1.简历github仓库https://github.com/fulq1234/a.git
2.建立本地仓库。我在本地新建一个文件夹gitgit,打开文件夹,右键选择git bash here

git init

3.在gitgit下面新建一个文件夹adddd,

git add .

4. git仓库迁移,git remote更改源

  • git remote                                    #不带参数,列出已经存在的远程分支
  • git remote -v                                 #(-v是–verbose 的简写,取首字母)列出详细信息,在每一个名字后面列出其远程url
  • git remote add [shortname] [url]              #添加远程仓库
  • git remote rm [shorname]                #删除git
$ git remote -v

$ git remote add origin https://github.com/fulq1234/a.git

$ git remote -v
origin  https://github.com/fulq1234/a.git (fetch)
origin  https://github.com/fulq1234/a.git (push)

$ git remote add origin2 https://github.com/fulq1234/a.git

$ git remote -v
origin  https://github.com/fulq1234/a.git (fetch)
origin  https://github.com/fulq1234/a.git (push)
origin2 https://github.com/fulq1234/a.git (fetch)
origin2 https://github.com/fulq1234/a.git (push)

$ git remote add origin2 https://github.com/fulq1234/a.git
fatal: remote origin2 already exists.

$ git remote rm origin2

$ git remote add origin2 https://github.com/fulq1234/a.git

$ git remote -v
origin  https://github.com/fulq1234/a.git (fetch)
origin  https://github.com/fulq1234/a.git (push)
origin2 https://github.com/fulq1234/a.git (fetch)
origin2 https://github.com/fulq1234/a.git (push)


5. 拉取远程仓库的代码。
git pull origin master
6.提交本地代码
git add .
git commit -m ""
git push origin master
7.怎么解决冲突。如果远程的一个文件改变了,本地的也改变了。那么提交代码会冲突

$ git add .

$ git commit -m "bbb"
[master 908e9a5] bbb
 1 file changed, 2 insertions(+), 1 deletion(-)

$ git push origin master
To https://github.com/fulq1234/a.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/fulq1234/a.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.

方法1:
更新成最新代码
git pull origin master
再看冲突的那个文件,本地代码和远程仓库的代码都有。需要修改后提交。

方法2:git stash
$ git stash save "1"
Saved working directory and index state On master: 1

$ git stash list
stash@{0}: On master: 1

$ git stash show
 a.txt | 1 +
 1 file changed, 1 insertion(+)

$ git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/fulq1234/a
 * branch            master     -> FETCH_HEAD
   f01172d..55fa00a  master     -> origin/master
Updating f01172d..55fa00a
Fast-forward
 a.txt | 1 +
 1 file changed, 1 insertion(+)

再看冲突的那个文件,本地代码和远程仓库的代码都有。需要修改后提交。

$ git stash pop
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt

方法3:把冲突文件新建分支,把文件再合并,删除新建的那个分支

8.多个版本切换
git log 查看历史记录
--hard当前版本,--hard^上一个版本。
git reset --hard HEAD

版本切换:
git reset --hard c17177259e63a6f964b27be0a0b1ee6ad4882abd

9.分支
git branch iss53    #建一个分支名字叫iss53
git checkout iss53    #版本切换到分支iss53上面
如果想把master合并到iss53上。
第一步,切换到master 。
git checkout master
第二步,合并iss53
git merge iss53
10.删除分支
git branch -d hotfix        #删除分支hotfix

11.查看当前分支,删除远程分支

F:\workspace\project>git branch -a
* dev
  dev-20200309
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/dev-20200226
  remotes/origin/dev-20200309
  remotes/origin/dev-20200309-<96><83><85>
  remotes/origin/dev-PL<96>
  remotes/origin/master
  remotes/origin/rostering
F:\workspace\project>git push origin --delete dev-20200226
To http://git.hldev.com/hlhlo/zillion-wfm.git
 - [deleted]           dev-20200226

F:\workspace\project>git branch -a
* dev
  dev-20200309
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/dev-20200309
  remotes/origin/dev-20200309-<96><83><85>
  remotes/origin/dev-PL<96>
  remotes/origin/master
  remotes/origin/rostering
F:\workspace\project>git push origin --delete dev-20200309-疫情
To http://git.hldev.com/hlhlo/zillion-wfm.git
 - [deleted]           dev-20200309-疫情

F:\workspace\project>git branch -a
* dev
  dev-20200309
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/dev-20200309
  remotes/origin/dev-PL<96>
  remotes/origin/master
  remotes/origin/rostering
F:\workspace\project>git push origin --delete dev-20200309
To http://git.hldev.com/hlhlo/zillion-wfm.git
 - [deleted]           dev-20200309

F:\workspace\project>git branch -a
* dev
  dev-20200309
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/dev-PL<96>
  remotes/origin/master
  remotes/origin/rostering

 

 

 

 

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