{"id":435,"date":"2017-09-14T11:31:51","date_gmt":"2017-09-14T11:31:51","guid":{"rendered":"http:\/\/www.codeinsightacademy.com\/blog\/?page_id=435"},"modified":"2024-12-24T03:28:03","modified_gmt":"2024-12-24T03:28:03","slug":"git","status":"publish","type":"page","link":"https:\/\/codeinsightacademy.com\/blog\/devops\/git\/","title":{"rendered":"Git"},"content":{"rendered":"<p><strong>Git Commands<\/strong><\/p>\n<ol>\n<li><strong>git symbolic-ref HEAD<\/strong><\/li>\n<li><strong>git log &#8211;oneline &#8211;graph &#8211;decorate &#8211;all<\/strong><\/li>\n<li>git &#8211;version<\/li>\n<li>git config &#8211;list<\/li>\n<li>git config &#8211;global user.name &#8220;Hello World&#8221;<\/li>\n<li>git config &#8211;global user.emao &#8220;hello@world.com&#8221;<\/li>\n<\/ol>\n<hr \/>\n<ol>\n<li>clear<\/li>\n<li>git config user.email<\/li>\n<li>git help<\/li>\n<li>git help commit<\/li>\n<\/ol>\n<hr \/>\n<p><strong>Linux Commands<\/strong><\/p>\n<ol>\n<li>pwd<\/li>\n<li>cd ~<\/li>\n<li>cd ..<\/li>\n<li>ls<\/li>\n<li>ls -l<\/li>\n<li>ls -lrth<\/li>\n<li>cd folder<\/li>\n<\/ol>\n<hr \/>\n<p><strong>Git\u00a0Basic Commands<\/strong><\/p>\n<ol>\n<li>git init<\/li>\n<li>ls -la<\/li>\n<li>git status<\/li>\n<li>git add filename.ext<\/li>\n<li>git add .<\/li>\n<li>git commit -m &#8220;Take snapshot till this point&#8221;<\/li>\n<li>git commit -am &#8220;add and commit at the same time&#8221;<\/li>\n<li>git log<\/li>\n<li>git log &#8211;author=&#8221;Hello World&#8221;<\/li>\n<li>git log &#8211;oneline<\/li>\n<\/ol>\n<hr \/>\n<ul>\n<li>working copy &gt;&gt; staging area &gt;&gt; repository<\/li>\n<\/ul>\n<hr \/>\n<ol>\n<li>git diff<\/li>\n<li>git diff filename.ext<\/li>\n<li>git diff &#8211;staged<\/li>\n<\/ol>\n<hr \/>\n<ol>\n<li>git rm filename.ext<\/li>\n<\/ol>\n<hr \/>\n<p>Rename file<\/p>\n<p>Way 1<\/p>\n<ol>\n<li>git add newfilename.ext<\/li>\n<li>git rm oldfilename.ext<\/li>\n<\/ol>\n<p>Way 2<\/p>\n<ol>\n<li>git mv oldfilename.ext newfilename.ext<\/li>\n<\/ol>\n<hr \/>\n<p>Undo changes in file (copy repo file to local file)<\/p>\n<ol>\n<li>git checkout &#8212; filename.ext<\/li>\n<li>git reset HEAD filename.ext<\/li>\n<\/ol>\n<hr \/>\n<ul>\n<li>REVERTING THE CHANGES<br \/>\ngit checkout versionnumber(few chars) &#8212; filename.ext<\/li>\n<li>Pull Request<\/li>\n<li>Watch<\/li>\n<li>Star<\/li>\n<li>Fork<\/li>\n<li>Github issues and Labels<\/li>\n<li>Github Wiki<\/li>\n<li>Git Organization<\/li>\n<\/ul>\n<hr \/>\n<p><strong>Useful\u00a0git commands for production<\/strong>:<\/p>\n<hr \/>\n<p><strong>Logs<\/strong><\/p>\n<p>git log &#8211;oneline<br \/>\nTo display commit log in one line format<\/p>\n<pre><code>git diff-tree --no-commit-id --name-only -r &lt;commit_id&gt;<\/code> <code>git show --pretty=\"\" --name-only &lt;commit_id&gt;<\/code> <code>git ls-tree --name-only -r &lt;commit_id&gt;\ngit show --name-only &lt;sha&gt;<\/code> <code>git show --stat --oneline HEAD<\/code><\/pre>\n<hr \/>\n<p><strong>Branching<\/strong><\/p>\n<p>git branch<br \/>\nTo show all existing branches on local machine<\/p>\n<p>git checkout -b NewBranch<br \/>\nTo create new branch from master branch<\/p>\n<p>git checkout Branch<br \/>\nTo switch from current branch to specified branch<\/p>\n<p>git branch -d BranchName<br \/>\nTo delete specific branch<\/p>\n<p>git push origin :branchname<br \/>\nOR<br \/>\ngit push origin &#8211;delete :branchname<br \/>\nTo delete remote branch<\/p>\n<hr \/>\n<p><strong>Merging Changes from Another Branch to Current Branch<\/strong><\/p>\n<p>git checkout target-branch<br \/>\nSwitch to target branch e.g. master or dev<\/p>\n<p>git merge source-branch<br \/>\nMerge change from source-branch to current branch e.g. git merge CHG001<\/p>\n<hr \/>\n<p><strong>Revert Uncommit Changes<\/strong><\/p>\n<p>git checkout &#8212; file<br \/>\nTo revert all uncommitted changes for specified file<\/p>\n<p>git checkout &#8212; .<br \/>\nTo revert all uncommitted changes for all files<\/p>\n<hr \/>\n<p><strong>Revert Commited Changes<\/strong><\/p>\n<p>git revert [-n] hashcommitcode<br \/>\nTo revert commited changes for hashcode<\/p>\n<hr \/>\n<p><strong>Reset Pushed Changes<\/strong><\/p>\n<p>git reset &#8211;hard hashcommitid<br \/>\nTo reset till specified hashcommitcode<\/p>\n<hr \/>\n<p><strong>Push after reset<\/strong><\/p>\n<p>git push -f origin master<br \/>\nTo push changes forcefully to git repository<\/p>\n<hr \/>\n<p>In Git, commands like <strong>reset<\/strong>, <strong>rebase<\/strong>, <strong>revert<\/strong>, and <strong>restore<\/strong> serve distinct purposes. Here&#8217;s a detailed breakdown:<\/p>\n<hr \/>\n<h3>1. <strong>git reset<\/strong><\/h3>\n<ul>\n<li><strong>Purpose<\/strong>: Moves the HEAD pointer and optionally modifies the working directory and staging area.<\/li>\n<li><strong>Use Case<\/strong>: Undo commits or unstage changes.<\/li>\n<li><strong>Types<\/strong>:\n<ul>\n<li><strong>Soft<\/strong>: Moves HEAD, but keeps changes staged.\n<pre><code class=\"language-bash\">git reset --soft &lt;commit&gt;\n<\/code><\/pre>\n<\/li>\n<li><strong>Mixed (default)<\/strong>: Moves HEAD and un-stages changes, but keeps them in the working directory.\n<pre><code class=\"language-bash\">git reset &lt;commit&gt;\n<\/code><\/pre>\n<\/li>\n<li><strong>Hard<\/strong>: Moves HEAD and deletes all changes in the staging area and working directory.\n<pre><code class=\"language-bash\">git reset --hard &lt;commit&gt;\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>2. <strong>git rebase<\/strong><\/h3>\n<ul>\n<li><strong>Purpose<\/strong>: Reapplies commits from one branch onto another in a linear fashion.<\/li>\n<li><strong>Use Case<\/strong>: Keep commit history clean by avoiding merge commits.<\/li>\n<li><strong>Types<\/strong>:\n<ul>\n<li><strong>Interactive Rebase<\/strong>:\n<pre><code class=\"language-bash\">git rebase -i &lt;base&gt;\n<\/code><\/pre>\n<p>Lets you squash, reorder, or edit commits.<\/li>\n<li><strong>Regular Rebase<\/strong>:\n<pre><code class=\"language-bash\">git rebase &lt;base&gt;\n<\/code><\/pre>\n<p>Applies commits from the current branch onto <code>&lt;base&gt;<\/code>.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Key Note<\/strong>: Rewriting history with rebase is fine for local branches, but avoid using it on shared branches.<\/li>\n<\/ul>\n<hr \/>\n<h3>3. <strong>git revert<\/strong><\/h3>\n<ul>\n<li><strong>Purpose<\/strong>: Creates a new commit that undoes the changes made by a previous commit.<\/li>\n<li><strong>Use Case<\/strong>: Safely undo a commit without rewriting history.<\/li>\n<li><strong>Example<\/strong>:\n<pre><code class=\"language-bash\">git revert &lt;commit&gt;\n<\/code><\/pre>\n<p>This creates a new commit that negates the changes introduced by <code>&lt;commit&gt;<\/code>.<\/li>\n<li><strong>Key Note<\/strong>: Unlike reset, it doesn\u2019t alter history and is suitable for public\/shared branches.<\/li>\n<\/ul>\n<hr \/>\n<h3>4. <strong>git restore<\/strong><\/h3>\n<ul>\n<li><strong>Purpose<\/strong>: Restores working directory files or staging area files to their original state.<\/li>\n<li><strong>Use Case<\/strong>: Discard changes in a file or unstage changes without affecting the commit history.<\/li>\n<li><strong>Examples<\/strong>:\n<ul>\n<li>Restore a file in the working directory:\n<pre><code class=\"language-bash\">git restore &lt;file&gt;\n<\/code><\/pre>\n<\/li>\n<li>Unstage changes:\n<pre><code class=\"language-bash\">git restore --staged &lt;file&gt;\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li><strong>Key Note<\/strong>: A safer alternative to <code>git reset<\/code> for managing uncommitted changes.<\/li>\n<\/ul>\n<hr \/>\n<h3>Summary Table<\/h3>\n<table>\n<thead>\n<tr>\n<th><strong>Command<\/strong><\/th>\n<th><strong>Main Action<\/strong><\/th>\n<th><strong>Scope<\/strong><\/th>\n<th><strong>Safe for Shared Branches?<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>reset<\/strong><\/td>\n<td>Moves HEAD pointer; optionally modifies working\/staging<\/td>\n<td>Undo commits or unstage files<\/td>\n<td>No<\/td>\n<\/tr>\n<tr>\n<td><strong>rebase<\/strong><\/td>\n<td>Reapplies commits to a new base<\/td>\n<td>Clean up commit history<\/td>\n<td>No (rewrites history)<\/td>\n<\/tr>\n<tr>\n<td><strong>revert<\/strong><\/td>\n<td>Creates a new commit that undoes changes<\/td>\n<td>Undo changes safely<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td><strong>restore<\/strong><\/td>\n<td>Resets files in working\/staging area<\/td>\n<td>Discard or unstage changes<\/td>\n<td>Yes<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<p>Choose the command based on whether you&#8217;re working locally or on a shared branch and whether you want to rewrite history or preserve it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Git Commands git symbolic-ref HEAD git log &#8211;oneline &#8211;graph &#8211;decorate &#8211;all git &#8211;version git config &#8211;list git config &#8211;global user.name &#8220;Hello World&#8221; git config &#8211;global user.emao &#8220;hello@world.com&#8221; clear git config user.email git help git help commit Linux Commands pwd cd ~ cd .. ls ls -l ls -lrth cd folder Git\u00a0Basic Commands git init ls [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":483,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/pages\/435"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=435"}],"version-history":[{"count":18,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/pages\/435\/revisions"}],"predecessor-version":[{"id":2798,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/pages\/435\/revisions\/2798"}],"up":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/pages\/483"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}