20

So you've just got Git basics? Here are some fun commands to know now

 4 years ago
source link: https://codersbible.com/5-git-commands-to-know-just-after-you-get-the-basics/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

So, after reading tons of articles, watching YouTube tutorials and browsing docs you’ve finally gotten it! You can initialize Git repository, check its current status, add and commit changes and, in the end, push all of that to GitHub or Bitbucket. Who knows, maybe you even created and merged few branches. But there’s a lot more you can do with this great version control system. In this post I’m giving you 5 Git commands to master right after grasping the essentials.

The Git repository we’ll be working with has 5 commits. The first one is “Initial commit” and the latest one (indicated by HEAD): “Refactored code and added tests”. Note, that each commit has unique id (which is abbreviated version of its hash) e. g. “78e1949” or “93b1e43”. We’ll use it to reference desired changes later in the article.

2QbeuyB.png!web

git reset --hard

Ok...we messed up. Our commit “Refactored code and added tests” has a bug. Well, not only bug, but it interferes with the rest of the project. Well, not only interferes, but it’s absolutely not needed; we understood Dave - our Project Manager - very poorly. The only sensible thing to do is to remove these changes. The command “git reset --hard” followed by unique commit identifier does just that! We want to go one commit back - straight to “Implemented a killer feature” with unique id “d5f44da”, hence we must input “git reset --hard d5f44da”. Look below at the result. All unnecessary changes are gone! The latest commit (HEAD) is now different.

bERvQzU.png!web

git reflog

Uhm...you know what? I’ve spoken with Dave. He said we need these changes back. Sorry. Does that mean that you have to code everything again? Nope! Type “git reflog” and see what happens. Git recorded each step we took. You can see commits, resets, rebases etc. - all commands, which modified our repository with handy identifier on their left.

AF32UzU.png!web

This is the commit we removed in the previous paragraph:

Jvy2IjR.png!web

Let’s use “78e1949” to reinstate it. How? You already know how! “git reset --hard 78e1949”. And we’re back:

JvaIJna.png!web

git tag

You can imagine that reflog, despite being very helpful, can get really really long. Browsing through all the changes we’ve made over the course of, say, last 2 weeks can be daunting. That’s why we can create our own identifier for commits, which we’re afraid to lose. Type “git tag please_dont_go”.

a2aIZrJ.png!web

Now, instead of reinstating changes from ““Refactored code and added tests” by using “78e1949” we can utilize human readable “please_dont_go”. See it for yourself by typing “git reset --hard please_dont_go” whenever you need this commit back, just like we did in the previous paragraph.

git cherry-pick

If you’re familiar with the concept of branches, then you know that via merging them we combine work of few developers into one. But what if I don’t want to integrate entire branch of my colleague into mine? What if I only need that one precious commit, which sits somewhere in the middle of his work. It turns out, you can pick it (and only it) just like a cherry using “git cherry-pick”. Beware of conflicts though.

BVjIbav.jpg!web

git log --graph --decorate --pretty=oneline --abbrev-commit --all

And one last thing. You probably wonder how I was able to format all my commits so nicely for this post. After all, each time you type “git log” you get something that’s always too verbose and unreadable.

jiE7rav.png!web

As you suspect, by typing “git log --graph --decorate --pretty=oneline --abbrev-commit --all” you can clean that mess, but who would want to input that every time, right? Thankfully, Git allows you to specify aliases, which, in essence, are your own names for standard commands. Here are the steps:

6rMjAv6.png!web Add alias. FzUvEb7.png!web Use it. uyY3Iby.png!web Enjoy clean commits list.

Thanks for reading. Share with everyone in the comments what is your favorite Git command and how it saved you!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK