Git Local Repositories for the Impatient

Git Local Repositories for the Impatient

2017 05 02 head

Start using Git

You shall put your source code under a version management system. The actual industrial standard is git, a distributed version control system - DVCS -.

Install git on your development machine. In case you are using IntelliJ IDEA configure git in the IDE through the preferences' pane.

To put your product under git version management, go to the root of the product and perform the command

git init

To add a specific file or all your source files to git

git add [filename]
git add *

You can perform these operations to add a product to a local Git repository directly in IntelliJ IDEA as follow

  1. Open the product you want to store in a repository.
  2. On the main menu, choose VCS | Import into Version Control | Create Git Repository.
  3. In the dialog that opens, specify the directory where you want to create a new Git repository.
  4. Put the required files under Git version control. The files appear in the Local Changes tab of the Version Control tool window, under the Default change list.

Commit Changes

To commit your changes you simply

git commit -m "commit message, should be clear and legible"

You can perform these operation directly in IntelliJ IDEA (VCS functions) as follow . Switch to the Version Control tool window and switch to the Local Changes tab. . Expand the Un-versioned Files node, and select the files to be added. From the context menu, chooseAdd to VCS, or press ⌥⌘A. . Switch to the Project tool window and select the files to be added. From the context menu, choose Git | Add or press⌥⌘A.

Each time you commit your changes you gain the ability to reverse to exactly this state. Each time you made a modification and tested it, just commit it. The cost of a commit is marginal. When working with Git, TDD, and ATDD it is normal to commit every few minutes. By few minutes we mean 5 or 10 minutes.

Observe yourself. If you commit at the end of the day, you are using your DVCS as a backup medium. It is time to change your habits. Use Git as an history of all successful changes you implement, and simply rollback all unsuccessful ones, simply discarding them.

What is Your Project Status?

To find out what the status of your product is, simply

git status

The same information is available in IntelliJ IDEA under

  1. Open the required product
  2. On the main menu, choose VCS | Refresh File Status
  3. Switch to the Version Control window and open the Local Changes tab.

Always Work with Trunk

Ideally you shall always work against trunk also called main branch. Because you develop using TDD and ATTD approaches you know your source code is always working. This approach is deeply compatible with lean and agile values. It is also the one with the least waste of effort.

If your team decides to work with branches, make them short lived!. See How to Git Branches for the Impatient post.

Discarding Changes

You find you the changes you made locally were not a good decision. No problem, with

git reset --hard

You revert to the last save committed set of files. Resetting with the hard option recursively discards all of your currently uncommitted (unstaged or staged) changes.

You want to restore just one file to its previous committed state.

git checkout --[filename]

Configuration Tips

You shall avoid end of line character warnings by configuring git to handle them. The situation arises because Microsoft OS uses CRLF for end of lines instead of CR.

You can configure git to handle it by running on Windows.

git config --global core.autocrlf true

or on Linux and macOS

git config --global core.autocrlf input

When using an IDE consider the Editor Configuration approach for end of line, identation and tabs versus spaces.