Git tips and tricks

Revert last commit in GIT

Revert last commit in GIt also knows as delete the last commit that was made intentionally or accidenlty. You can revert commit using following action.

git reset --soft HEAD~1
git reset --hard HEAD~1

–soft will not remove your uncommited code. Again if you replace the same with –hard it is going to replace all the uncommited code in your local

git reset HEAD~1
git push origin master --force

This above code will remove the last commit. However you can still see the files. Now when you make git push origin master with force it is going to delete the last commit from history.

Undo current git merge

git merge --abort

This is going to reset your working copy to whatever state it was in before the merge. That means that it should restore any uncommitted changes from before the merge, although it cannot always do so reliably. Generally you shouldn’t merge with uncommitted changes anyway.

Sometimes you still need to use git reset --merge even in more recent versions. I had git merge --abort error out (making no changes) where git reset --merge succeeds (and does the right thing) in git 2.2.1.

git merge --abort # is equivalent to git reset --merge when MERGE_HEAD is present.

Git rebase

// quit any currported or ongoing rebase
git rebase --quit

//merge branch
git rebase --merge branch-name

//rebase particular commit either drop, etc
git rebase -i HEAD~2

Reference link

Git logs

git log --oneline
git log --oneline --graph --decorate

//git log with with merge and list of files 
git log -m --name-only

Above git log –oneline will show list of commits in one line with short id within.

git logs search and find

git log --after="2021-7-1" --before="2021-7-5"
git log --grep='any commit message word'  

Vi editor

hit Esc
type :wq
press enter

It will save the current change and exit the vi editor

:qa! to exit without saving.

Recover accidently deleted commits

Lets say you accidently delete a commit by using

git reset --hard old_hash_id

It is simply going to delete all commit that was after this hashId. Now we will use git reflog to get the statement of our git activitites.

git reflog
#now get the hashid of HEAD 1 
git branch recover-commits hashId_HEAD1
git rebase --merge recover-commits

This way you will be able to see all commit including lost commit in your new branch and you can simply merge that branch into new commits with last line. Also you can use git cherry-pick in order to merge only specific commit into your working branch. You need to get the id of that lost commit from new branch you had created and perform the following action.

git cherry-pick lost-commit-hashID

Git merge with squash

git merge feature --squash
git commit -m "feature merge"
git push origin master

git merging with squash will compress multiple commit into one and merge it into current branch. After running above command for merge with squash you might need to commit with new message to reflect on current branch.

Filename too long in Git for Windows

Git has a limit of 4096 characters for a filename but it is not same for Windows operating system while Git is compiled with msys. It uses an older version of the Windows API and there’s a limit of 260 characters for a filename.

You can circumvent this by using another Git client on Windows or set core.longpaths to true as explained in other answers.

git config --system core.longpaths true

Reference link

Issue : Pulling without specifying how to reconcile divergent branches is discouraged

git pull origin main --rebase

When fast forward merge is not working.

git pull origin layout --no-ff

Remove files and folder using linux command

cd /opt/codedeploy-agent/deployment-root
sudo rm -r -f *

Leave a Reply

Your email address will not be published. Required fields are marked *