DEV Community

Gabi
Gabi

Posted on • Edited on

Visual Git actions from IntelliJ

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:

1. Create a new local branch

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:




From Terminal:


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:

Enter fullscreen mode Exit fullscreen mode

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

3. Rebase

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 Atlassian documentation.

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.

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

4. Merge remote branch into feature branch

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:

Choose what branch you want to merge into current one
Log after merge
Push the merge changes

5. "Reset current branch to here"

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.

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

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

6. Update your forked branch in GitHub from the master branch

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 this article 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.

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

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!

Top comments (0)