// remove all files marked as deleted from git git ls-files --deleted -z | xargs -0 git rm
// change remote url from HTTP to SSH git remote set-url origin [email protected]:USERNAME/REPOSITORY.git
// add files to the previous commit without modifying commit message git commit --amend --no-edit
// squash (combine) several commits into one git rebase <branch_name> -i // change pick to squash for commit messages that will be combined
// squash to one commit git rebase -i HEAD~[NUMBER OF COMMITS]
// check which commits will be pushed to remote (assuming that branch was created from master) git cherry -v master
// list merged branches git branch -a --merged
// list all branches merged into git branch --merged
// delete all merge branches git branch --merged master | grep -v "(^*|master)" | xargs git branch -d
// switch to previous branch git branch -
// delete remote branch git push origin --delete :$branchname
// rename branch git branch -M newbranch
// git add & git commit files in the current working directory with a single command git commit -am 'test message'
// change message of the last commit git commit --amend -m 'new commit message'
// show difference between specific commit and current HEAD ( HEAD is pointing to the last commit in current branch) git diff COMMIT_ID
// add files to a previous commit. This might cause Git error: git add git commit --amend --no-edit (--no-edit - allows to keep same commit message)
// Above command might cause git error -> Updates were rejected because the tip of your current branch is behind
git push —force-with-lease // this will overwrite remote commit with your local commit
// adding --force-with-lease
will allow you to push the code if there are no conflicting changes with the current parent branch
// stash changes and give a stash specific name git stash save stashname
// list stashes and apply specific stash git stash list git stash apply {index}
// stash untracked files git stash --include-untracked // or the shorter alternative: git stash -u
// git log history of the current branch git log --graph --oneline --decorate
// remove untracked files ( files which are not yet added to staging area) // check which untracked files will be removed git clean -dn // remove untracked files and directories git clean -df
// Unstage all files git reset
// discard all local modified files git reset --hard HEAD // or git checkout .
// discard single modified file git checkout -- file.txt
// checkout file/folder from feature-branch into master git checkout master git checkout feature-branch -- src/file.py
// remove local commit (1 - means latest commit)
// use it when your commit is not yet in a remote
git reset HEAD1
// keeps files in staging area git reset --soft HEAD~1
// completely removes committed files git reset --hard HEAD~1
// undo commit from remote repo, this will create a new commit message git revert commit-id
// undo the last commit, but don't create a revert commit git revert -n HEAD
// * undo last commit from remote git reset HEAD^ git push origin +HEAD
// remove file from remote but keeps it locally git remove --cached file.txt
// completely remove file from a history https://www.30secondsofcode.org/git/s/purge-file
// Remove .env file from history git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch .env" HEAD git push --force
// Grep over commit messages git log --grep="#123"
// Fetch Pull-Request to test it locally git fetch origin pull/ID/head:BRANCHNAME // where id is pull-request id
// Delete all branches except main git branch | grep -v "main" | xargs git branch -D
// Move allchanges to another branch git switch -c "new-branch"