Skip to content

Instantly share code, notes, and snippets.

@p4p4
Last active December 31, 2017 10:22
Show Gist options
  • Save p4p4/c0ad2e27605d1197bb1e3be0232189c9 to your computer and use it in GitHub Desktop.
Save p4p4/c0ad2e27605d1197bb1e3be0232189c9 to your computer and use it in GitHub Desktop.
My git cheat-sheet

resources

remove files from git commit

src: http://stackoverflow.com/questions/12481639/remove-files-from-git-commit

moving the mistakenly committed files back to the staging area from the previous commit, without cancelling the changes done to them:

git reset --soft HEAD^ 

or:

git reset --soft HEAD~1

then:

git reset HEAD path/to/unwanted_file

finally re-use same commit msg:

git commit -c ORIG_HEAD

undo git stash

src: http://stackoverflow.com/questions/10827160/undo-a-git-stash

git stash apply --index

interactive git rebasing, squashing, splitting:

src: https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages

rebase last 3 commits:

git rebase -i HEAD~3

remove untracked files

Show what will be deleted with the -n option:

git clean -f -n

Then - beware: this will delete files - run:

git clean -f

add last commit to staging again (rework last commit)

instead of ammending, one can do this:

git reset --soft @~

and then add the modified files, and create the commit again:

 git commit -c ORIG_HEAD

reformat last commit:

  1. git reset --soft @~
  2. format VCS changed code in IDE
  3. git add <modified files>
  4. git commit -c ORIG_HEAD

fetch a pull request locally:

  • git fetch upstream pull/1836/head:pr-1836
    • use origin instead of upstream when you are not working with a fork
  • git checkout pr-1836

hide a VCS file from git

git update-index --assume-unchanged .idea/encodings.xml

stash a single file:

git checkout *filename*

add a remote:

see here

git remote add origin https://github.com/user/repo.git
# Set a new remote

git remote -v
# Verify new remote
origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

keep track of develop

in feature branch:

git fetch origin develop
git merge FETCH_HEAD
git commit
git push origin HEAD

generate patch of commit, apply patch

generate: git format-patch -1 <sha> apply: git apply <file.patch>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment