avatarRafiullah Hamedy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

7298

Abstract

figcaption></figure><p id="3288">This command is used to show your changes to versioned files. The <code>git diff</code> for <code>README.md</code> shows the lines that are added in green and lines removed in red, including the # of lines added and removed.</p><h2 id="886e">The “git add” command</h2><figure id="ccbd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VUqIsZlhQ3GgPia0-vAQtw.png"><figcaption>Show unstaged changes</figcaption></figure><p id="6043">The <code>git status</code> shows that we have already staged the <code>.gitignore</code> file, however, the <code>README.md</code> and <code>pom.xml</code> file’s changes are not yet staged.</p><figure id="4f76"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ZZwNPxQRznDSHDx870N9Tw.png"><figcaption>The changes are staged after git add . command</figcaption></figure><p id="a466">Let’s stage the <code>README.md</code> and <code>pom.xml</code> files and to do that we run <code>git add README.md pom.xml</code> or <code>git add .</code> to stage changes in the current directory.</p><h2 id="4336">The “git commit” command</h2><p id="8c2b">This command commits your changes to the git branch. Let’s commit the changes shown in the above screenshot.</p><figure id="c39b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*6X7vp220i3abuc1Bu8xyVg.png"><figcaption>Commit the staged files</figcaption></figure><p id="371c">The first commit adds the <code>.gitignore</code> with a clear message and the second commit adds the rest of the changes and <code>git status</code> will show no more changes.</p><h2 id="0bc8">The “git log” command</h2><figure id="24f2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1cmrNzn49B29iGXyJ3GFEQ.png"><figcaption>The git log command</figcaption></figure><p id="3e3d">Use this command to see the commit history of the current git branch.</p><p id="2c37">You can pass the <code>--oneline</code> flag to the <code>git log</code> to only show short commit SHA and the commit message.</p><p id="7c15">You can also pass the <code>--graph</code> and <code>--pretty</code> flags to the <code>git log</code> command.</p><figure id="214b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ING2Cj8MUVCqaQOK0Ww4Sw.png"><figcaption>The commit messages are pure genius.</figcaption></figure><h2 id="384d">The “git push” command</h2><p id="2b8a">Now that we have committed some changes to the local repository, as shown above, let’s push the changes to the upstream remote repository.</p><figure id="b741"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Q9rmTgjOymMmRW0z-wziLA.png"><figcaption>Push changes up using git push</figcaption></figure><p id="7379">Now the local branch<code>repository-setup</code> is pushed to the upstream repository, and you should see it in the UI, i.e., Github.</p><h2 id="317e">The “git stash” command</h2><p id="7aaf">Let’s say you have made some changes and would like to save them and revert the working branch to the state before your changes, HEAD commit.</p><p id="cea6">I pushed the <code>repository-setup</code> branch upstream and then created a pull request against the <code>develop</code>branch in Github and merged it. Now my local <code>develop</code> branch does not have the changes in the upstream <code>develop</code> branch, and I also made some changes in the develop locally.</p><figure id="fd7d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*u15xTcowESgH_fwymWQN2w.png"><figcaption>Cannot git pull upstream develop because of local changes to the files</figcaption></figure><p id="2f5a">The above screenshot shows the local changes as well as the output of the <code>git pull upstream develop</code> that says</p><blockquote id="a2ac"><p>Please commit your changes or stash them before you merge.</p></blockquote><figure id="2564"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*a24lR8y3L0l-7kyhcEIBzw.png"><figcaption>Stash local changes and pull the upstream</figcaption></figure><p id="ba3f">So let’s <code>stage</code> all the local changes using <code>git add .</code> and then stash (save) them away so we can pull upstream changes.</p><p id="a8a3">As shown in the screenshot, we have successfully pulled the upstream changes.</p><p id="aa96">You might be wondering, what happened to my stashed changes? Use the <code>git stash list</code> command to see a list of all stashes as shown below</p><div id="dc97"><pre><span class="hljs-attribute">stash</span>@{<span class="hljs-number">0</span>}: WIP <span class="hljs-literal">on</span> develop: c44e545 Initial commit</pre></div><p id="6dde">To apply your most recent stashed changes into your current working branch, you can use the command <code>git stash pop</code> (removes the stashed item from the top of the stash list) or <code>git stash apply</code> (keeps the stash entry in the stash list).</p><p id="edfa">For more on git stash run <code>git stash --help</code> or read the documentation <a href="https://git-scm.com/docs/git-stash">https://git-scm.com/docs/git-stash</a></p><h2 id="8423">The “git rebase” command</h2><p id="7469">As shown in the drawing below, a rebase is taking all your changes (commits) and putting it on top of the branch’s HEAD commit. Let’s say; you checked out <code>develop</code> a week ago to work on a <code>feature-todo</code> and after one week, you have ten commits, and you are ready to push your changes upstream. During this one week, your colleagues have been merging changes to the <code>develop</code> branch, and that means your changes are on top of an old commit.</p><p id="7439">A rebase takes your changes from the old commit and put it on top of the HEAD commit of the branch.</p><figure id="d299"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nILRXseiC75Vq-lbLOjMpg.jpeg"><figcaption>Source: <a href="https://www.maxwellantonucci.com/2017/09/24/the-git-rebase-introduction-i-wish-i'd-had">https://www.maxwellantonucci.com/2017/09/24/the-git-rebase-introduction-i-wish-i'd-had</a></figcaption></figure><p id="d367">So I created two branches of <code>develop</code></p><ul><li>The branch (1) <code>update-readme-file</code></li><li>The branch (2) <code>feature-todo</code></li></ul><figure id="b9de"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*YyKpm1KZxTHpa8A0910lcA.png"><figcaption>Sublime Merge — shows the status of branches</figcaption></figure><p id="a852">Added some commits to the the<code>update-readme-file</code>, pushed the changes up and merged the pull request to the <code>develop</code> branch.</p><p id="e526">The <code>feature-todo</code> branch, as shown in the screenshot, does not have the recent <code>develop</code> changes (from <code>update-readme-file</code>). Let’s rebase <code>feature-todo</code> on top of the <code>develop</code> branch.</p><p id="47f1">Here is the series of commands</p><div id="fb7f"><pre>git checkout develop git pull upstream develop git checkout <span class="hljs-built_in">feature</span>-todo git rebase develop</pre></div><p id="bda4">Since the <code>README.md</code> file is changed in the<code>develop</code> and also in our branch <code>feature-todo</code>, rebase will flag that. We can do the following to

Options

resolve the conflicts</p><ul><li>Edit the <code>README.md</code> file and make the necessary changes to resolve the conflict</li><li>Stage the <code>README.md</code> file</li></ul><p id="af22">Now you can continue the rebase with <code>git rebase --continue</code> command and if you wish to abort the rebase then <code>git rebase --abort</code>. A successful rebase would put our commits (top 3) on top of the <code>develop</code> HEAD commit as shown below</p><figure id="cc90"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*xtxpT_uIK5w8Sj6qc8R1kg.png"><figcaption>git rebase result</figcaption></figure><p id="fa79">You can also use git rebase to squash one or more commits together. Let’s say you have four and wish to squash them to one</p><figure id="5a53"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*OmikFNMrnuDh9bw9R7sDDQ.png"><figcaption>Git rebase to squash commits</figcaption></figure><p id="7ff3">The above will allow you to mark each of the commits whether you want to <code>keep</code> them or <code>squash (s)</code> them. I often squash my commits before pushing my changes upstream.</p><h2 id="a245">The “git merge” command</h2><p id="33b5">One difference between <code>git merge</code> from <code>git rebase</code> is that <code>git merge</code> creates a merge commit, and majority developers don’t like the extra merge commit.</p><figure id="8418"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Jzp7pSDzzJI5sTvGzucoQQ.gif"><figcaption>A git merge behind the scene</figcaption></figure><p id="bbee">Let’s say you want to merge changes from <code>develop</code> to the <code>canary</code> branch and you can do that by</p><div id="d53b"><pre>git checkout canary git <span class="hljs-built_in">merge</span> --ff-<span class="hljs-keyword">only</span> develop</pre></div><p id="e138">For more on how merge works see <code>git merge --help</code> or the documentation <a href="https://git-scm.com/docs/git-merge">https://git-scm.com/docs/git-merge</a>.</p><h2 id="3265">The “git fetch” command</h2><p id="803a">I use the <code>git fetch --all</code> to pull down the remote branches and tags.</p><h2 id="9682">The “git cherry-pick” command</h2><p id="e121">As the name implies, this command allows you to cherry-pick a commits from elsewhere and apply it to your current working git branch.</p><p id="4d37">Let’s say you have a pull request, and a colleague will make a pull request against your pull request. Your options are to merge your colleague’s pull request to yours or avoid the merge commit and pull down your colleague’s changes and cherry-pick the necessary commits form your colleague’s pull request and apply it to yours.</p><figure id="97a5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FTPeJvPtq9oAVXuKOqCWvw.png"><figcaption>Git cherry-pick a commit to master branch</figcaption></figure><h2 id="8b6e">The “git reflog” command</h2><p id="582a">Learn how to use <code>git reflog</code> and you shall always be able to get out of a local git mess. Reference logs are where everything you do with your local branch is recorded. Here is how I have used <code>git reflog</code> in the past.</p><figure id="9864"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*O3umuwQy-69bvAEutPwBKw.png"><figcaption>git reflog showing everything we did in previous steps</figcaption></figure><p id="5da4">Let’s say our branch’s state is in a total mess, and you desperately want to get back to an earlier state where everything was working. Checkout out the git reflog and find out the entry where you think you want to go back to and then use the <code>git checkout HEAD@{12}</code> or <code>git reset HEAD@{3}</code> to get back to that state in time.</p><p id="0d2d">I highly recommend learning more about <code>git reflog</code> , and it would massively boost your confidence when playing around with git. It’s worth mentioning that <code>git reflog</code> is for the local repository, so careful what you <code>push</code> upstream.</p><h2 id="785c">The “git reset” command</h2><p id="1960">Use the <code>git reset</code> command to reset the current HEAD of local branch to a specific state.</p><p id="5bdb">Let’s say I would like to reset the last commit</p><div id="547b"><pre>git <span class="hljs-keyword">reset</span> <span class="hljs-comment">--soft HEAD~1</span></pre></div><p id="e992">When using <code>--soft</code> the changes in the last commit are staged and when using <code>--hard</code> the changes are deleted. See <code>git reset --help</code> for more detail.</p><h2 id="ea1c">The “git grep” command</h2><p id="44b9">Use this command to search the tracked files for a pattern.</p><figure id="62c7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*CmIuwo-_WuwvvWz2-yTPJw.png"><figcaption>Git grep command for pattern</figcaption></figure><h1 id="fe8e">Conclusion</h1><p id="6c96">Thank you for reading. If your most commonly used git commands are not listed here, then please feel free to share them with the rest of us in the comments section.</p><p id="74fc">If you liked reading this article, feel free to check out my other articles, including the following three that you might find helpful.</p><div id="e47a" class="link-block"> <a href="https://readmedium.com/how-to-contribute-to-open-source-6ece27476671"> <div> <div> <h2>How to contribute to open source</h2> <div><h3>A guide on how to get started with open source contribution</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*IE3gQsMNW0yhaEDBmgA3hw.png)"></div> </div> </div> </a> </div><div id="352e" class="link-block"> <a href="https://codeburst.io/key-habits-and-things-i-wish-i-knew-earlier-as-a-developer-43c9466a0407"> <div> <div> <h2>Key habits and things I wish I knew earlier as a developer</h2> <div><h3>A listing of key habits and skills that could help you become a better developer</h3></div> <div><p>codeburst.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*YEEq1mZmYPN8QzNy3Aclfg.jpeg)"></div> </div> </div> </a> </div><div id="ffee" class="link-block"> <a href="https://readmedium.com/how-to-load-test-a-developers-guide-to-performance-testing-5264faaf4e33"> <div> <div> <h2>How to Load Test: A developer’s guide to performance testing</h2> <div><h3>How to design and run load tests using Apache JMeter & Taurus</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*1QRewxOETUC04exbcdmXIA.png)"></div> </div> </div> </a> </div><p id="02b3">For more articles, add me on Linkedin and follow on Medium.</p></article></body>

Git tutorial — A Beginner’s Guide to the most Frequently used Git Commands

A shortlist of 20 Git commands for all your code versioning needs

Source: rakhim.org

Git is hard, and there’s an overwhelming amount of command and tricks that you could learn and use but, when it comes to day to day development, there is a small subset of Git commands that you are most likely going to use over and over. Here is a list of git commands that I use daily

  • The git status
  • The git clone
  • The git branch
  • The git checkout
  • The git pull
  • The git push
  • The git diff
  • The git log
  • The git reflog aka the life-saver
  • The git rebase
  • The git merge
  • The git remote
  • The git stash
  • The git reset
  • The git fetch
  • The git cherry-pick
  • The git commit
  • The git add
  • The git grep
  • The git init

Remember you can learn more about these commands by running git command --help i.e., git checkout --help

Source: https://www.stickycomics.com/where-did-you-meet

The “git init” command

Use this command to create an empty Git repository. Given a directory, when the git init is run, a .git directory with subdirectories is generated.

git-article is now initialized with an empty git repository

I use the git init less often, only when I am starting a new project.

The “git remote” command

Use this command to point your local git repository to track a remote git repository. I created a repository in GitHub, and as shown below, you can set the remote tracking URL.

Now push and fetch/pull will track the remote repository

You can use git remote -rm upstream to delete the above or git remote set-url upstream-url to change the remote upstream's URL.

When you fork a repository, you can add another remote, i.e., origin to track your forked remote repository. Below is a screenshot of my fork of the spring-security repository, and after the fork, I run the git remote add origin [email protected]:rhamedy/spring-security.git command.

git origin corresponds to this https://github.com/rhamedy/spring-security forked repository

The “git clone” command

How to clone a repository using git clone

Clones a repository into a newly-created directory, creates remote-tracking branches for each branch in the cloned repository.

In the attached screenshot, gitclone the Dropwizard repository and then listed its remote branches.

The “git pull” command

Now let’s pull the changes from the remote repository. The remote repository has README.md and a LICENSE file.

Pulling changes from remote repository

Using the git pull upstream master we indicate to git, to pull down changes from upstream tracked remote repository and it’s master branch.

Instead of upstream you can use a different tracked remote repository. Let’s say; I would like to pull down my forked version of the spring-security master branch using git pull origin master

The “git checkout” command

This command helps you switch between different git branches. It also allows you to check out out a git commit SHA or a specific version of a file.

Git to create a new branch and switch to a branch

Initially, there is themaster branch and we used git checkout with -b to create a new branch and call it develop.

Now you can use git checkout {branch_name} to switch between the two branches.

Git checkout different version of a

Let’s say you changed a file in your branch and would like to revert to the original version, use git checkout {file-path} or you can also do git checkout upstream/master {file-path} and that would revert your changes.

You can also use git checkout {commit-sha} to checkout a specific SHA.

The “git branch” command

List git branches

This command lists all your git branches. I use this almost always before switching to a branch, push, pull, and rebase changes. For more options run git branch --help command.

The “git status” command

The output of git status command

The git status command shows changes in your current git branch. The screenshot shows that a new file pom.xml is added but, not yet staged, and the .gitignore is staged. A change has to be staged first and then commit.

The “git diff” command

The git diff command showing new changes

This command is used to show your changes to versioned files. The git diff for README.md shows the lines that are added in green and lines removed in red, including the # of lines added and removed.

The “git add” command

Show unstaged changes

The git status shows that we have already staged the .gitignore file, however, the README.md and pom.xml file’s changes are not yet staged.

The changes are staged after git add . command

Let’s stage the README.md and pom.xml files and to do that we run git add README.md pom.xml or git add . to stage changes in the current directory.

The “git commit” command

This command commits your changes to the git branch. Let’s commit the changes shown in the above screenshot.

Commit the staged files

The first commit adds the .gitignore with a clear message and the second commit adds the rest of the changes and git status will show no more changes.

The “git log” command

The git log command

Use this command to see the commit history of the current git branch.

You can pass the --oneline flag to the git log to only show short commit SHA and the commit message.

You can also pass the --graph and --pretty flags to the git log command.

The commit messages are pure genius.

The “git push” command

Now that we have committed some changes to the local repository, as shown above, let’s push the changes to the upstream remote repository.

Push changes up using git push

Now the local branchrepository-setup is pushed to the upstream repository, and you should see it in the UI, i.e., Github.

The “git stash” command

Let’s say you have made some changes and would like to save them and revert the working branch to the state before your changes, HEAD commit.

I pushed the repository-setup branch upstream and then created a pull request against the developbranch in Github and merged it. Now my local develop branch does not have the changes in the upstream develop branch, and I also made some changes in the develop locally.

Cannot git pull upstream develop because of local changes to the files

The above screenshot shows the local changes as well as the output of the git pull upstream develop that says

Please commit your changes or stash them before you merge.

Stash local changes and pull the upstream

So let’s stage all the local changes using git add . and then stash (save) them away so we can pull upstream changes.

As shown in the screenshot, we have successfully pulled the upstream changes.

You might be wondering, what happened to my stashed changes? Use the git stash list command to see a list of all stashes as shown below

stash@{0}: WIP on develop: c44e545 Initial commit

To apply your most recent stashed changes into your current working branch, you can use the command git stash pop (removes the stashed item from the top of the stash list) or git stash apply (keeps the stash entry in the stash list).

For more on git stash run git stash --help or read the documentation https://git-scm.com/docs/git-stash

The “git rebase” command

As shown in the drawing below, a rebase is taking all your changes (commits) and putting it on top of the branch’s HEAD commit. Let’s say; you checked out develop a week ago to work on a feature-todo and after one week, you have ten commits, and you are ready to push your changes upstream. During this one week, your colleagues have been merging changes to the develop branch, and that means your changes are on top of an old commit.

A rebase takes your changes from the old commit and put it on top of the HEAD commit of the branch.

Source: https://www.maxwellantonucci.com/2017/09/24/the-git-rebase-introduction-i-wish-i'd-had

So I created two branches of develop

  • The branch (1) update-readme-file
  • The branch (2) feature-todo
Sublime Merge — shows the status of branches

Added some commits to the theupdate-readme-file, pushed the changes up and merged the pull request to the develop branch.

The feature-todo branch, as shown in the screenshot, does not have the recent develop changes (from update-readme-file). Let’s rebase feature-todo on top of the develop branch.

Here is the series of commands

git checkout develop 
git pull upstream develop 
git checkout feature-todo 
git rebase develop

Since the README.md file is changed in thedevelop and also in our branch feature-todo, rebase will flag that. We can do the following to resolve the conflicts

  • Edit the README.md file and make the necessary changes to resolve the conflict
  • Stage the README.md file

Now you can continue the rebase with git rebase --continue command and if you wish to abort the rebase then git rebase --abort. A successful rebase would put our commits (top 3) on top of the develop HEAD commit as shown below

git rebase result

You can also use git rebase to squash one or more commits together. Let’s say you have four and wish to squash them to one

Git rebase to squash commits

The above will allow you to mark each of the commits whether you want to keep them or squash (s) them. I often squash my commits before pushing my changes upstream.

The “git merge” command

One difference between git merge from git rebase is that git merge creates a merge commit, and majority developers don’t like the extra merge commit.

A git merge behind the scene

Let’s say you want to merge changes from develop to the canary branch and you can do that by

git checkout canary
git merge --ff-only develop

For more on how merge works see git merge --help or the documentation https://git-scm.com/docs/git-merge.

The “git fetch” command

I use the git fetch --all to pull down the remote branches and tags.

The “git cherry-pick” command

As the name implies, this command allows you to cherry-pick a commits from elsewhere and apply it to your current working git branch.

Let’s say you have a pull request, and a colleague will make a pull request against your pull request. Your options are to merge your colleague’s pull request to yours or avoid the merge commit and pull down your colleague’s changes and cherry-pick the necessary commits form your colleague’s pull request and apply it to yours.

Git cherry-pick a commit to master branch

The “git reflog” command

Learn how to use git reflog and you shall always be able to get out of a local git mess. Reference logs are where everything you do with your local branch is recorded. Here is how I have used git reflog in the past.

git reflog showing everything we did in previous steps

Let’s say our branch’s state is in a total mess, and you desperately want to get back to an earlier state where everything was working. Checkout out the git reflog and find out the entry where you think you want to go back to and then use the git checkout HEAD@{12} or git reset HEAD@{3} to get back to that state in time.

I highly recommend learning more about git reflog , and it would massively boost your confidence when playing around with git. It’s worth mentioning that git reflog is for the local repository, so careful what you push upstream.

The “git reset” command

Use the git reset command to reset the current HEAD of local branch to a specific state.

Let’s say I would like to reset the last commit

git reset --soft HEAD~1

When using --soft the changes in the last commit are staged and when using --hard the changes are deleted. See git reset --help for more detail.

The “git grep” command

Use this command to search the tracked files for a pattern.

Git grep command for pattern

Conclusion

Thank you for reading. If your most commonly used git commands are not listed here, then please feel free to share them with the rest of us in the comments section.

If you liked reading this article, feel free to check out my other articles, including the following three that you might find helpful.

For more articles, add me on Linkedin and follow on Medium.

Programming
Computer Science
Developer
Git
JavaScript
Recommended from ReadMedium