git commands
undoing local changes
Checking what was committed
$ git show $COMMIT
Undoing git add / Unstaging changes
$ git reset
Undoing 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
pick
toedit
in the line of the hashDo the change in the files
Amend the changes
$ git commit --amend
Go back to the HEAD
$ git rebase --continue
stash and pop
For stashing current changes
$ git stash
To show what is stashed
$ git stash list
and$ git stash show
For popping out the last stashed changes
$ git stash apply
Stash only unstaged changes
$ git stash save --keep-index
adding main repo as upstream
Make sure upstream is not configured yet
$ git remote -v
Add upstream
$ git remote add upstream
https://github.com/main_repo/main_repo.git
Get upstream data
$ git fetch upstream
merging git branch into master
go to master
git checkout master
make sure it is updated solving conflicts if they exist
git pull origin master
merge with the branch (list of branches can be obtained by
git branch
)git merge branch_name
push 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 "message
Pushing a tag:
$ git push origin vX.Y
or$ 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 master
going 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 master
Execute 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 -- --all
Remove 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?