The Talent500 Blog
5 essential Git tips for software engineers 1

5 essential Git tips for software engineers

Without a version control system, it’s virtually impossible to maintain source code and share it between teams without conflict. Version control is important to keep track of what changes were made to the code base, by who, and what was the reason for these changes. A software developer needs version control, irrespective of their language. Of all the available version control tools, Git is the most popular choice. It is why Git has over 85% source code management market share.

Chances are you are already familiar with Git if you are a developer. If you are a beginner, mastering Git will provide you with a crucial skill employers look for. Git is a free, open-source distributed version control system that is used to manage source code and introduce parallel changes while keeping track of every change made.

Here we list some helpful Git tips that are optimal for beginners to master.

1. Git aliases

You will use Git multiple times during a coding session. Type commands every time can be too time-consuming. Fortunately, Git allows you to create aliases that improve your daily workflow.

While you can create an alias for any possible Git command, here are the aliases for the three most-used Git commands – checkout, branch, and commit.

git config –global alias.co checkout

git config –global alias.ci commit

git config –global alias.br branch

Next time you have to use these commands in the terminal; instead of typing the full command, for instance, git checkout master, you only need to type git co master.

To add aliases in Git, you can include them in the ~/.gitconfig file directly like this:

[alias]

   co = checkout

   ci = commit

   br = branch

2. Easy way to compare commits 

You don’t have to download different versions of the same file to make a comparison. Git allows developers to compare the difference between two commits or versions using the git diff command.

As a beginner, you must learn how to compare files between commits. It will save you much time as you can find any changes quickly.

To compare the same file between different commits, you use the following command:

$ git diff $start_commit..$end_commit — path/to/file

And to compare all the changes between two commits, you use:

$ git diff $start_commit..$end_commit

When you use these Git commands, the diff view will open inside the terminal. For a more visual picture, you can use git difftool. A much more comprehensive way to visually compare diffs is Meld.

You can configure Meld with this command:

$ git config –global diff.tool git-meld

To view diffs in Meld use:

$ git difftool $start_commit..$end_commit — path/to/file

or

$ git difftool $start_commit..$end_commit

3. Way to easily pull changes from the <main_branch> to a local branch

To pull or download all the newest changes made to the main branch of the code base on the local branch, you can use the git merge or rebase command to update the local brand. Both perform the same function, but beginners are advised to use rebase carefully or stick with the merge command. It is because Git rebase pulls the changes at a much higher hierarchical level than git merge. Here’s a great resource to learn the difference between merging and rebasing.

How to use Git rebase:

git stash -u //stash all your working files

git checkout <main_branch>

git pull

git checkout <your_branch>

git rebase <main_branch>

git stash pop //re-apply all your changes

The same function using the merge command: 

git stash -u //stash all your working files

git checkout <main_branch>

git pull

git checkout <your_branch>

git merge <main_branch>

git stash pop //re-apply all your changes

4. Stashing uncommitted changes

Sometimes you might have to work on an emergency fix in the middle of whatever task you handle. It puts you in a fix, as the situation demands immediate action, but you don’t want to lose your current changes either.

What do you do?

You use the git stash command to remove the changes you are working on temporarily. It is one of the essential Git tips for beginners. Learn to use the stash command, and you will be able to handle any change at any moment without losing your work.

With the $ git stash command, you hide the unfinished changes and have a clean working directory. It allows you to switch to a new brand to fix bugs or make updates without committing to incomplete changes.

Once you have fixed the error; you can revisit the last changes you were working on using:

$ git stash pop

This command recovers all the changes. If you do not need the changes anymore, you can clear the stash stack with:

$ git stash drop

5. Pull frequently

It can take longer to implement the changes when working on a large project or a significant feature. Unexperienced developers wait for the feature to be fully implemented before pulling the changes to the local branch. It is a bad practice, and you should make it a habit to pull changes frequently. It will help you avoid any significant conflicts as you can solve them early on as they arise. When done right, it improves the workflow and makes the merging of branches easier.

To learn more about Git. here are 7 GitHub repositories for new developers.

Conclusion 

Git is an essential tool to master for any developer. These five programmer tips will help you make better use of Git to improve your productivity and collaboration.

Talent500 is the platform for developers to discover global job opportunities with the best companies. Sign up here to know more.

 

0
Girish

Girish

Girish is Talent500’s architect for systems software. His experience in backend development has helped him convert visions of many a product into reality. From his days at BITS-Pilani, he has always dreamt about beating AplhaZero at chess.

Add comment