Skip to content

Everyday work with Git

The Git version control system is a powerful and at times complicated tool. Under the hood there are many mechanisms and tactics most users will never learn about, because... they do not need to. That is why it is easy to learn Git to a "good enough" level and, in normal work, use literally four simple commands 95% of the time.

You may know commands like git init or git clone, which you will use a few times a year - after all, how many projects will you work on? And git revert, git reset and the like - if you do not stir the code wildly and do everything sensibly (OK, simply following proven "procedures"), you will use them... once or twice. A year. If at all.

What the work looks like

I assume you work in a "hybrid" way. You work on the code in an IDE, handle the repository in the CLI, and look at bigger changes in the right web application. You split the aspects of your work between tools. Personally, I find it convenient and safe.

Convenient, because we write code in our favourite IDE, and today's IDEs integrate smoothly with a Git repository. In the file list we see which files are modified or have conflicts. In the editor window we see which lines changed, can check who changed them and when (the IDE uses git blame) and handle conflicts. The IDE reads the metadata from the repository, formats it nicely and saves us from going to the terminal for every little thing. That lets us focus on the code instead of clicking through windows.

Convenient, because we read the contents of commits in the browser, doing code reviews with other developers or creating pull/merge requests. That switches our thinking from writing to reading and working with the team.

Safe, because we save changes to the repository and push them to the server with a minimalist, and therefore very clear, terminal (I know, that is debatable). No clicking, just a little dry information and quick decisions - add, commit, update, push. We are not writing code, we are saving it.

Use Git in the CLI

I mentioned there are only 4 of these commands, and they are not complicated. The "complicated" ones for reading the repo are handled brilliantly by IDEs, and let's not take that away from them. In the terminal, let's focus on saving, because that is what matters most. This is the stage where you can make "silly" mistakes, make a mess and then have to undo changes.

Undoing changes with revert or a hard reset is an incredibly useful tool in many situations, but locally it is not very elegant. And it takes ages, because now you have to analyse for a long time what to undo. It is easier to check what you are doing as you go. It is also better not to "click through" Git inside the IDE. I honestly recommend using Git in the terminal, not in an IDE already overloaded with information, where it is easy to misclick or pick a familiar-sounding option that works slightly differently.

Avoid a mess with 3 simple rules:

  1. Commit after every specific feature or fix,
  2. Before you run git commit, check twice what you are actually saving,
  3. Pull changes from the server often (git pull) and resolve conflicts as they come.

OK, let's finally get to the magnificent seven fantastic four.

Following these 3 rules makes working with Git much simpler. If in "one batch" you edit only the files of one feature, there will usually be few of them, so git status is easy to read. You do not touch files unrelated to the task, so you can commit with git commit -am "<message>", which adds all changed, already tracked files to the commit by itself - instead of wondering which files you edited now and which are just "half-done". Note: -a does not add new, untracked files; add those with git add first. Then run git pull to fetch the changes from the server. If you work on "your own" branch (from which you will soon create a merge request to, say, develop), a plain git pull makes no sense, so fetch and merge the changes from the target branch, e.g. develop on the remote called origin, with git pull origin develop. Now resolve any conflicts in the IDE, and in the terminal finish the merge as Git tells you (if there were conflicts). Then you can safely run git push or (for a new branch) git push -u origin <new_branch_name_on_the_server>. Now create the merge request in the browser; code review aside, it is ready to merge into the target branch "with one click". You save time for yourself and others. Job done.

A simple flow for saving changes to the server:

  1. git status - check the state, to be sure what you will commit,
  2. git commit -am "<message>" - save all changes in tracked files,
  3. git pull or git pull <remote> <branch_name> - fetch the latest changes and resolve conflicts
  4. git push - push the changes to the server

Just starting with Git? See my guide on how to get the hang of Git in a quarter of an hour or two. It's free 😉

Advertisement