Skip to content

Instantly share code, notes, and snippets.

@hightemp
Last active May 9, 2019 20:06
Show Gist options
  • Save hightemp/e6843787c910d9e9ac21464874b0dd85 to your computer and use it in GitHub Desktop.
Save hightemp/e6843787c910d9e9ac21464874b0dd85 to your computer and use it in GitHub Desktop.
Git snippets

Git snippets

Merge unrelated histories repositories

git pull origin master --allow-unrelated-histories

Copy commit from one branch to another

git cherry-pick -x <sha>

Find string in commits

git log -S "string to find"

Push amended commit

git push origin mybranch --force-with-lease

Update commit message

git commit --amend -m 'xxxxxxx'

Time of commit

git show -s --format=%ci HEAD

Remove last commit

Push has been done

git reset HEAD^ --hard
git push --force-with-lease [remote] [branch]

Push has not been done

git reset --soft HEAD@{1}

Edit comment of made commit

git commit --amend -m "New commit message" 

Add file to commit

git add file
git commit --amend --no-edit

Remove directory

Remove directory from git and local

git rm -r one-of-the-directories
git commit -m "Remove duplicated directory"
git push origin <your-git-branch>

Remove directory from git but NOT local

git rm -r --cached myFolder

Get list of not merged branches

Local branches

git branch --no-merged master

With remote branches

git branch -a --no-merged master

Add changes of file into sevral commits

git add -p

Create a branch from a commit

git checkout -b <branch> <SHA1_OF_COMMIT>

Pulled from/into the wrong branch

(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

(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

git log -U -m --simplify-merges --merges -- a.txt

Resolve easy/obvious conflicts

If solution is to accept local/our version, run:

git checkout --ours PATH/FILE

If solution is to accept remote/other-branch version, run:

git checkout --theirs PATH/FILE

Oh shit, I accidentally committed something to master that should have been on a brand new branch!

# 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

# 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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment