{"id":4834,"date":"2020-07-02T14:02:14","date_gmt":"2020-07-02T05:02:14","guid":{"rendered":"https:\/\/learningbox.co.jp\/?p=4834"},"modified":"2020-07-02T14:02:14","modified_gmt":"2020-07-02T05:02:14","slug":"blog_git-command","status":"publish","type":"post","link":"https:\/\/learningbox.co.jp\/en\/2020\/07\/02\/blog_git-command\/","title":{"rendered":"Recommended for programmers! Here are some git commands you can use during development!"},"content":{"rendered":"<p>Hello. I'm \ud83d\udc08 Yokoyama from development. Do you use git? I think it is rare not to use git in development now. In this article, I will explain<span style=\"border-bottom:solid 2px red;\"><strong>Here are some git commands that we use during development.<\/strong><\/span><\/p>\n<p>When I first started working with git, it was very difficult and I used to crash a lot \ud83d\ude2d.<br \/>\nEven now, I can't say that I understand it, but I almost never get stuck with git operations during development anymore. With this paper as a guide, I urge you to get familiar with the git commands!<\/p>\n<p class=\"mokujimidasi\">Click here for table of contents<\/p>\n<ul class=\"mokuji\">\n<li>1. what is git?<\/li>\n<li>2. features and mechanism of git<\/li>\n<li>3. git commands recommended for developers<\/li>\n<li>4. summary<\/li>\n<\/ul>\n<h2><b>To begin with, what is git?<\/b><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tatsuno-system.co.jp\/wp-content\/uploads\/2020\/06\/git-\u521d\u5fc3\u8005.png\" alt=\"First time working with git\" width=\"1265\" height=\"418\" class=\"alignnone size-full wp-image-4872\" srcset=\"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/06\/git-\u521d\u5fc3\u8005.png 1265w, https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/06\/git-\u521d\u5fc3\u8005-300x99.png 300w, https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/06\/git-\u521d\u5fc3\u8005-768x254.png 768w, https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/06\/git-\u521d\u5fc3\u8005-1024x338.png 1024w\" sizes=\"auto, (max-width: 1265px) 100vw, 1265px\" \/><\/p>\n<p>git is a distributed version control system that keeps track of changes to program source code. git was created by Linus Torvalds, the inventor of Linux, to manage the source code of the Linux kernel.<\/p>\n<p>With git, you can store (commit) your code changes in your local environment as well, so you don't need to be constantly connected to a remote server. This allows you to work without being connected to a network.<\/p>\n<h2><b>What is git and how does it work?<\/b><\/h2>\n<p class=\"well2\"><strong>Features of \u25c6git<\/strong><br \/>\nI could easily revert to an older version!<br \/>\nI can centrally manage my old and new files!<br \/>\nYou can share your edited history with multiple people!<br \/>\nI can merge multiple people's revisions into one!\n<\/p>\n<h2><b>Recommended git commands for developers<\/b><\/h2>\n<p>Here are some of the commands I use during development.<\/p>\n<h3><b>git status<\/b><\/h3>\n<p>This function is used to check the current status. Specifically, you can add or modify a file, or register it in the index with add, to see what the current status is.<\/p>\n<h3><b>git branch<\/b><\/h3>\n<p>Branching is a feature of version control that allows you to branch off of your master history. git branch lets you see what branch you're on and what other branches you have, and you can use it before you checkout.<\/p>\n<h3><b>git log<\/b><\/h3>\n<p>This is used when you want to check your commit history. You can use this command to see your own history and the history of others, and it's very useful when you're developing with multiple people. If you're working with multiple people, this is a very useful command. log shows you the commits you've made so far in the form of a list of commit messages.<\/p>\n<h3><b>git checkout<\/b><\/h3>\n<p>Use this to switch branches or revert changes to a file back to the way it was before you changed it.<br \/>\nIt's too versatile, so they added git switch and git restore, but I still use checkout out of habit.<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 50%;\">git checkout .<\/td>\n<td style=\"width: 50%;\">Restore all files to the state of the latest commit.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">git checkout .<\/td>\n<td style=\"width: 50%;\">Restore the specified file to the state of the latest commit.<br \/>\nMore than one is allowed. Directory is also acceptable.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">git checkout .<\/td>\n<td style=\"width: 50%;\">Switch to the specified branch.<br \/>\nIf you don't have a specified branch locally, but have one remotely, you can create a branch and switch between them.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">git checkout -b &lt;branch name<\/td>\n<td style=\"width: 50%;\">Cut a branch from the current branch and switch straight to it.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">git checkout .<\/td>\n<td style=\"width: 50%;\">Switches to the state of the specified commit.<br \/>\nIt is used for bug verification and so on.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3><b>git add<\/b><\/h3>\n<p>Used to add changes to the workspace to staging.<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 50%;\">git add .<\/td>\n<td style=\"width: 50%;\">All files with changes in the workspace are added to staging.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">git add <\/td>\n<td style=\"width: 50%;\">Add the specified file to staging.<br \/>\nMore than one is allowed. Directory is also acceptable.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">git add -p<\/td>\n<td style=\"width: 50%;\">You can choose to subdivide your changes and add them to staging.<br \/>\nUse this if you've made some changes and want to split the commit into multiple commits.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>git commit<\/b><\/h3>\n<p>Use this to commit files that have been added to staging.<\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 60px;\" border=\"1\">\n<tbody>\n<tr style=\"height: 60px;\">\n<td style=\"width: 50%; height: 60px;\">git commit -m \"commit message\"<\/td>\n<td style=\"width: 50%; height: 60px;\">You can commit by specifying a commit message.<br \/>\nIf you don't have this, you may be confused at first when vim opens.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>git reset<\/b><\/h3>\n<p>Use this to undo commits or to return things that have been added to staging to the workspace.<\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 115px;\" border=\"1\">\n<tbody>\n<tr style=\"height: 30px;\">\n<td style=\"width: 50%; height: 30px;\">git reset<\/td>\n<td style=\"width: 50%; height: 30px;\">Returns the files added to the staging to the workspace.<\/td>\n<\/tr>\n<tr style=\"height: 57px;\">\n<td style=\"width: 50%; height: 57px;\">git reset HEAD^<\/td>\n<td style=\"width: 50%; height: 57px;\">Undo the previous commit.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>git pull<\/b><\/h3>\n<p>Reflects the status of the remote repository to the local repository.<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 50%;\">git pull<\/td>\n<td style=\"width: 50%;\">Basically this is the only thing I use.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">git pull -prune<\/td>\n<td style=\"width: 50%;\">Updates the information in the local repository for the remote repository branch.<br \/>\nIt's too much work to specify this every time.<\/p>\n<pre>git config --global fetch.prune true<\/pre>\n<p>you can just do a git pull.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>git push<\/b><\/h3>\n<p>Adds your local commits to the remote repository.<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 50%;\">git push<\/td>\n<td style=\"width: 50%;\">If the remote repository doesn't have a branch with the same name, you can use<\/p>\n<pre>fatal: The current branch master has no upstream branch.\r\nTo push the current branch and set the remote as upstream, use\r\ngit push --set-upstream origin master<\/pre>\n<p>error. It's too much work to add this option every time.<\/p>\n<pre>git config --global push.default current<\/pre>\n<p>This can be avoided by specifying<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">git push -f<\/td>\n<td style=\"width: 50%;\">If your local branch and the remote branch have different commit histories, git push alone is rejected.<br \/>\nThis often happens when you push a local branch once, rebase the master branch to a development branch, and then re-push.<br \/>\nIn this case, you can use -f to force the remote branch to overwrite the local branch's content.<br \/>\nPlease use with caution in branches where multiple people are developing.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>git rebase<\/b><\/h3>\n<p>I'm using it instead of merge. I know there are many opinions, but I use rebase because I don't want to keep a commit log of the merge.<\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 227px;\" border=\"1\">\n<tbody>\n<tr style=\"height: 57px;\">\n<td style=\"width: 50%; height: 57px;\">git rebase .<\/td>\n<td style=\"width: 50%; height: 57px;\">Joins the current branch to the HEAD of the specified branch.<\/td>\n<\/tr>\n<tr style=\"height: 113px;\">\n<td style=\"width: 50%; height: 113px;\">git rebase -abort<\/td>\n<td style=\"width: 50%; height: 113px;\">Use this to undo a rebase and revert to before the rebase.<br \/>\nIf you have a conflict during rebase, you need to resolve the conflict, but it is difficult to resolve the conflict.<\/td>\n<\/tr>\n<tr style=\"height: 57px;\">\n<td style=\"width: 50%; height: 57px;\">git rebase -continue<\/td>\n<td style=\"width: 50%; height: 57px;\">After resolving conflicts, continue rebase.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>git stash<\/b><\/h3>\n<p>This is used to save the changes in the workspace without commit.<\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 90px;\" border=\"1\">\n<tbody>\n<tr style=\"height: 30px;\">\n<td style=\"width: 50%; height: 30px;\">git stash<\/td>\n<td style=\"width: 50%; height: 30px;\">Saves the contents of the workspace without commit.<\/td>\n<\/tr>\n<tr style=\"height: 30px;\">\n<td style=\"width: 50%; height: 30px;\">git stash -u<\/td>\n<td style=\"width: 50%; height: 30px;\">The untracked file is also included in the evacuation.<\/td>\n<\/tr>\n<tr style=\"height: 30px;\">\n<td style=\"width: 50%; height: 30px;\">git stash pop<\/td>\n<td style=\"width: 50%; height: 30px;\">Return the contents of the last stash to the workspace.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>git reflog<\/b><\/h3>\n<p>Displays the HEAD movement history.<br \/>\nIf you undo too many commits with git reset -hard or something similar, you can use<br \/>\nYou can use reflog to see where you want to go back, and then git reset -hard  to go back.<\/p>\n<h2><b>summary<\/b><\/h2>\n<p>In this article, I've introduced some git commands that I personally use frequently. If you're not familiar with git commands, it's hard to understand, but it's very useful. If you've never used it before, I recommend you learn it once!<\/p>\n<p>For programmers, reverting back to the original version of the code is an everyday occurrence when something goes wrong after editing a lot of code. However, it takes a lot of time and needless to say, human error will increase if you save the old version with the date and time and start a new work again every time you edit the code. Git is a tool to do such work efficiently and without waste.<\/p>","protected":false},"excerpt":{"rendered":"\u3053\u3093\u306b\u3061\u306f\u3002\u958b\u767a\u306e\u6a2a\u5c71\ud83d\udc08\u3067\u3059\u3002\u7686\u69d8 git \u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u304b\uff1f\u4eca\u3084\u958b\u767a\u3092\u884c\u3046\u4e0a\u3067git\u3092\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u306f [&hellip;]","protected":false},"author":6,"featured_media":4853,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":".p-entry__body h1  {\r\n  \/*\u7dda\u306e\u7a2e\u985e\uff08\u5b9f\u7dda\uff09 \u592a\u3055 \u8272*\/\r\n  border-bottom: solid 8px #3FA5F5;\r\n  display:inline-block;\r\n  font-size:36px;\r\n}\r\n\r\nh2 {\r\n  padding: 0.25em 0.5em;\/*\u4e0a\u4e0b \u5de6\u53f3\u306e\u4f59\u767d*\/\r\n  color: #494949;\/*\u6587\u5b57\u8272*\/\r\n  background: transparent;\/*\u80cc\u666f\u900f\u660e\u306b*\/\r\n  border-left: solid 5px #3FA5F5;\/*\u5de6\u7dda*\/\r\n  padding-left: 19px !important;\r\n  margin-bottom: 24px !important;\r\n}\r\n\r\n.yellowline {background:rgba(0, 0, 0, 0) linear-gradient(transparent 60%, #ffffbc 0%) repeat scroll 0 0;}\r\n\r\n\r\n.mokujimidasi{\r\n  position: relative;\r\n  padding: 0.6em 0.6em 0.6em 1.4em;\r\n  background: #e0edff;\r\n  font-weight:bold;\r\n}\r\n\r\n.mokujimidasi:after {\r\n  position: absolute;\r\n  content: '';\r\n  top: 100%;\r\n  left: 30px;\r\n  border: 15px solid transparent;\r\n  border-top: 15px solid #e0edff;\r\n  width: 0;\r\n  height: 0;\r\n}\r\n\r\n\r\nul.mokuji{\r\n    padding: 0.5em 1em;\r\n    font-weight: bold;\r\n    border: solid 3px #3b5998;\r\n}\r\n\r\nul.mokuji li{\r\n  line-height: 1.5;\r\n  padding: 0.5em 0;\r\n  list-style: none;\r\n}\r\n\r\n.p-entry__body ul.mokuji{\r\nmargin-left:0em;\r\n}\r\n\r\n.p-entry__body p{\r\nmargin-bottom:18px;\r\n}\r\n\r\n.box1 {\r\n    padding: 0.5em 1em;\r\n    margin: 2em 0;\r\n    font-weight: bold;\r\n    border: solid 3px #000000;\r\n}\r\n.box1 p {\r\n    margin: 0; \r\n    padding: 0;\r\n}\r\n\r\n.box28 {\r\n    position: relative;\r\n    margin: 2em 0;\r\n    padding: 25px 10px 7px;\r\n    border: solid 2px #FFC107;\r\n}\r\n.box28 .box-title {\r\n    position: absolute;\r\n    display: inline-block;\r\n    top: -2px;\r\n    left: -2px;\r\n    padding: 0 9px;\r\n    height: 25px;\r\n    line-height: 25px;\r\n    font-size: 17px;\r\n    background: #FFC107;\r\n    color: #ffffff;\r\n    font-weight: bold;\r\n}\r\n.box28 p {\r\n    margin: 0; \r\n    padding: 0;\r\n}\r\n\r\nh3 {\r\n  position: relative;\r\n  padding: 0.25em 0;\r\n}\r\nh3:after {\r\n  content: \"\";\r\n  display: block;\r\n  height: 4px;\r\n  background: -webkit-linear-gradient(to right, rgb(255, 186, 115), #ffb2b2);\r\n  background: linear-gradient(to right, rgb(255, 186, 115), #ffb2b2);\r\n}\r\n\r\n.box27 {\r\n    position: relative;\r\n    margin: 2em 0;\r\n    padding: 0.5em 1em;\r\n    border: solid 3px #62c1ce;\r\n}\r\n.box27 .box-title {\r\n    position: absolute;\r\n    display: inline-block;\r\n    top: -27px;\r\n    left: -3px;\r\n    padding: 0 9px;\r\n    height: 25px;\r\n    line-height: 25px;\r\n    font-size: 17px;\r\n    background: #62c1ce;\r\n    color: #ffffff;\r\n    font-weight: bold;\r\n    border-radius: 5px 5px 0 0;\r\n}\r\n.box27 p {\r\n    margin: 0; \r\n    padding: 0;\r\n}","veu_display_promotion_alert":"","vkexunit_cta_each_option":"0","_wpcom_ai_launchpad_first_post":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[6,7,40],"tags":[],"class_list":["post-4834","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-7","category-40"],"acf":[],"jetpack_featured_media_url":"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/06\/git-command.png","jetpack_shortlink":"https:\/\/wp.me\/pgMrZ4-1fY","_links":{"self":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/4834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/comments?post=4834"}],"version-history":[{"count":36,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/4834\/revisions"}],"predecessor-version":[{"id":4884,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/4834\/revisions\/4884"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/media\/4853"}],"wp:attachment":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/media?parent=4834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/categories?post=4834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/tags?post=4834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}