git commands
undoing local changes
Checking what was committed
$ git show $COMMITUndoing git add / Unstaging changes
$ git resetUndoing a git reset
$ git reset 'HEAD@{1}'Undoing last git commit
$ git reset HEAD^Changing a specific git commit:
Rebase to the specific commit
$ git rebase --interactive 'hash^'Modify
picktoeditin the line of the hashDo the change in the files
Amend the changes
$ git commit --amendGo back to the HEAD
$ git rebase --continue
stash and pop
For stashing current changes
$ git stashTo show what is stashed
$ git stash listand$ git stash showFor popping out the last stashed changes
$ git stash applyStash only unstaged changes
$ git stash save --keep-index
adding main repo as upstream
Make sure upstream is not configured yet
$ git remote -vAdd upstream
$ git remote add upstreamhttps://github.com/main_repo/main_repo.gitGet upstream data
$ git fetch upstream
merging git branch into master
go to master
git checkout mastermake sure it is updated solving conflicts if they exist
git pull origin mastermerge with the branch (list of branches can be obtained by
git branch)git merge branch_namepush merged local repository
git push origin master
managing tags
Creating a tag:
$ git tag -a vX.Y -m "message"Creating a tag to a specific commit:
$ git tag -a vX.Y COMMIT_ID -m "messagePushing a tag:
$ git push origin vX.Yor$ git push origin --tags
go to a specific commit and back to the latest master
go to specific commit
git checkout <sha>do the necessary job
make sure the local is updated
git pull origin mastergoing back to master
git checkout master
cherry-picking - add a specific commit to another branch
Checkout the branch in which the commit will be added (e.g.: master):
$ git checkout masterExecute the cherry-pick
$ git cherry-pick <commit-hash>
permanently removing files from git
Removing file from all branches (replace PATH/TO/FILENAME):
$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch PATH/TO/FILENAME' --prune-empty --tag-name-filter cat -- --allRemove old references
$ rm -rf .git/refs/original/Prune garbage
$ git reflog expire --expire=now --all && git gc --aggressive --prune=now
checking staged diffs
Checking staged diffs:
$ git diff --cached
removing a file and all its history
$ git filter-branch --tree-filter 'rm -f path/to/file' -- --all
Last updated
Was this helpful?