【Git】特定场景命令

1. 本地仓库强制与远程仓库同步

场景: 本地仓库做了一些操作后,使用常规命令同步远程仓库出现错误,同步失败
命令:

git fetch --all
git reset  --hard origin/branch
git pull

branch为实际使用分支名

2. 本地仓库的某一个目录下代码回退到指定commit id

场景: 有时为方便回溯代码,需要把代码回退到一个之前的版本,并不需要实际的回退,仅本地仓库需要回退的目录或文件回退,远程仓库保持最新,问题验证完成后,本地仓库和远程仓库同步,依旧为最新
命令:

git reset commit-id 目录或文件
git status
git checkout -- 目录或文件

如此,指定的目录或文件就回退到了指定的版本。验证完成后,使用以下两种方法还原和远端一致

  • 使用场景1方法
  • 回退代码后,由于和最新代码不一致,回退的代码以已修改的状态在cache区域,通过git status可以看到,根据提示把目前未提交状态的代码回退掉,不提交,这样就和远端一致了
    回退到指定commit id后的代码:
$ git status .
On branch iandos-latest
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   AndroidManifest.xml
        modified:   src/com/android/settings/dashboard/SuggestionsChecks.java

还原保持和远端一致后

$ git status .
On branch iandos-latest
nothing to commit, working directory clean

3. 强制远端仓库与本地仓库同步

场景: 本地做了一些修改,push到远端服务器出现一些不需要再修复的冲突,需要强制同步本地仓库到远端仓库
命令:

git push -f -u origin branch

branch为实际使用分支,例如:

$ git push -f -u origin icetech_algorithms 
[email protected]'s password: 
Counting objects: 8, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 1.39 KiB | 0 bytes/s, done.
Total 8 (delta 4), reused 0 (delta 0)
To [email protected]:/home/ice/RK3288_6.0_0518
   168f8b2..0c55c85  icetech_algorithms -> icetech_algorithms
Branch icetech_algorithms set up to track remote branch icetech_algorithms from origin.
binn.chen@ice-tech:~/1.CODE/RK3288_6.0_0518$ 

猜你喜欢

转载自blog.csdn.net/mcsbary/article/details/91897173