how to merge specific files in git

前端 未结 4 785
傲寒
傲寒 2021-01-04 09:48

Suppose I have a project on MASTER branch with 100s of php files. To do a bug fixing in the project, i create a separate branch

git checkout -b bugfix
         


        
4条回答
  •  没有蜡笔的小新
    2021-01-04 10:47

    There are two approaches:


    Approach 01

    The following solution is adopted from a blog post

    It turned out that checkout can also be helpful in this matter. You can simply callout (checkout) those specific files from another branch:

    # switch to the branch you want to be your merge destination
    git checkout master
    
    # checkout specific files from specific branch (e.g bugfix)
    # format: git checkout    ... 
    git checkout bugfix login.php register.php
    
    # check the status
    git status
    
    # merge them in
    git commit -m "your merge comment"
    

    Approach 02

    This is an easy alternative approach, but it only works if you have one commit per file (meaning every time you have changed a file, you have made one commit and then have moved to the next file). In this case you can simply bring those specific commits to the other branch (in your case the master branch):

    # get which commit you want to take to the other branch (first 7 characters will do)
    git log
    
    # switch to the branch you want to be your merge destination
    git checkout master
    
    # bring specific commit to this branch (replace the 63344f2 with your own 7 character commit ID)
    git cherry-pick 63344f2
    

提交回复
热议问题