Visual Git actions from IntelliJ
Tags: git, tutorial, beginners, codenewbie
Published: 2019-03-12 09:57:05

---
title: Visual Git actions from IntelliJ
published: true
description: A short developer story with Git
tags: git, tutorial, beginners, codenewbie
---

<img src="https://thepracticaldev.s3.amazonaws.com/i/m9ii68rqbznn932itn75.png"/>

Hi, my experience with version control includes CVS, SVN and more recently with Git. What I used always depended on the company and their preferences. Below I am outlining the developer stories I have encountered most and used so far with Git using IntelliJ and sometimes a terminal. 
This is not a tutorial on how to use Git or a good/bad route but a personal guideline which I would like to improve in time.
Until then, these are my developer stories with Git:

<b>1. Create a new local branch</b>

Often I need to create a new local feature branch to implement a specific task that would later be pushed remote. Below are screenshots of what steps I made to accomplish this.

From IntelliJ:

<img src="https://thepracticaldev.s3.amazonaws.com/i/7zz9wd9biag1k4v6u7rw.png"/>
<img src="https://thepracticaldev.s3.amazonaws.com/i/htl90tsiv106g6e2hp6p.png"/>
<img src="https://thepracticaldev.s3.amazonaws.com/i/d92wat29jb850xx1cl93.png"/>
<img src="https://thepracticaldev.s3.amazonaws.com/i/eljggu06gkgtc955h67y.png"/>


From Terminal:
<code>
Initialized empty Git repository in D:/workspace-services/newproject/.git/
git checkout -b newFeatureBranch
Switched to a new branch 'newFeatureBranch'</code>

<b>2. Push local branch to remote</b>

There will come a time when you have to push the local branch to remote. First commit all changes, then add the remote (if you haven't already) to which you want to push the code.
 
From IntelliJ:
<img src="https://thepracticaldev.s3.amazonaws.com/i/nfxn1byciipwo4mr7b1p.png"/>
<img src="https://thepracticaldev.s3.amazonaws.com/i/ov4aov76tpmlasclc2x0.png"/>
<img src="https://thepracticaldev.s3.amazonaws.com/i/9873cqvk3hxnm4c38ti0.png"/>
<img src="https://thepracticaldev.s3.amazonaws.com/i/72nuhr5oj7wvx4svymah.png"/><img src="https://thepracticaldev.s3.amazonaws.com/i/r332u8ubuljgralue65m.png"/>

From Terminal:
<code>
git add *
git commit -m "First commit for demo."
git remote add origin https://{...}.git
git push -u origin master</code>

<b>3. Rebase</b>

I want to Rebase my new branch on top of master branch so that my colleagues can have my latest modifications. It could be a big project with a lot of teams working in parallel or/and part of the "code conduct and practices" inside the company. First, make sure you commit all your changes.
Some teams might like Merging others like Rebasing. I personally like to use Rebase more. I consider that a linear history is easier to follow. For more technical details checkout out this nice <a href="https://medium.com/r/?url=https%3A%2F%2Fwww.atlassian.com%2Fgit%2Ftutorials" target="_blank">Atlassian documentation</a>.

From IntelliJ:

Checkout your feature branch, then from right bottom corner pick what branch to rebase onto, in my case it would be master. I want to take all my commits from feature branch and place them on top of master branch.

<img src="https://thepracticaldev.s3.amazonaws.com/i/b923gs20d3gu8gedn4ic.png"/>

Push the changes, because you will be in branch "master" and have commits from the feature branch.

<img src="https://thepracticaldev.s3.amazonaws.com/i/h1qzhkdkqoxd6265awrl.png"/>

<b>4. Merge remote branch into feature branch</b>

Let's presume I worked on your feature branch and now I am behind some commits from a "master" branch and I want to have the latest modifications. I cannot do a rebase (policy) then merge is my friend.

Checkout the feature branch in case you haven't already. Choose what branch to merge like below and then commit the changes.

From IntelliJ:


<img src="https://thepracticaldev.s3.amazonaws.com/i/86ooi9gb3oxx7yi6exqm.png" alt="Choose what branch you want to merge into current one"/>
<img src="https://thepracticaldev.s3.amazonaws.com/i/ufizkwdns1jsch09vt5w.png" alt="Log after merge"/>
<img src="https://thepracticaldev.s3.amazonaws.com/i/ys3m0znsalkkux1rfljr.png" alt="Push the merge changes"/>

<b>5. "Reset current branch to here"</b>

Choose from Version Control a commit that you want to reset the project to. One scenario can be that maybe the last commits introduced a new bug and you cannot figure out what is wrong, then you can reset the branch to the point in history where you want. 
Another scenario can be that you fetched all the commits from remote and you don't want to Git->Pull (either you don't like this, or you have some local modifications that you don't want to stash or delete or the company is against this), you can reset the current branch there.

<img src="https://thepracticaldev.s3.amazonaws.com/i/f6x9yyn7npi02msssc2s.png"/>

Next, choose how to reset. I usually use mixed. Choosing Hard will not keep any  local changes and will reset to the exact commit.

<img src="https://thepracticaldev.s3.amazonaws.com/i/leg1ufib7bo49urcyjmt.png"/>

If you didn't choose Hard, then you will be left with the modified code to commit and after push to remote.

<img src="https://thepracticaldev.s3.amazonaws.com/i/msc73p1f3ybvbvnuq7hn.png"/>

<b>6. Update your forked branch in GitHub from the master branch</b>

Let's presume you want to participate in a cool Open Source project in Github. You fork the project first and after some time you might want to update your fork from main project.
This was a tricky one for me, but very happy I found <a href="https://help.github.com/en/articles/syncing-a-fork">this article</a> in the Github documentation, specific for terminal commands.

From IntelliJ:

a) Add Upstream URL in Git>Remotes…, meaning the URL from the repository you previously forked.

<img src="https://thepracticaldev.s3.amazonaws.com/i/1byonyeb4ybpy2pk1in9.png"/>

b) Fetch from upstream: VCS>Git>Fetch
c) Pull from upstream/master: VCS>Git>Pull

https://thepracticaldev.s3.amazonaws.com/i/06212se7s0jvnmmvg0ch.png"/>

d) Or rebase your fork: VCS>Git>VCS Operations Popup

<img src="https://thepracticaldev.s3.amazonaws.com/i/mnbqzzi5xbummc783yiv.png"/>

An important rule I hoped I've learned is to commit fast and often. Far too many times I lost my work because I didn't commit it even locally. Don't do like me :)

Git is a powerful tool and it can seem daunting at the beginning but in time it will become an amazing useful tool and easy to use. I am still learning and happy to do so.

Thanks!