{"id":337,"date":"2023-02-01T09:54:21","date_gmt":"2023-02-01T09:54:21","guid":{"rendered":"https:\/\/santosh-shah.com\/blog\/?p=337"},"modified":"2023-07-13T06:51:49","modified_gmt":"2023-07-13T06:51:49","slug":"git-tips-and-tricks","status":"publish","type":"post","link":"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/","title":{"rendered":"Git tips and tricks"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Revert last commit in GIT<\/h2>\n\n\n\n<p>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. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --soft HEAD~1\ngit reset --hard HEAD~1<\/code><\/pre>\n\n\n\n<p>\u2013soft will not remove your uncommited code. Again if you replace the same with \u2013hard it is going to replace all the uncommited code in your local<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset HEAD~1\ngit push origin master --force<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Undo current git merge<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>git merge --abort<\/code><\/pre>\n\n\n\n<p>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&#8217;t merge with uncommitted changes anyway.<\/p>\n\n\n\n<p>Sometimes you still need to use&nbsp;<code>git reset --merge<\/code>&nbsp;even in more recent versions. I had&nbsp;<code>git merge --abort<\/code>&nbsp;error out (making no changes) where&nbsp;<code>git reset --merge<\/code>&nbsp;succeeds (and does the right thing) in git 2.2.1.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git merge --abort # is equivalent to git reset --merge when MERGE_HEAD is present.<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Git rebase<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ quit any currported or ongoing rebase\ngit rebase --quit\n\n\/\/merge branch\ngit rebase --merge branch-name\n\n\/\/rebase particular commit either drop, etc\ngit rebase -i HEAD~2\n<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/5741407\/how-to-undo-a-git-merge-with-conflicts\" target=\"_blank\" rel=\"noreferrer noopener\">Reference link<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Git logs<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>git log --oneline\ngit log --oneline --graph --decorate\n\n\/\/git log with with merge and list of files \ngit log -m --name-only<\/code><\/pre>\n\n\n\n<p>Above git log &#8211;oneline will show list of commits in one line with short id within.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"412\" src=\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1-1024x412.png\" alt=\"\" class=\"wp-image-355\" srcset=\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1-1024x412.png 1024w, https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1-300x121.png 300w, https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1-768x309.png 768w, https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1-1536x618.png 1536w, https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1.png 1994w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>git logs search and find<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git log --after=\"2021-7-1\" --before=\"2021-7-5\"\ngit log --grep='any commit message word'  <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Vi editor<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>hit Esc\ntype :wq\npress enter<\/code><\/pre>\n\n\n\n<p>It will save the current change and exit the vi editor<\/p>\n\n\n\n<p>:qa! to exit without saving.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Recover accidently deleted commits<\/h2>\n\n\n\n<p>Lets say you accidently delete a commit by using <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --hard old_hash_id<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reflog\n#now get the hashid of HEAD 1 \ngit branch recover-commits hashId_HEAD1\ngit rebase --merge recover-commits<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git cherry-pick lost-commit-hashID<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Git merge with squash<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>git merge feature --squash\ngit commit -m \"feature merge\"\ngit push origin master<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Filename too long in Git for Windows<\/h3>\n\n\n\n<p>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&#8217;s a limit of 260 characters for a filename. <\/p>\n\n\n\n<p>You can circumvent this by using another Git client on Windows or set&nbsp;<code>core.longpaths<\/code>&nbsp;to&nbsp;<code>true<\/code>&nbsp;as explained in other answers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git config --system core.longpaths true<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/22575662\/filename-too-long-in-git-for-windows\">Reference link<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Issue : Pulling without specifying how to reconcile divergent branches is discouraged<\/h3>\n\n\n\n<p><code>git pull origin main --rebase<\/code><\/p>\n\n\n\n<p>When fast forward merge is not working.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git pull origin layout --no-ff\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-santosh-kumar-shah wp-block-embed-santosh-kumar-shah\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"ZG4OdcA88Q\"><a href=\"https:\/\/santosh-shah.com\/blog\/track-all-remote-branches-git\/\">Track all remote branches GIT<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Track all remote branches GIT&#8221; &#8212; Santosh Kumar Shah\" src=\"https:\/\/santosh-shah.com\/blog\/track-all-remote-branches-git\/embed\/#?secret=g6nuAkcQB9#?secret=ZG4OdcA88Q\" data-secret=\"ZG4OdcA88Q\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Remove files and folder using linux command<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/opt\/codedeploy-agent\/deployment-root\nsudo rm -r -f *<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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. \u2013soft will not remove your uncommited code. Again if you replace the same with \u2013hard it is going to replace all the uncommited code in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,5],"tags":[21,12,22,24,23,20],"class_list":["post-337","post","type-post","status-publish","format-standard","hentry","category-git","category-tips-and-ticks","tag-abort-merge","tag-branch","tag-cherry-pick","tag-delete-commit","tag-recover-lost-commit","tag-revert-commit"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Git tips and tricks - Santosh Kumar Shah<\/title>\n<meta name=\"description\" content=\"Git tips and tricks - Remove last commit using git rebase technique. Remove specific commit using git rebase.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Git tips and tricks - Santosh Kumar Shah\" \/>\n<meta property=\"og:description\" content=\"Git tips and tricks - Remove last commit using git rebase technique. Remove specific commit using git rebase.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/\" \/>\n<meta property=\"og:site_name\" content=\"Santosh Kumar Shah\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-01T09:54:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-13T06:51:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1-1024x412.png\" \/>\n<meta name=\"author\" content=\"Santosh Kumar Shah\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Santosh Kumar Shah\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/\",\"url\":\"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/\",\"name\":\"Git tips and tricks - Santosh Kumar Shah\",\"isPartOf\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1-1024x412.png\",\"datePublished\":\"2023-02-01T09:54:21+00:00\",\"dateModified\":\"2023-07-13T06:51:49+00:00\",\"author\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f\"},\"description\":\"Git tips and tricks - Remove last commit using git rebase technique. Remove specific commit using git rebase.\",\"breadcrumb\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/#primaryimage\",\"url\":\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1.png\",\"contentUrl\":\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1.png\",\"width\":1994,\"height\":802},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/santosh-shah.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Git tips and tricks\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#website\",\"url\":\"https:\/\/santosh-shah.com\/blog\/\",\"name\":\"Santosh Kumar Shah\",\"description\":\"JavaScript Developer\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/santosh-shah.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f\",\"name\":\"Santosh Kumar Shah\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g\",\"caption\":\"Santosh Kumar Shah\"},\"description\":\"I am JavaScript developer.\",\"sameAs\":[\"https:\/\/santosh-shah.com\/blog\"],\"url\":\"https:\/\/santosh-shah.com\/blog\/author\/sks7yu\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Git tips and tricks - Santosh Kumar Shah","description":"Git tips and tricks - Remove last commit using git rebase technique. Remove specific commit using git rebase.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/","og_locale":"en_US","og_type":"article","og_title":"Git tips and tricks - Santosh Kumar Shah","og_description":"Git tips and tricks - Remove last commit using git rebase technique. Remove specific commit using git rebase.","og_url":"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/","og_site_name":"Santosh Kumar Shah","article_published_time":"2023-02-01T09:54:21+00:00","article_modified_time":"2023-07-13T06:51:49+00:00","og_image":[{"url":"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1-1024x412.png","type":"","width":"","height":""}],"author":"Santosh Kumar Shah","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Santosh Kumar Shah","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/","url":"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/","name":"Git tips and tricks - Santosh Kumar Shah","isPartOf":{"@id":"https:\/\/santosh-shah.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/#primaryimage"},"image":{"@id":"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/#primaryimage"},"thumbnailUrl":"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1-1024x412.png","datePublished":"2023-02-01T09:54:21+00:00","dateModified":"2023-07-13T06:51:49+00:00","author":{"@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f"},"description":"Git tips and tricks - Remove last commit using git rebase technique. Remove specific commit using git rebase.","breadcrumb":{"@id":"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/#primaryimage","url":"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1.png","contentUrl":"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/02\/image-1.png","width":1994,"height":802},{"@type":"BreadcrumbList","@id":"https:\/\/santosh-shah.com\/blog\/git-tips-and-tricks\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/santosh-shah.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Git tips and tricks"}]},{"@type":"WebSite","@id":"https:\/\/santosh-shah.com\/blog\/#website","url":"https:\/\/santosh-shah.com\/blog\/","name":"Santosh Kumar Shah","description":"JavaScript Developer","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/santosh-shah.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f","name":"Santosh Kumar Shah","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g","caption":"Santosh Kumar Shah"},"description":"I am JavaScript developer.","sameAs":["https:\/\/santosh-shah.com\/blog"],"url":"https:\/\/santosh-shah.com\/blog\/author\/sks7yu\/"}]}},"_links":{"self":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/comments?post=337"}],"version-history":[{"count":18,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/337\/revisions"}],"predecessor-version":[{"id":387,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/337\/revisions\/387"}],"wp:attachment":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/media?parent=337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/categories?post=337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/tags?post=337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}