{"id":3537,"date":"2016-05-10T10:14:17","date_gmt":"2016-05-10T14:14:17","guid":{"rendered":"http:\/\/www.acarlstein.com\/?p=3537"},"modified":"2018-08-24T17:52:30","modified_gmt":"2018-08-24T21:52:30","slug":"git-useful-commands","status":"publish","type":"post","link":"http:\/\/blog.acarlstein.com\/?p=3537","title":{"rendered":"Git: Useful Commands"},"content":{"rendered":"<p>Below are a few most common commands that are useful to know:<\/p>\n<ul>\n<li>Clean up your branch list of\u00a0branches that doesn&#8217;t exist any longer on the remote repository:\u00a0\n<ul>\n<li>\n<pre>git fetch --prune<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Delete local branch in local repository:\n<ul>\n<li>\n<pre>git branch -D &lt;branch_name&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Delete remote branch in remote repository:\n<ul>\n<li>\n<pre>git push origin --delete &lt;branch_name&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Show which files are tracked or not:\n<ul>\n<li>\n<pre>git status<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Track all changes you have done so far:\n<ul>\n<li>\n<pre>git add .\r\n\r\n<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Commit your tracked changes to your local repository:\n<ul>\n<li>\n<pre>git commit<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Revert addition done in git:\n<ul>\n<li>\n<pre>git reset<\/pre>\n<\/li>\n<li>\n<pre>git reset &lt;filename&gt;<\/pre>\n<\/li>\n<li>\n<pre>git rm --cached &lt;filename&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Submit your tracked changes, saved in your local repository, to the remote repository:\n<ul>\n<li>\n<pre>git push -u origin &lt;branch_name&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Merge one branch into your current branch:\n<ul>\n<li>\n<pre>git merge --no-ff &lt;other_branch&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Change branches:\u00a0\n<ul>\n<li>\n<pre>git checkout &lt;branch_name&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Create a new branch from current branch:\n<ul>\n<li>\n<pre>git checkout -b &lt;new_branch_name&gt;\r\n\r\n<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>Revert file to its state in master branch:\n<ul>\n<li>\n<pre>git checkout origin\/master &lt;filename&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Find and Restore a deleted file from repository:<br \/>\n(First we find if the file exist then we retrieve it)<\/p>\n<ol>\n<li>\n<pre>git rev-list -n 1 HEAD -- &lt;FILE_PATH\/FILENAME&gt;<\/pre>\n<\/li>\n<li>\n<pre>git checkout &lt;branch_name&gt;^ -- &lt;FILE_PATH\/FILENAME&gt;<\/pre>\n<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<ul>\n<li>Save in the stash your files changes (add -u or &#8211;include-untracked if needed):\n<ul>\n<li>\n<pre>git stash\r\ngit stash --include-untracked\r\ngit stash -u<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Apply your file changes saved in the stash into your current branch:\n<ul>\n<li>\n<pre>git stash pop\u00a0<\/pre>\n<p>or<\/li>\n<li>\n<pre>git stash apply<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>History of an specific file that you don&#8217;t know where reside:\n<ul>\n<li>\n<pre>git log --all &quot;**\/file.groovy&quot;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Stash untracked files without staging them (Version 1.7.7+):\n<ul>\n<li>\n<pre>git stash save -u<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Get all the stuff from remote repository:\n<ul>\n<li>\n<pre>git pull<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Problems when pulling? Asking you to retry a link issue? Try the garbage collector\n<ul>\n<li>\n<pre>git gc<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Reset local branch to origin (remote) version\n<ul>\n<li>\n<pre>git reset --hard origin\/&lt;branch_name&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Change Local and Remove branch name\u00a0\n<ul>\n<li>\n<pre>git branch -m &lt;branch_new_name&gt;<\/pre>\n<ul>\n<li>\n<pre>git branch -m &lt;branch_old_name&gt; &lt;branch_new_name&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<pre>git push origin :&lt;branch_old_name&gt; &lt;branch_new_name&gt;<\/pre>\n<\/li>\n<li>\n<pre>git push origin -u &lt;branch_new_name&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n<script>\nvar zbPregResult = '0';\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Below are a few most common commands that are useful to know: Clean up your branch list of\u00a0branches that doesn&#8217;t exist any longer on the remote repository:\u00a0 git fetch &#8211;prune Delete local branch in local repository: git branch -D &lt;branch_name&gt; Delete remote branch in remote repository: git push origin &#8211;delete &lt;branch_name&gt; Show which files are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1458],"tags":[1459],"class_list":["post-3537","post","type-post","status-publish","format-standard","hentry","category-git","tag-git"],"_links":{"self":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/3537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3537"}],"version-history":[{"count":16,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/3537\/revisions"}],"predecessor-version":[{"id":4039,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/3537\/revisions\/4039"}],"wp:attachment":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3537"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}