Tag: Version control

Git workflow or three tree architecture

Git follows following three tree architecture.

Working – is the working directory where the file changes are made. For the first time you either push the changes to the repository and following changes you may pull the repository copy to your working directory

Staging – before the work is been committed to repository the changes are prepared for commit and sits in a staging index.

Repository – once the changes are staged these can be committed to repository.

gitarch

Git- add (stage), commit and unstage changes

Before this if you want to see Git workflow/ three tree architecture

Stage changed files
To add the changes to the staging index from working directory use following command-

 git add .

This will stage all changed files (.) in working directory
To add specific file to staging index-

 git add filename.txt

Stage and commit at once

Per above this is 2 stage process to commit changes to repository. If you have all files that are ONLY modified use following command to directly commit from working repository. This command can also be used for renamed file.

 git commit -am "provide a message here"

So no need to explicitly add to the staging index, instead the above command shall update repository.

Commit changes

Going back to committing changes, use following command-

 git commit -m "message goes here"

Please note the best practice to put the message on what is this commit about and the message to be in the present tense.

Unstage changes

 git reset HEAD fiel.txt

If you put something in staging directory and don’t want there, then use reset head.

GIT – diff, remove and rename

Before this if you want to see Git workflow/ three tree architecture

To check the difference between Repository and working directory using following command-

git diff

To get the difference in words insteadĀ  of whole statement highlighted with diff, use following command-

git diff --color-words

To checkĀ  staged changes difference, use following command-

git diff --staged

To delete file from working directory and from repo, use following command-

git rm file-to-delete.txt

The file will be staged with this command so you dont need to explicitly stage the deleted file.

Commit this file-

git commit -m "file deleted."

Please note while doing so the files will be permanently deleted and not found in thrash to recover. So use this command wisely if the file is not in the repo for you to pull back.

Rename file in repository and working folder, use following command-

 git mv filebeforerenaming.txt fileafterrenaming.txt

This shall rename file and stage the changes. So this is not required to manually staged.