# Git tricks and loving git resource –source and github pull requests splitting

- Link: https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/
- Published: 2021-02-27T11:01:47-08:00
- Author: rich

Ok this has always required a git checkout and then copy some files somewhere and
then go back, but with July 2019 Git 2.3, you can now use –source in git restore
to [get a single file back](https://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch).

    ```wp-block-code
    git switch your-dev branch
    # make some stupid changes like linting all the files
    git restore --source main main.py
    ```

The place where this really comes in handy is that if you do a git pull request 
and realize that you have changed too many files, then [split the pull request](https://www.thedroidsonroids.com/blog/splitting-pull-request)
restore the ones you want and create a separate branch so you end up with multiple
branches, these are cleaner to PR.

## If you have a linear commits to be cut in two

The mechanism is a little hard if you have lots of commits, but one thing to do 
is to [separate the commits](https://gist.github.com/Loilo/930f141d9acf89e9e734ffa042acd750)
into separate branches. This means you have to get all the commits you want in branch
A there and this is the simple case where say the first 3 commits should be in one
PR and the second in another.

    ```wp-block-code
    # figure out where you want to cut the first set of commits off
    # so if you want the last three commits to be in the second-branch 
    git switch mongo-branch
    git branch first-branch
    git reset --hard HEAD~3
    git rebase --onto main first-branch second-branch
    # first-branch is the first N and second is the last 3
    ```

## If you have commits in random orders

If you have commits in random order, then rebase -I is your friend to do interactive
rearranging

    ```wp-block-code
    # get to the mongo branch
    git rebase main -
    # now go interactively and rearrange the first branch and second branch commits in the right order
    # Now you can do the linear split.
    ```

## If you forget what files changed in a commit

Then you have a friend in git log to show what [commits are differen](https://stackoverflow.com/questions/13965391/how-do-i-see-the-commit-differences-between-branches-in-git)
t between branches and git show to show [what files changes in a commit](https://tosbourn.com/git-command-to-show-files-changed-in-a-commit/).

And then finally make the commit messages more accurate with

    ```wp-block-code
    # find out what commits your are talking about
    git switch mongo-branch
    git log mongo-branch..main
    git show --name-only {what every commit you want}
    # to fix the message in the last three commit
    git rebase -i HEAD~3 
    # when you are all done will need a force
    git push -f
    ```

### Share this:

 * [  Mastodon ](https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/?share=mastodon)
 * [  LinkedIn ](https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/?share=linkedin)
 * [  Facebook ](https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/?share=facebook)
 * [  WhatsApp ](https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/?share=jetpack-whatsapp)
 * [  Email ](https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/?subject=%5BShared%20Post%5D%20Git%20tricks%20and%20loving%20git%20resource%20--source%20and%20github%20pull%20requests%20splitting&body=https%3A%2F%2Ftongfamily.com%2F2021%2F02%2F27%2Fgit-tricks-and-loving-git-resource-source%2F&share=email)
 * [More](https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/#)

 * [  Telegram ](https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/?share=telegram)
 * [  Print ](https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/#print?share=print)
 * [  Pinterest ](https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/?share=pinterest)
 * [  Tumblr ](https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/?share=tumblr)
 * [  X ](https://tongfamily.com/2021/02/27/git-tricks-and-loving-git-resource-source/?share=twitter)

### Like this:

Like Loading…
