How to make using Github tools easy?
Tags: github, startup, career
Published: 2020-06-16 10:50:38

I have just started as Software Engineer at a startup after completing my CS degree. I haven't come across Github being taught in the university courses. But it is a necessity in the corporate world.

Here are some commands which I think are mostly used in all companies, you need to get thorough with. 

It is seen that `ssh keys` are used, so that you wouldn't need to do authentication every time you have to push changes. Once you start using it, you'll realise it saves time. And if you are over a zoom call with your boss breathing down your neck for you to push changes, you wouldn't want to exasperate them.

So add `ssh key` to your Github account and start using it!
<br />
First step is:
```
git clone "repo-url"
```
This is pretty basic step. Though there are n-branches for that repository, only the `default` one gets cloned.

At work, you would be told to create your own branch to start working. 
Use:
```
git branch 
```
To check your current branch.
<br />
It is better to use github web to create origin branch. It's really easy to create and you understand from where should your branch originate. 

![Click here](https://dev-to-uploads.s3.amazonaws.com/i/o9qi8dhlig9hyc9wgmep.png)
<br />
Select branch from which you wish to create and then enter the name of the branch. The naming conventions might differ but if you are developing a new feature start with `feature/feature-name` or `development/`. If you are teaming up with someone `team/feature-name`. For bug fixes, don't forget to include `bug` or `fix` in the branch-name. For rest you can use your name `your-name/task-name`. 

Now, you create the same branch on your machine (local branch). It is better to name the branch the same as the origin. 
```
git checkout -b "branch-name"
```

You need to track the origin branch for any commits and code pushes. Setting upstream allows you to track the origin branch. You could use just `git pull` and `git push` without mentioning the remote.
```
git branch --set-upstream-to=origin/<branch-name> <branch-name>
```

It is always safe to rebase whenever you are taking a pull from the origin. A basic `git pull` might merge your commits on your local into one commit. A rebase will apply your commits on top of the working tree, so you know if you want those commits on the branch or not.
```
git pull --rebase
```
<br />
![Click here](https://dev-to-uploads.s3.amazonaws.com/i/ij3sak5hmr2pmqk8nney.png)

Github desktop is really handy. It eliminates the effort of staging the changes using `git add` before committing. You can see all your changes in various different files and choose what files to commit by checking the checkbox. 

You can see your current branch and switch branches. Take a rebase whenever you are switching branches. This will apply commits on top of the working tree. You can view this in the history tab of Github desktop. To remove unwanted commits at the top you can use,
```
git reset --hard "commit-hash"
```
to start anew!

OR

```
git reset --soft "commit-hash"
```
to retain the changes and commit again according to your will.
<br />

Alternative to reseting `^HEAD` will be creating a branch from a specific commit, if you think you have messed up after some commit and wish to redo. 
```
git checkout -b "branch-name" "commit-hash"
```

And suppose you want a specific commit from another branch, you can `cherry-pick` that commit into your branch. This commit will sit on top of your working tree.
```
git cherry-pick "commit-hash"
```
I would recommend you use this only when the other branch is going to remain un-merged. Otherwise, merge would create duplicate commits in the working tree. 
<br />

Use github web to merge branches. Create a pull request. When you do this, you can compare and see if the branch is `Able to merge`. 
![Click here](https://dev-to-uploads.s3.amazonaws.com/i/4o5s5dq5738323rr6hh9.png)
Go ahead and merge, choose a reviewer if needs code review.

And if you see this, `Can't automatically merge`
![Click here](https://dev-to-uploads.s3.amazonaws.com/i/gxxl117kvsgnmkv9jqyd.png)

You need to do it locally and resolve merge conflicts. In this case, if you wish to merge `branch B` into `branch A`. You need to checkout to `branch A` and use: 
```
git merge "branch B"
```
And now you when you come to Github desktop, it will prompt you to resolve conflicts. If you are resolving conflicts, sit with your team and do it. This way you avoid overwriting anybody else's precious code.
<br />

To remove all your local branches except the master branch:

```
git branch | grep -v "master" | xargs git branch -D
```
<br />
I think this was all pretty basic stuff, which you might have come across. But in corporate, you must be extra careful before making any changes. Your code is valuable and affects the company value in the market.

Keep hacking!