git创建新分支,合并分支,切换分支,仓库迁移

git创建新分支
  1. 本地创建分支
    $ git checkout -b feature-0401
  2. 查看分支
    $ git branch
  3. 提交该分支到远程仓库
    $ git push origin feature-0401
  4. 拉取分支,提示错误
    $ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
    git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> feature-0401
  1. 把远程分支和本地分支关联
$ git branch --set-upstream-to=origin/feature-0401 feature-0401
Branch 'feature' set up to track remote branch 'feature' from 'origin'.
  1. 再去拉取分支
$ git pull
Already up to date.  #拉取成功
git 切换分支(远程已存在分支,pull完之后直接checkout即可)
  1. 查看所有远程仓库分支,确定当前分支与要合并的分支
    git branch -a
  2. 切换到远程dev分支
    git checkout -b dev origin/dev
git checkout -b  dev origin/dev
Updating files: 100% (1762/1762), done.
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
Switched to a new branch 'dev'
git分支合并
  1. 回退上一个版本
    $ git reset --hard HEAD^
    $ git reset --hard 667c54ca6fbfd63928da7fdb0592e0599ea48b79

  2. 对比master
    $ git diff master

  3. 把本地master合并到当前分支
    $ git merge master

  4. 查看日志是否合并成功
    $ git log

  5. 强制push到当前分支对应的远程仓库
    $ git push -f

  6. 至此分支合并成功

git合并其它远程的分支到当前分支
  1. 运行 git fetch 命令,以确保你的本地分支已获取远程分支的最新更新
$ git fetch
remote: Enumerating objects: 22, done.
   4b96281a..a6e87f74  feature    -> origin/feature
   84310e1f..68d4cf52  publish    -> origin/publish
  1. 运行 git merge 命令
$ git merge origin/feature
	...
#显示冲突内容
Automatic merge failed; fix conflicts and then commit the result.
  1. 手动合并冲突
  2. 最后正常 add commit push 即可
git fatch 与git pull 区别:

#git pull相当于是从远程获取最新版本并merge到本地
git pull origin master :相当于git fetch 和 git merge

git fetch从远程获取最新的版本到本地的test分支上
之后再进行比较合并
git fetch origin master:tmp
git diff tmp
git merge tmp
git push

撤销 commit

git reset --soft HEAD~1

删除错误git add的内容

将file退回到unstage区
git reset HEAD filename

忽略已跟踪文件的改动

git update-index --assume-unchanged filename
恢复跟踪
git update-index --no-assume-unchanged FLIE

git仓库迁移

方式一: 简单粗暴

  1. 从原仓库xxx.git地址,拉取镜像
$ git clone --bare xxx.git
Cloning into bare repository 'dispatch_front.git'...
remote: Counting objects: 1673, done.
remote: Total 1673 (delta 161), reused 1034 (delta 161)
Receiving objects: 100% (1673/1673), 6.67 MiB | 4.06 MiB/s, done.
Resolving deltas: 100% (161/161), done.
  1. 进入拉取到的目录front.git
$ ls
 front.git/
$ cd front.git
  1. 上传到新仓库xxx1.git
$ git push --mirror xxx.git
Counting objects: 22357, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (17072/17072), done.
Writing objects: 100% (22357/22357), 59.01 MiB | 2.59 MiB/s, done.
Total 22357 (delta 975), reused 22357 (delta 975)
remote: Resolving deltas: 100% (975/975), done.
To xxx.git
 * [new branch]        dev -> dev
 * [new branch]        master -> master

方式二:
仓库已做镜像的情况下

#查看地址
git remote -v 
# 修改地址
git remote set-url origin https://xxx.git

git push -u origin “master”

git add filename 误操作后需要删除

加上–cache不希望这个文件被版本控制

git rm --cache -f .\CacheBuilderTest.java 
git rm --cache -rf .\CacheBuilderTest   //目录
$ git rm --cache -rf ./plugin/webview_flutter/example/android/.gradle
rm 'plugin/webview_flutter/example/android/.gradle/7.1.1/dependencies-accessors/dependencies-accessors.lock'
rm 'plugin/webview_flutter/example/android/.gradle/7.1.1/dependencies-accessors/gc.properties'
rm 'plugin/webview_flutter/example/android/.gradle/7.1.1/fileChanges/last-build.bin'
rm 'plugin/webview_flutter/example/android/.gradle/7.1.1/fileHashes/fileHashes.lock'
rm 'plugin/webview_flutter/example/android/.gradle/7.1.1/gc.properties'
rm 'plugin/webview_flutter/example/android/.gradle/7.4.2/checksums/checksums.lock'
rm 'plugin/webview_flutter/example/android/.gradle/7.4.2/checksums/sha1-checksums.bin'
rm 'plugin/webview_flutter/example/android/.gradle/7.4.2/dependencies-accessors/dependencies-accessors.lock'
rm 'plugin/webview_flutter/example/android/.gradle/7.4.2/dependencies-accessors/gc.properties'
rm 'plugin/webview_flutter/example/android/.gradle/7.4.2/fileChanges/last-build.bin'
rm 'plugin/webview_flutter/example/android/.gradle/7.4.2/fileHashes/fileHashes.lock'
rm 'plugin/webview_flutter/example/android/.gradle/7.4.2/gc.properties'
rm 'plugin/webview_flutter/example/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock'
rm 'plugin/webview_flutter/example/android/.gradle/buildOutputCleanup/cache.properties'
rm 'plugin/webview_flutter/example/android/.gradle/checksums/checksums.lock'
rm 'plugin/webview_flutter/example/android/.gradle/vcs-1/gc.properties'

$ git status .
On branch feature
Your branch is up to date with 'origin/feature'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    plugin/webview_flutter/example/android/.gradle/7.1.1/dependencies-accessors/dependencies-accessors.lock
        deleted:    plugin/webview_flutter/example/android/.gradle/7.1.1/dependencies-accessors/gc.properties
        deleted:    plugin/webview_flutter/example/android/.gradle/7.1.1/fileChanges/last-build.bin
        deleted:    plugin/webview_flutter/example/android/.gradle/7.1.1/fileHashes/fileHashes.lock
        deleted:    plugin/webview_flutter/example/android/.gradle/7.1.1/gc.properties
        deleted:    plugin/webview_flutter/example/android/.gradle/7.4.2/checksums/checksums.lock
        deleted:    plugin/webview_flutter/example/android/.gradle/7.4.2/checksums/sha1-checksums.bin
        deleted:    plugin/webview_flutter/example/android/.gradle/7.4.2/dependencies-accessors/dependencies-accessors.lock
        deleted:    plugin/webview_flutter/example/android/.gradle/7.4.2/dependencies-accessors/gc.properties
        deleted:    plugin/webview_flutter/example/android/.gradle/7.4.2/fileChanges/last-build.bin
        deleted:    plugin/webview_flutter/example/android/.gradle/7.4.2/fileHashes/fileHashes.lock
        deleted:    plugin/webview_flutter/example/android/.gradle/7.4.2/gc.properties
        deleted:    plugin/webview_flutter/example/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock
        deleted:    plugin/webview_flutter/example/android/.gradle/buildOutputCleanup/cache.properties
        deleted:    plugin/webview_flutter/example/android/.gradle/checksums/checksums.lock
        deleted:    plugin/webview_flutter/example/android/.gradle/vcs-1/gc.properties

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   .gitignore
        modified:   Makefile

对应IDEA的操作就是
文件右键 -> git -> ROLLBACK

清除git历史记录中的大文件

…但是执行之后,不知为何占用空间还变大了.

查询大文件

$ git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
14ad8fb385c589a25b77578bc599a95a211xxxx plugin/xxxx.so
60a278485ef346f95657eecfbc0b2c30637xxxx plugin/xxxx.so
  1. git rev-list --objects --all: 列出仓库中的所有对象(commits、trees、blobs 等)。
  2. git verify-pack -v .git/objects/pack/*.idx: 验证对象包,并输出每个对象的类型、SHA-1 和大小。
  3. sort -k 3 -n: 将上一步的输出按对象大小排序(第 3 列)。
  4. tail -5: 取最大的 5 个对象。
  5. awk ‘{print$1}’: 只输出最大 5 个对象的 SHA-1。
  6. grep “$(tail -5 | awk ‘{print$1}’)”: 过滤输出,只保留最大 5 个对象。
  7. 最后输出这 5 个最大对象的 SHA-1。

删除文件

$ git filter-branch --force --index-filter "git rm -rf --cached --ignore-unmatch android/libs/arm64-v8a/xxx.so" --prune-empty --tag-name-filter cat -- --all
WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites.  Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.  See the
         filter-branch manual page for more details; to squelch this warning,
         set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...

Rewrite 0a3e88ca0a21df9d054387ac50bb1ad5ed74c129 (53/73) (4 seconds passed, remaining 1 predicted)    rm 'android/libs/arm64-v8a/xxx.so'
...略
Ref 'refs/heads/feature' was rewritten
WARNING: Ref 'refs/heads/master' is unchanged
...

这条命令使用 Git 的 filter-branch 效果重新打包 Git 的历史记录,过滤掉指定的大文件 FILENAME。
filter-branch 的作用是重写 Git 历史,可以用来修改提交记录、移除文件等。
这条命令各个选项的意思是:
–force: 强制重写历史,避免非快进合并。
–index-filter: 允许自定义过滤操作,这里使用 git rm 删除指定文件。
“git rm -rf --cached --ignore-unmatch FILENAME”: 过滤操作,删除 FILENAME 文件(需要替换为实际文件名)。
–prune-empty: 如果过滤后某次提交没有改动,则删除此空提交。
–tag-name-filter cat: 保留标签。
– --all: 重写所有分支的历史。
所以,这条 filter-branch 命令的作用是:

  1. 重写仓库的全部历史(–all)。
  2. 在每次提交中删除指定的 FILENAME 文件(–index-filter)。
  3. 如果某次提交变为空提交,则删除这个提交(–prune-empty)。
  4. 保留所有标签(–tag-name-filter cat)。
  5. 强制进行历史重写,即使存在非快进合并(–force)。
    这样,执行此命令后,仓库的历史记录中将不存在 FILENAME 这个文件,且所有分支和标签都会更新到最新历史。
$ git push --force
Enumerating objects: 710, done.
Counting objects: 100% (710/710), done.
Delta compression using up to 4 threads
Compressing objects: 100% (526/526), done.
Writing objects: 100% (652/652), 53.29 MiB | 1.63 MiB/s, done.
Total 652 (delta 264), reused 264 (delta 29), pack-reused 0
remote: Resolving deltas: 100% (264/264), completed with 39 local objects.
To https://xxx.git
 + ced1890...0092ad0 feature -> feature (forced update)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值