{"id":828,"date":"2023-08-22T13:22:16","date_gmt":"2023-08-22T13:22:16","guid":{"rendered":"https:\/\/tbekk.com\/devstream\/?p=828"},"modified":"2023-08-22T13:25:05","modified_gmt":"2023-08-22T13:25:05","slug":"git-concepts-commands-explained","status":"publish","type":"post","link":"https:\/\/tbekk.com\/devstream\/2023\/08\/22\/git-concepts-commands-explained\/","title":{"rendered":"GIT Concepts &amp; Commands Explained"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-text-color has-light-gray-color has-alpha-channel-opacity has-light-gray-background-color has-background is-style-wide\"\/>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>Link: <\/em><\/strong><a href=\"https:\/\/python.plainenglish.io\/git-explained-f4952f574089\"><em>python.plainenglish<\/em><\/a><\/li>\n\n\n\n<li><strong><em>Author:<\/em><\/strong> <a href=\"https:\/\/abhijit-paul.medium.com\/?source=post_page-----f4952f574089--------------------------------\"><em>Abhijit Paul<\/em><\/a><\/li>\n\n\n\n<li>, <em><strong>Publication date: <\/strong>June 17, 2023<\/em><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-light-gray-color has-alpha-channel-opacity has-light-gray-background-color has-background is-style-wide\"\/>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Git &amp; Github\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/KNIyO-i4hzQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"0aae\">What Is Git?<\/h1>\n\n\n\n<p id=\"94f7\">It is a open source, high-quality distributed version control system suitable for tracking modifications in source code in software development. It was originally created as an open-source system for coordinating tasks among programmers, but today it is widely used to track changes in any set of files. The key objectives of Git are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Speed and efficiency<\/li>\n\n\n\n<li>Data integrity<\/li>\n\n\n\n<li>Support for distributed and non-linear workflows<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"82f5\">What Is GitHub?<\/h1>\n\n\n\n<p id=\"a8e5\">It is a web-based Git repository. This hosting service has cloud-based storage. GitHub offers all distributed version control and source code management functionality of Git while adding its own features. It makes it easier to collaborate using Git.<\/p>\n\n\n\n<p id=\"c7e5\">GitHub is a collaboration and version control platform for storing and managing code. Using this tool, you can collaborate with others on projects from anywhere.<\/p>\n\n\n\n<p id=\"af53\">I have been using Git &amp; Github for couple of years by now for my personal projects as well as corporate ones.<\/p>\n\n\n\n<p id=\"4348\">This cheat sheet features the most important and commonly used Git commands for easy references.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"6688\">SETUP<\/h1>\n\n\n\n<p id=\"be80\">Configuring user information used across all local repositories.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git config --global user.name \"[firstname lastname]\"<\/pre>\n\n\n\n<p id=\"e0bf\"><em>set a name that is identifiable for credit when review version history<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git config --global user.email \"[valid-email]\"<\/pre>\n\n\n\n<p id=\"b4af\"><em>set an email address that will be associated with each history marker<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git config --global color.ui auto<\/pre>\n\n\n\n<p id=\"2f10\"><em>set automatic command line coloring for Git for easy reviewing<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"e9bc\">SETUP &amp; INIT<\/h1>\n\n\n\n<p id=\"19e7\">Configuring user information, initializing and cloning repositories<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git init<\/pre>\n\n\n\n<p id=\"d66d\"><em>initialize an existing directory as a Git repository<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git clone [url]<\/pre>\n\n\n\n<p id=\"2e8c\"><em>retrieve an entire repository from a hosted location via URL<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"abc2\">STAGE &amp; SNAPSHOT<\/h1>\n\n\n\n<p id=\"055b\">Working with snapshots and the Git staging area.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git status<\/pre>\n\n\n\n<p id=\"0918\"><em>show modified files in working directory, staged for your next commit<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git add [file]<\/pre>\n\n\n\n<p id=\"853f\"><em>add a file as it looks now to your next commit (stage) [copy the entire relative path as displayed after running git status]<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git reset [file]<\/pre>\n\n\n\n<p id=\"660e\"><em>unstage a file while retaining the changes in working directory<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git diff<\/pre>\n\n\n\n<p id=\"774b\"><em>diff of what is changed but not staged<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git diff --cached<\/pre>\n\n\n\n<p id=\"88c8\"><em>diff of what is staged but not yet commited<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git diff --name-only --cached<\/pre>\n\n\n\n<p id=\"0fb3\"><em>If you want to see only the file names, then run the same command with the name-only option<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git commit -m \"[descriptive message]\"<\/pre>\n\n\n\n<p id=\"a0b6\"><em>commit your staged content as a new commit snapshot<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"96a9\">BRANCH &amp; MERGE<\/h1>\n\n\n\n<p id=\"8a15\">Isolating work in branches, changing context, and integrating changes<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git branch<\/pre>\n\n\n\n<p id=\"0cad\"><em>list your all branches. A * will appear next to the currently active branch<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git branch [branch-name]<\/pre>\n\n\n\n<p id=\"b2b2\"><em>create a new branch at the current commit<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git checkout -b [branch-name]<\/pre>\n\n\n\n<p id=\"3f5f\"><em>switch to another branch and check it out into your working directory<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git switch [branch-name]<\/pre>\n\n\n\n<p id=\"484b\"><em>switch to another branch without any checkouts<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git merge [branch]<\/pre>\n\n\n\n<p id=\"cb2b\"><em>merge the specified branch\u2019s history into the current one<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"4fda\">INSPECT &amp; COMPARE<\/h1>\n\n\n\n<p id=\"76b9\">Examining logs, diffs and object information<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git log<\/pre>\n\n\n\n<p id=\"7447\"><em>show the commit history for the currently active branch<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git log --graph --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%an%C(reset)%C(bold yellow)%d%C(reset) %C(dim white)- %s%C(reset)' --all<\/pre>\n\n\n\n<p id=\"05a6\"><em>Using&nbsp;<\/em><code><em>--graph<\/em><\/code><em>&nbsp;and&nbsp;<\/em><code><em>--format<\/em><\/code><em>&nbsp;we can quickly get a summary view of git commits in our project having more visibility<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git log [branchB]..[branchA]<\/pre>\n\n\n\n<p id=\"73c4\"><em>show the commits on branchA that are not on branchB<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git log --follow [file]<\/pre>\n\n\n\n<p id=\"47ce\"><em>show the commits that changed file, even across renames<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git diff [branchB]\u2026[branchA]<\/pre>\n\n\n\n<p id=\"3434\"><em>show the diff of what is in branchA that is not in branchB<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git show [SHA]<\/pre>\n\n\n\n<p id=\"44cf\"><em>show any object in Git in human-readable format<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"cb82\">TRACKING PATH CHANGES<\/h1>\n\n\n\n<p id=\"eb7f\">Versioning file removes and path changes<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git rm [file]<\/pre>\n\n\n\n<p id=\"c801\"><em>delete the file from project and stage the removal for commit<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git mv [existing-path] [new-path]<\/pre>\n\n\n\n<p id=\"cda0\"><em>change an existing file path and stage the move<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git log --stat -M<\/pre>\n\n\n\n<p id=\"b6c5\"><em>show all commit logs with indication of any paths that moved<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git clean<\/pre>\n\n\n\n<p id=\"9a7f\"><em>Removes untracked files from the working directory.<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"2c90\">SHARE &amp; UPDATE<\/h1>\n\n\n\n<p id=\"3f18\">Retrieving updates from another repository and updating local repos<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git remote add [alias] [url]<\/pre>\n\n\n\n<p id=\"2850\"><em>add a git URL as an alias<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git fetch [alias]<\/pre>\n\n\n\n<p id=\"e1aa\"><em>fetch down all the branches from that Git remote<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git merge [alias]\/[branch]<\/pre>\n\n\n\n<p id=\"4435\"><em>merge a remote branch into your current branch to bring it up to date<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git push [alias] [branch]<\/pre>\n\n\n\n<p id=\"83d0\"><em>Transmit local branch commits to the remote repository branch<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git pull<\/pre>\n\n\n\n<p id=\"71d5\"><em>fetch and merge any commits from the tracking remote branch<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"37ba\">REWRITE HISTORY<\/h1>\n\n\n\n<p id=\"bd25\">Rewriting branches, updating commits and clearing history<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git rebase [branch]<\/pre>\n\n\n\n<p id=\"1609\"><em>apply any commits of current branch ahead of specified one<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git reset --hard [commit]<\/pre>\n\n\n\n<p id=\"02a9\"><em>clear staging area, rewrite working tree from specified commit<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"9437\">TEMPORARY COMMITS<\/h1>\n\n\n\n<p id=\"b194\">Temporarily store modified, tracked files in order to change branches<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git stash<\/pre>\n\n\n\n<p id=\"2fec\"><em>Save modified and staged changes<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git stash list<\/pre>\n\n\n\n<p id=\"cd4a\"><em>list stack-order of stashed file changes<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git stash pop<\/pre>\n\n\n\n<p id=\"9ca1\"><em>write working from top of stash stack<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git stash drop<\/pre>\n\n\n\n<p id=\"d799\"><em>discard the changes from top of stash stack<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"c25a\">IGNORING PATTERNS using .gitignore<\/h1>\n\n\n\n<p id=\"6831\">Preventing unintentional staging or committing of files<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">logs\/ *.notes pattern*\/<\/pre>\n\n\n\n<p id=\"38d1\">Save a file with desired patterns as .gitignore with either direct string matches or wildcard globs<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git config --global core.excludesfile [file]<\/pre>\n\n\n\n<p id=\"f5b5\">system wide ignore pattern for all local repositories<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"accc\"><strong>Few important concepts explained\u2026<\/strong><\/p>\n<\/blockquote>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"692d\">git merge vs git rebase<\/h1>\n\n\n\n<p id=\"6166\">When there are changes on the&nbsp;<code>main or master<\/code>&nbsp;branch that you want to incorporate into your local branch, you can either&nbsp;<em>merge<\/em>&nbsp;the changes in or&nbsp;<em>rebase<\/em>&nbsp;your branch from a different point.<\/p>\n\n\n\n<p id=\"d4b3\"><strong>merge<\/strong>&nbsp;takes the changes from one branch and merges them into another branch in one&nbsp;<em>merge commit<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git merge origin\/main your-local-branch<\/pre>\n\n\n\n<p id=\"b5a5\"><strong>rebase<\/strong>&nbsp;adjusts the point at which a local branch actually branched off from base branch (i.e. moves the branch to a new starting point from the base branch).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git rebase origin\/main your-local-branch<\/pre>\n\n\n\n<p id=\"931b\"><em>Remember,<\/em>&nbsp;you\u2019ll use&nbsp;<code>rebase<\/code>&nbsp;when there are changes in an upstream branch (like&nbsp;<code>main or master<\/code>) that you want to include in your local branch. You\u2019ll use&nbsp;<code>merge<\/code>&nbsp;when there are changes in a local branch that you want to put back into&nbsp;<code>main or master<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">hint: its remote counterpart. Integrate the remote changes (e.g.<br>hint: 'git pull ...') before pushing again.<br>hint: See the 'Note about fast-forwards' in 'git push --help' for details.<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"a159\">Resources<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/git-scm.com\/docs\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/git-scm.com\/docs<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/joshnh\/Git-Commands\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/github.com\/joshnh\/Git-Commands<\/a><\/li>\n<\/ul>\n\n\n\n<p id=\"aa9c\">Please leave a comment or follow to support.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What Is Git? It is a open source, high-quality distributed version control system suitable for tracking modifications in source code in software development. It was originally created as an open-source&#8230; <a class=\"read-more-link\" href=\"https:\/\/tbekk.com\/devstream\/2023\/08\/22\/git-concepts-commands-explained\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,248,296],"tags":[293,295,297],"class_list":["post-828","post","type-post","status-publish","format-standard","hentry","category-article","category-software-development","category-version-control","tag-git","tag-github","tag-guide"],"_links":{"self":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/comments?post=828"}],"version-history":[{"count":1,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/828\/revisions"}],"predecessor-version":[{"id":829,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/828\/revisions\/829"}],"wp:attachment":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/media?parent=828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/categories?post=828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/tags?post=828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}