Skip to content

Instantly share code, notes, and snippets.

@gnud
Last active April 27, 2020 20:03
Show Gist options
  • Save gnud/e160693f0911323866cd to your computer and use it in GitHub Desktop.
Save gnud/e160693f0911323866cd to your computer and use it in GitHub Desktop.
== download only subdirectory from the repo
git clone --depth 1 -b 4.1 https://github.com:/celery/celery/
git filter-branch --prune-empty --subdirectory-filter examples/ 4.1
Expected to see the file structure from examples
==how to show code from popped stash==
git stash show -p {put hash here}
==how to find removed references==
for c in $(git fsck --unreachable | awk -F " " '{ print $3 }'); do git stash show -p "$c"; done
==save credentials==
git config --global credential.helper cache
git push # or any write command and write password, the next execution will not ask for a password
git status
git checkout -- myfile
Stash:
git stash # then the working directory is clean
git stash apply <stash>
git stash clear
git stash pop # save the reference hash for later, just in case, see: ==how to show code from popped stash==
Diff:
git diff --cached # will diff cached i.e unstage versiaf590cbon of the already staged files
Commit:
git status # will list all the files that are changed
git add some-file
git commit -m
or
git commit -m "Refactor to simplify" some-file
::::MOST IMPORTANT: REVERT LAST COMMIT::::
git reset --hard #SOMECOMMIT
# Edit some file
git commit -a
git push --force
Fixing things:
1) rename the file
git reset HEAD some-file
git checkout -- some-file
git reset --merge ORIG_HEAD
Someone commited in meantime:
2)
Reverting your local commit:
git reset --soft HEAD~1
git pull -ff
git status
3) reset open merge on a file
git reset HEAD some_file
git checkout -- some_file
How to push only specific local commit:
git push origin <commit SHA>:<remotebranchname>
Branches:
List branches (Local):
git branch
List branches (Remote):
git branch -r
Checking out
git checkout branch-name
Merging
git merge branch-name
Remove and recheckout
git branch -d -r origin/branch-name
https://gist.github.com/geelen/590895
Remove the first commit:
# first you need a new empty branch; let's call it `newroot`
git checkout --orphan newroot
git rm -rf .
# then you apply the same steps
git commit --allow-empty -m 'root commit'
git rebase --onto newroot --root master
git branch -d newroot
@gnud
Copy link
Author

gnud commented Jan 15, 2015

How to clean the code from leftover test files

Cleans other files not added to git's index

git clean -f -d -e "myfile.py" # -f forces the operation

First inspect what'll be done

git clean -n -d -e "myfile.py" # -n simulates aka "dry run"

@gnud
Copy link
Author

gnud commented Jan 29, 2015

Commit mistakes :(

Undoing the commit

git reset --soft HEAD~1 # This will revert the last commit and give you back the files

Save the changes

git stash # will save your files in a stash named {0}

Prepare a new branch

git branch {mynewbranch}
git push origin mynewbranch
git checkout mynewbranch

Put the code in the branch

git stash apply stash@{0}
git stash clear # optional, but be careful
git list # will list all the stashes available

Final result

git status # will list the same file changes as before in the previous branch
git add .
git commit # write the commit message and exit vi{m}/nano
git push

@gnud
Copy link
Author

gnud commented Jun 6, 2015

Add file to ignore list that's already committed and cannot be gitignored :)

git update-index --assume-unchanged myfile

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