<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: OmarElsamahy</title>
    <description>The latest articles on DEV Community by OmarElsamahy (@sama7y).</description>
    <link>https://dev.to/sama7y</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1267176%2Ff1cabb91-6375-4099-8fc7-c6907e5dcd7d.png</url>
      <title>DEV Community: OmarElsamahy</title>
      <link>https://dev.to/sama7y</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sama7y"/>
    <language>en</language>
    <item>
      <title>Navigating Git History: A Guide to git log, git checkout and git switch</title>
      <dc:creator>OmarElsamahy</dc:creator>
      <pubDate>Sun, 04 Feb 2024 14:20:53 +0000</pubDate>
      <link>https://dev.to/sama7y/navigating-git-history-a-guide-to-git-log-git-checkout-and-git-switch-4kbf</link>
      <guid>https://dev.to/sama7y/navigating-git-history-a-guide-to-git-log-git-checkout-and-git-switch-4kbf</guid>
      <description>&lt;p&gt;As developers, we've all been there – navigating the vast sea of Git commits, feeling lost in a complex maze. It can be a daunting task, trying to make sense of our version history and finding our way out of a seemingly endless commit.&lt;/p&gt;

&lt;p&gt;But fear not, for in these moments lies the importance of mastering Git commands that not only provide us with insights but also serve as a lifeline when things go awry. In this journey through the capabilities of Git, we'll dive into the invaluable &lt;strong&gt;git log&lt;/strong&gt;, &lt;strong&gt;git checkout&lt;/strong&gt; and &lt;strong&gt;git switch&lt;/strong&gt; commands.&lt;/p&gt;

&lt;p&gt;They are not simply lines of code, but rather powerful allies. Have you ever wanted to revisit a specific commit, desperate to undo a merge gone wrong or about creating a new branch tied to an older commit? Worry not, as these commands will become guide you in achieving these tasks.&lt;/p&gt;

&lt;p&gt;In this guide, we'll showcase their powers how they can be your escape hatch when working with Git.&lt;/p&gt;

&lt;p&gt;Basic Usage:&lt;br&gt;
&lt;code&gt;git log&lt;/code&gt;&lt;br&gt;
Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;commit abc123
Author: John Doe &amp;lt;john.doe@example.com&amp;gt;
Date:   2022-01-27

    Updated README.md

commit def456
Author: Jane Smith &amp;lt;jane.smith@example.com&amp;gt;
Date:   2022-01-26

    Added new feature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the default git log usage that shows the commit SHA, author, date and the commit message&lt;/p&gt;

&lt;p&gt;we can also have different args for that command as:&lt;br&gt;
&lt;code&gt;git log --pretty=format:"%h %ad | %s%d [%an]" --date=short&lt;/code&gt;&lt;br&gt;
This command helps us format the output in a different presentation like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;commit abc123 | 2022-01-27 | Updated README.md [John Doe]
commit def456 | 2022-01-26 | Added new feature [Jane Smith]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could also search for a specific commit with its author, message or date.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git log --author="John Doe"
git log --grep="bug fix"
git log --since="2024-01-01" --until="2024-01-21"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now for &lt;strong&gt;git checkout&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The git checkout command is versatile and is used for various purposes, including switching branches, creating new branches, and navigating through commits.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When used with a branch name, it switches your working directory to that branch.&lt;/li&gt;
&lt;li&gt;When used with a commit SHA, it puts you in a "detached HEAD" state, allowing you to explore the commit without being on a specific branch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Switch to a Specific branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout main
git checkout -b new-feature-branch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Switch to a Specific Commit (Detached HEAD State):&lt;br&gt;
&lt;code&gt;git checkout abc123&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now for &lt;strong&gt;git switch&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git switch branch-name

This command structure is for switching between existing branches like git checkout.

git switch -c new-local-branch origin/remote-branch

This command is used to establish a new local branch that is rooted in a remote tracking branch.

git switch -c new-branch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The -c option for create makes the process more concise and aligns with the common use case of creating and switching branches.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git log
git checkout commit-SHA
git switch -c new-branch-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After git log you select the commit you want to branch from you should be able to git switch and now you have created a new local branch of it and able to make all required changes &lt;/p&gt;

&lt;p&gt;don't forget to push it after committing your changes to your repo and setting your upstream&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git push --set-upstream origin new-branch-name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This streamlined flow aids in making parallel changes for a root branch and also allows us to explore historical commits and branch off of them.&lt;/p&gt;

</description>
      <category>git</category>
      <category>softwaredevelopment</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
