Page 20 of 22

Git – tree-ish navigating the commit tree

Git has a concept called as tree-ish it means something that is part of tree or reference to tree. Tree in git means nothing but the structure of commits. These commits contains the information like Sha-1, date, author, comments and changes.

We are able reference our commit by using full or shorten SHA-1 hash, ancestry or HEAD pointer.

for e.g.:- this will point to the tip of the current checkedout branch i.e. HEAD

 git ls-tree HEAD 

This returns list of files at that point. May also use branch name instea of HEAD.

git ls-tree master

Also can see particular dirctory within the branch

 git ls-tree master directoryname

Git ignore

When any changes made to a file in working folder it is been tracked by Git to add that to staging and to repository.

There are some files which we dont want to track for e.g. log files. This are constatntly changed and hence git shall always ask to track the changed file.

So this files dont need to be tracked and that can be done by a magic file .gitignore.

.gitignore file sit in a project folder so the files in folders and subfolders can be ignored for specific project. Although the files also can be set to ignore globally, but its best to have .gitignore file at project level.

.gitignore file can have a list of files that can be ignored. We may use regular expressions to ignore the file with specific extension. For e.g. *.log

So all files with log extension wont be tracked and git wont keep poping if there should be any change in file.

We may also negate expression to track files. for e.g. ignore all files with html extension but not index.html. For ths .gitignore file should have following-

*.html

!index.html

Note that .gitignore is also a file, so git shall track this file. This file has to be commited to repositoy so that team members shall have the same fileignoring rules to the project they are working.

How to track empty folders?

Note that git does not track empty folders. It can only track files. If wish to have a empty folder in your project for any valid reasons. for e.g. log folder, so everybody in project shall have log folder but not necessary that the log files should be tracked. In this case the trick to track the folder woud be to creats a temp file which then git shall track an hence the folder itself will be tracked. Normally it best to create a .gitkeep file. This file dobt need to have any content, its just that foder now can be tracked.

Ignoring tracked files

Suppose you have a situation where the file is commited to repository but now you dont want to track this anymore. For e.g. log file. Although every developer may need to have a log file hich was create and tracked in intial commit but then once the file is in repository any logs on each developers log dont needto be puahed. So in this case the fie that is tarcked and commited shouldnt be tracked anymore. So to ignore tracked file use following-

 git rm --cached file.txt

This will remove the file fom staging and won be tracked further

Hope this helps 🙂

Git reset

Git reset allows to specify where the HEAD pointer points to.

HEAD is nothing but with every commit a SHA is created and point to that commit. With Git reset we get flexibility to point to the commit, so any commits after that shall be overridden or ignored based on the option used.

Different ways reset can be performed-

–soft

– is the safest reset and least destructive. It just moves the HEAD pointer but does not change staging index or working directory. Staging and working directory has the later changes and tell the difference with or the changes done in repository.

 git reset --soft SHA1324

–mixed(default)

– this option moves the HEAD pointer to the specified commit and changes the staging index to match the repository but does not change the working directory. All the work in working directory has later changes.

 git reset --mixed SHA1324

–hard

– this option changes staging index and working directory to match the repository and the HEAD is moved to commit specified in command, so any later changes after the commit that is been reset are completely overwritten after the next commit, so use this cautiously.

 git reset --hard SHA1324

After moving the HEAD using -hard option you may still be able to point the HEAD to current commit, so no changes are lost. But if another commit is made after –hard reset then the later changes are lost. For e.g:-

Have following version in git-

V4, V3, V2 and V1 and HEAD points to V4 i.e. the latest commit. Now if you want changes from V2, use following command-

git reset –hard V2

Working directory and staging index will be overwritten, and not if you do git status, HEAD moved to V2. But still the V4 and V3 sits there and now if you want to revert it back to V4 use-

git reset –hard V4

All your later changes are back.

But instead you do another commit V4 and V3 are lost. For e.g.:- if you have again same version’s V4, V3, V2 and V1 and HEAD points to V4

git reset –hard V2

Head is moved to V2 and staging and working directory is changed, now make changes to any file,

git add file.txt

git commit -m “reset the head and changes to the file”

the version in repository shall be as follwos-

V5, V2 and V1

so in this case V4 and V3 are completely lost.

 

Hope this helps 🙂

 

 

Git- undo working directory and amend commits

Undo working directory

If you want to undo any changes to working directory, user following command-

git checkout -- file.txt

Use — to undo a file in a current branch.

You may checkout specific commit or old version, use following command.

To see the commit user-

git log

Copy the first 10 characters of SHA and use following command to checkout a specific commit-

git checkout sha121212 -- file.txt

Where sha121212 is a SHA of the specific commit.

Amend commits

If you want to amend the commit i.e. something in the repository, you may do that using amend command. Note only the last/recent commit i.e. the HEAD point to can be amended and in this case date and SHA will be changes along with message, use following command-

git commit --amend -m "message goes here"

You may check the commit changes in log-

 git log

If you want to revert the current commit use following command-

git revert SHA21121

SHA21121 is the SHA for that commit.

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.

Sitecore MediaFolder VS Media.FileFolder

Sitecore has 2 settings in the config named MediaFolder and Media.FileFolder, both has a different functionality.

MediaFolder – path used in this setting, Sitecore shall watch this folder for the files that need to be automatically uploaded to Sitecore media library. The path in this setting can be a Site path (e.g. /folder/) or a absolute path(e.g. C:\folder). The file path should be different from Media.FileFolder. By default it is set to /upload. So any files moved to this folder will be watched by Sitecore and uploaded media library.

Disable file watcher
There is a security aspect here, if you want to only enable the file upload using Sitecore client then disable the file upload watcher.

To ensure that only way to upload file is through Media Library disable the file watcher by removing the following line in web.config in

[code language=”xml”]

[/code]

Media.FileFolder – path used in the settings can be a Site path (e.g. /folder/) or a absolute path(e.g. C:\folder)

When media files are uploaded from Media Library as a file, the files are been uploaded into configured path in this settings. So this files are not saved in database instead reside in as a physical file on server or computer.

MediaAsFile