{"id":5222,"date":"2018-08-22T10:51:53","date_gmt":"2018-08-22T10:51:53","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=5222"},"modified":"2021-11-17T10:16:17","modified_gmt":"2021-11-17T10:16:17","slug":"how-to-update-or-pull-current-branch-before-committing-in-git","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-update-or-pull-current-branch-before-committing-in-git\/","title":{"rendered":"Git Commands Tutorials and Example: Git Fetch &#8211; Update remote branch before merge to local branch"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5277\" src=\"http:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2018\/08\/git-1.png\" alt=\"\" width=\"600\" height=\"350\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/08\/git-1.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/08\/git-1-300x175.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Best practice says that before you commit in git, you need to either do git pull or git fetch\/merge. However, there is a way to find out wheather your branches is not in sync with remote.<\/p>\n<p><strong>To check the remote repo status you are really simulating a &#8220;fetch&#8221;<\/strong><br>\n$ git fetch -v &#8211;dry-run<\/p>\n<p><strong>To bring your remote refs up to date<\/strong><br>\n$ git remote -v update<\/p>\n<p><strong>This command will print whether the branch you are tracking is ahead, behind or has diverged with remote. If it says nothing, the local and remote are the same.<\/strong><br>\n$ git status -uno<\/p>\n<p><strong>To pull<\/strong><br>\n$ git pull origin &lt;&lt;branchname&gt;&gt;<br>\nor<br>\n$ git fetch origin &lt;&lt;branchname&gt;&gt;<br>\n$ git merge remote\/&lt;&lt;branchname&gt;&gt;<br>\n$ git commit<\/p>\n<p><strong>Compare the two branches:<\/strong><br>\n$ git log HEAD..origin\/master &#8211;oneline<\/p>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_51498\"><iframe loading=\"lazy\"  id=\"_ytid_94574\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_51498\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Best practice says that before you commit in git, you need to either do git pull or git fetch\/merge. However, there is a way to find out wheather your branches&#8230; <\/p>\n","protected":false},"author":1,"featured_media":5277,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[38],"tags":[160,5020,5017,5021,5018,5019,5022],"class_list":["post-5222","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-git","tag-git","tag-git-commit","tag-git-fetch","tag-git-merge","tag-git-pull","tag-git-push","tag-git-remote"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=5222"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5222\/revisions"}],"predecessor-version":[{"id":25589,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5222\/revisions\/25589"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/5277"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}