DEV Community

Sendil Kumar
Sendil Kumar

Posted on • Updated on • Originally published at sendilkumarn.com

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰πŸŽ‰πŸŽ‰

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Checkout the branch

Working on a feature branch. Check them out.

$ git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

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

$ git stash
$ git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Want to go back to stash.

$ git stash list
$ git stash apply 
Enter fullscreen mode Exit fullscreen mode

Checkout the PR

But sometimes you just need to checkout a PR to check. Maintainers (or) reviewers will ❀️ 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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Other commands that I use a lot

To get the status of my current repository

$ git status
Enter fullscreen mode Exit fullscreen mode

To log the commit history

$ git log
Enter fullscreen mode Exit fullscreen mode

To amend the commit log

$ git commit --amend 
<amend the commit>
Enter fullscreen mode Exit fullscreen mode

To sign off a commit:

git commit --amend --signoff
Enter fullscreen mode Exit fullscreen mode

Top comments (0)