Month: March 2018

git push and fetch

Last Updated on January 30, 2021 by sandeeppote

Changes committed to local or remote repository needs to be synced.
To get the remote repository synced with local use fetch and push vise versa.

 git fetch origin branch_name

When a fetch is done it creates or brings in changes from remote to origin/branch_name in local but not merge with local branch. For this we need to merge the changes to the local branch

To see the origin branch-

 git branch -r

Use these strategies for fetch-
Fetch the changes often
Fetch before you start the work
Fetch before you push the changes so that any conflicts can be resolved.

To Merge the changes to the local branch or the working branch first checkout the branch where you want to merge and then use following-

git merge origin/branch_name

You may also use git pull
git pull = git fetch + git merge

To pull the changes again switch to the local branch you want to merge the changes to-

 git pull origin branch_name

Push changes to remote
You may use following-

 git push origin remote_branch_name

This command shall push changes from source local branch into origin target branch

You may also use this –

 git push origin source_local_branch:target_remote_branch

Note this is separated by :(colan)

Delete remote branch

To remove remote branch simply use –

git push origin :target_remote_branch

Note there is nothing before : here which means push nothing to target remote branch which shall delete that branch.

OR use following to delete remote branch-

 git push origin --delete remote_branch

Git clone and add remote

Last Updated on January 30, 2021 by sandeeppote

Git has working directory, Staging Index and local repository. All changes done to the project sits in local repository until it is been pushed to the remote  repository. There are different tools that facilitates to provide code management system using git. One of them is GitHub, you may also use VSTS.

To push or sync the changes to the remote repository, you may clone the changes from remote to your local empty repository or if you have local repository you may add changes to remote repository.

To add changes to remote- github in this case-

git remote add origin remote_repository_url

To remove the remote remote origin-

 git remote rm origin

To clone from the remote-

git clone remote_repository_url

This shall be cloned in the current directory. In case if you want to new directory specify a name-

git clone remote_repository_url directory_name

 

 

Git stash

Last Updated on January 30, 2021 by sandeeppote

git-stash is used to record the current state of the working directory and the index and want to clean the directory with the intent of applying or dropping the stashed changes to the working directory. Suppose there are changes made to a file in working directory but before committing those changes you are willing to pull the changes from master. Stash the changes made to working directory-

git-branch-9

Stash Changes

 git stash

You may also stash changes with comments-

git stash save "Content updated to stash"

git-branch-10

You can stash multiple times, while stashed git reference this with unique name i.e. stash@{0}, stash@{1} and so forth. You may also switch between the branches once stashed as the recorded copy is available between the branches.

If you want to see the stash list

 git stash list

git-branch-11

Revert Stashed Changes
To apply stashed changes made to the working directory, use either-

 git stash pop

This shall apply/merge changes to the working directory and remove it from stash list.

 git stash apply

This shall apply/merge changes to the working directory but still keep the copy of stash.

Removing stashed changes
To delete specific stash item use-

 git stash drop stash@{0}

To delete all stash items-

 git stash clear

Use Stash clear wisely as it is destructive.

Git branch

Last Updated on January 30, 2021 by sandeeppote

Branches are used to isolate working folder. The HEAD always point to the current branch and can be checked as follows-

 cat .git/HEAD

This shall give the reference where the HEAD point to.

Before creating a branch you may check the if its already created by-

git branch

This shall display the list of branch in current repository.
git-branch-2

To create a new branch-

 git branch branch_name

Use basic letters and punctuation for branch name. Although the branch is created it is not yet checkout to use. To switch the branch-

 git checkout branch_name

You may also use following to create and switch to the new branch-

 git checkout -b "branch_name"

git-branch-1

Switching between branches

If you want to switch between the branches that are already created-

git checkout branch_name

If there are staged changes but changes not committed you wont be able to switch between branch as git gives a warning that the changes done in current branch might be overwritten-
gitlog-oneline

Comparing branches
If you want compare the difference between the branches-

git diff master..branch_name

master and branch_name are branches been compared here.
.. is a range operator.

git-branch-3
“Changed file content” text in the fileafterrenaming.txt is the change between branches

If you want to see the differences with colored text-

 git diff --color-words master..branch_name

git-branch-4

Rename branch

You might want to rename branch to match to the changes it has or if you are resolving and defect to identify branch with the ticket number or name. To rename branch-

 git branch -m branch_you_wan_to_rename newname

Deleting branch
If you are up to deleting a branch switch to master branch as you are not able to delete the branch if its been checked out and hit below command-

 git branch -d branch_to_delete

If there are changes that are not merged between branches git wont allow to delete the branch and shall provide following warning- To see the difference use

git diff master..new-branch

git-branch-6

 git branch -d new-branch

git-branch-7

As it suggests if you want to really delete use -D to permanently delete the branch losing the changes-

git branch -D new-branch

git-branch-8

Creating branch from remote-

If you want to checkout and create a local branch from remote-

 git checkout -b local_branch_name remote_branch_name

Here all changes from remote_branch_name will be available in local machine with the branch name local_branch_name
 

Git log- basics

Last Updated on January 30, 2021 by sandeeppote

Git log shows commit logs. Helps to see the paginated view of the commits made to the repository. To see commit logs-

git log

This shall give you the list of commits having specific information.
Commit ID, Author, Date of Commit and Message

This is how we see what changes has been in the past done on the project.

gitlog

If you try to see log in a non commit repository i.e. if not a even single has been mage to repository it shall give fatal error-

fatal: your current branch ‘master’ does not have any commits yet

If you want to see the logs in one line-

 git log --online

gitlog-oneline

Here you can see the difference the log is displayed with short SHA value and comments.

If you wish to see limited number of logs or see recent number of commits use following-

 git log -n 5

This shows recent 5 commits made to repository. You may also use this-

 git log -5

If you want to see the time period since the commits were made-

git log --since="2018-01-01"

If you want to see the time period until certain date the commits were made-

git log --until="2018-01-01"

There is lot of flexibility for using time period-

git log --since="3 weeks ago" --until="4 days ago"

To search by author-

git log --author="authorname"

To do a global search use-

git log --grep="commit"

grep means global regular expressions and will search in commits for specified search value.

If you want to see the from a particular commit for a particular file-

git log SHA123.. file.txt

SHA123 is a commit ID and .. if after that for file.txt

git log --stat --summary

Will give the what has been changed and quantity of change.

By default the commits are shown in chronological order, if you want to change the order of the commits-

 git log --date-order

This shall show the commits in date and time descending order

There are more options on this, you may see here to know more.

To view whats in the commits use-

git show SHA-VLAUE

This shall show the changes that happened in the commit for mentioned commit ID with the differences in the file content.
git show can show the commits based on SHA, blob or tree.
 

 

Delete or un-initialize git repository

Last Updated on January 30, 2021 by sandeeppote

If you want to completely remove the or un-initialize git from project and keep the working copy intact, delete the .get folder from your project root or use following to remove from command-

rm -rf .git

After this if you might get this error if you are trying to command on the non-initialized folder

fatal: Not a git repository (or any of the parent directories): .git

Sitecore Discussion Club

Last Updated on January 30, 2021 by sandeeppote

Other day(12th March 2018) we had a great time discussing about the topics and trends in Sitecore with very exciting presentations.

I managed to present a topic on Media Plugin available in Sitecore Market Place (Allowed Media Plugin). Some glimpse me presenting and the presentation attached. Worth joining this club if interested. More information keeping looking @ sitecore.events Cheers

Media Plugin

 

Git – tree-ish navigating the commit tree

Last Updated on January 30, 2021 by sandeeppote

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

Last Updated on January 30, 2021 by sandeeppote

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

Last Updated on January 30, 2021 by sandeeppote

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 🙂