Jul 15, 2019

Git basics - Git + Hub makes it easy

From being cool hipster kid, Git has transformed into an absolute necessary in your toolkit.

Mastering Git is essential and important thing in your career.

Here is a quick list of commands that you might need to use:

Note: By no means this is a complete Git 101. But just a few commands that will be useful.

Setup

Install Hub from GitHub.

For Mac brew install hub For Windows scoop | choco install hub For Fedora sudo dnf install hub For Debian sudo apt install hub

Create an alias for hub as git

Configuration

The most important thing with Git is to add your username and email configuration. You can set the user name and email configuration globally using the following commands.

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

Create a project

To create a project to both local and remote with out opening browser.

$ mkdir awesome-project
$ cd awesome-project
$ git init                          // Initializes git repo
$ git add .                        // Adds files and directories
$ git commit -am "Initial commit" // commits the changes
$ git create                     // Creates a remote repository
$ git push                      // push the changes

🎉🎉🎉

Clone a project

Interested in a project and want to contribute? You can clone and use the repository.

$ git clone <org>/<repository> // Clone the repo
$ cd <repository>             // Go inside

It is something that you want to contribute for a long time or customize something. Well you can fork it too.

$ git fork --remote-name=origin  // Fork the repo

Checkout the branch

Working on a feature branch. Check them out.

$ git checkout <branch-name>

Have some intermediate working files? Want to store it temporarily, stash it.

$ git stash
$ git checkout <branch-name>

Want to go back to stash.

$ git stash list
$ git stash apply

Checkout the PR

But sometimes you just need to checkout a PR to check. Maintainers (or) reviewers will :heart: it.

$ git pr list -L 20 --format='%t [%H] | %U%n' // List down the PRs
$ git pr checkout  <pr-number>               // Check out a particular PR

Well in mid of PR review and there are more changes don't worry.

$ git am -3 https://github.com/<org>/<repo>/pull/<pr-number>

Created a PR - Want to rebase it to a single commit

Most project maintainers, want their Git history clean. This simple task will make the project history easy to understand and maintain. Often you might have faced situations where the maintainers will ask you to rebase the commit to single commit.

$ git rebase -i HEAD~<number_of_commits>

Well, that is easy but it will be tricky when you want to rebase wit master or with some merge conflicts. You can address that by:

$ git rebase -i master
// (fix the conflicts)
$ git add .
$ git rebase --continue

Want to abort it via git rebase --abort.

Browse issues

Wanna look the issues to grab. Find it difficult to type the entire GitHub URL. You can do it with hub.

$ git browse --issues

Other commands that I use a lot

To get the status of my current repository

$ git status

To log the commit history

$ git log

To amend the commit log

$ git commit --amend
<amend the commit>

To sign off a commit:

git commit --amend --signoff

Up Next