# Git snippets ## Merge unrelated histories repositories ```bash git pull origin master --allow-unrelated-histories ``` ## Copy commit from one branch to another ```bash git cherry-pick -x ``` ## Find string in commits ```bash git log -S "string to find" ``` ## Push amended commit ```bash git push origin mybranch --force-with-lease ``` ## Update commit message ```bash git commit --amend -m 'xxxxxxx' ``` ## Time of commit ```bash git show -s --format=%ci HEAD ``` ## Remove last commit ### Push has been done ```bash git reset HEAD^ --hard git push --force-with-lease [remote] [branch] ``` ### Push has not been done ```bash git reset --soft HEAD@{1} ``` ## Edit comment of made commit ```bash git commit --amend -m "New commit message" ``` ## Add file to commit ```bash git add file git commit --amend --no-edit ``` ## Remove directory ### Remove directory from git and local ```bash git rm -r one-of-the-directories git commit -m "Remove duplicated directory" git push origin ``` ### Remove directory from git but NOT local ```bash git rm -r --cached myFolder ``` ## Get list of not merged branches ### Local branches ```bash git branch --no-merged master ``` ### With remote branches ```bash git branch -a --no-merged master ``` ## Add changes of file into sevral commits ```bash git add -p ``` ## Create a branch from a commit ```bash git checkout -b ``` ## Pulled from/into the wrong branch ```bash (master)$ git reflog ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward c5bc55a HEAD@{1}: checkout: checkout message goes here Simply reset your branch back to the desired commit: $ git reset --hard c5bc55a ``` ## Restore deleted branch ```bash (master)$ git reflog 69204cd HEAD@{0}: checkout: moving from my-branch to master 4e3cd85 HEAD@{1}: commit: foo.txt added 69204cd HEAD@{2}: checkout: moving from master to my-branch (master)$ git checkout -b my-branch-help Switched to a new branch 'my-branch-help' (my-branch-help)$ git reset --hard 4e3cd85 HEAD is now at 4e3cd85 foo.txt added (my-branch-help)$ ls README.md foo.txt ``` ## List merge commits affecting a file ```bash git log -U -m --simplify-merges --merges -- a.txt ``` ## Resolve easy/obvious conflicts If solution is to accept local/our version, run: ```bash git checkout --ours PATH/FILE ``` If solution is to accept remote/other-branch version, run: ```bash git checkout --theirs PATH/FILE ``` ## Oh shit, I accidentally committed something to master that should have been on a brand new branch! ```bash # create a new branch from the current state of master git branch some-new-branch-name # remove the commit from the master branch git reset HEAD~ --hard git checkout some-new-branch-name # your commit lives in this branch now :) ``` ## Oh shit, I accidentally committed to the wrong branch! Solution 1 ```bash # undo the last commit, but leave the changes available git reset HEAD~ --soft git stash # move to the correct branch git checkout name-of-the-correct-branch git stash pop git add . # or add individual files git commit -m "your message here" # now your changes are on the correct branch ``` Solution 2 ```bash git checkout name-of-the-correct-branch # grab the last commit to master git cherry-pick master # delete it from master git checkout master git reset HEAD~ --hard ```