Wednesday, May 17, 2017

Git Local Repositories for the Impatient

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. If you are using IntelliJ IDEA configure git in the IDE through the preferences pane.
To put your project under git version management, go to the root of the project and do

 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 project to a local Git repository directly in IntelliJ IDEA as follow
  1. Open the project 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
    1. Switch to the Version Control tool window and switch to the Local Changes tab. 
    2. Expand the Un-versioned Files node, and select the files to be added. From the context menu, chooseAdd to VCS, or press ⌥⌘A.
    3. 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 Project Status?
    To find out what the status of your project is, simply


     git status

    The same information is available in IntelliJ IDEA under
    1. Open the required project 
    2. On the main menu, choose VCS | Refresh File Status
    3. Switch to the Version Control window and open the Local Changes tab.

    Work Always with Trunk

    Ideally you shall always work against trunk. Because you develop using TDD and ATTD 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 Work with Branches" 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  
    

    No comments:

    Post a Comment