Saturday, June 25, 2016

Unleashing the Git - part 5 - git stashing

More often than not, there can be situations where you have done some changes and you need to temporarily hide those from the working tree since those changes are not completely ready yet. Stashing comes into play in such situations. May be when you have done some changes in a branch and you want to switch to another branch without committing your changes in the current branch. Stash is the easiest option for that.

Say you have changed some files. Issuing the git status would show what has changed.
$ git stash // Temporarily hide your changes. Now issue git status. You won't see the modifications. Equivalent to $ git stash save
$ git stash save --patch // Opens the text editor to choose what portion to stash
$ git stash apply // Apply the last stashed change and keep the stash in stack. But this is normally not done. Instead following is done
$ git stash pop // Apply the last stashed change and remove it from stack
$ git stash list // Shows available stashes.
Stashes have name in the format of stash@{#} where # is 0 for most recent one and 1 for the second recent and so on.
$ git stash drop <stash name>   // e.g: git stash drop stash@{0} to remove the first stash
$ git stash clear // Remove all the stashed changes
$ git stash branch <stash name>  // Create a branch from an existing stash
In the next part of this series we'll discuss git branching in detail.

Friday, June 24, 2016

Unleashing the Git - part 4 - Git Tagging

Tagging comes very handy in managing a repository. Tags and branches are similar concepts. But tags are read only. There are two types of tags. 1) Lightweight 2) Annotated
$ git tag Basic_Features    // Creates a Basic_Features tag.
$ git tag            // Shows available tags
Basic_Features
$ git show Basic_Features   // Which show the details of the commit on which Basic_Feature tag is added
$ git tag beta1 HEAD^ // Creates a tag from next to last commit
Earlier I showed how to check out code with commit id. We can also use the tags to checkout code of a particular stage using the following command.
$ git checkout Basic_Feature
All of the above are lightweight/unannotated tag. Creating an annotated tag is as simple as creating lightweight tag as below,
$ git tag -a Basic_Messaging_Annotated -m "Annotated tag at Messaging"
$ git show Basic_Messaging_Annotated   // this will provide the details of the tagger as well.
Deleting a tag is same for both tag types.
$ git tag -d Basic_Messaging_Annotated  // Deletes the annotated tag we created.
$ git push origin v1.0 // Push local tag v1.0 to remote origin.
$ git push --tags origin // Push all the local tags to remote origin
$ git fetch --tags origin // fetches all the remote origins tags to local repository. Note: If you have a local tag with same name as a remote tag, it'll be overwritten

Saturday, June 18, 2016

Unleashing the Git - part 3 - Working with remote repositories

More often we need to share the files that we modify and make it available for other users. This is typically the case in Software development projects, where several people work on a set of files and all needs to modify and do changes. The solution is to have the repository in a centralised place and each and every one work with there own copies before they commit it to the central place. There are many online git repository providers. Examples are GitHub, BitBucket etc.

Choose, whatever best suites for you and sign up for an account. Most online repositories provide free accounts. I've a created a one in bitbucket and have created a repository named online_workbench.
$ git remote add origin https://shazni@bitbucket.org/shazni/online_workbench.git  // Syntax is git remote add <name> <repository URL>
$ git remote rm <name> // To remove a remote from your local repository
$ git push -u origin master
Password for 'https://shazni@bitbucket.org':
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 88.01 KiB, done.
Total 4 (delta 0), reused 0 (delta 0)
To https://shazni@bitbucket.org/shazni/online_workbench.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

After you entered your password, you'll have your repository online and its local location is your local $HOME/example directory.

'git push -u origin master' is to make the git aware, that all the pull and push operations to default to the master branch. If in case '-u origin master' is not specified, each and every time you issue a 'pull' or 'push' command, you will have to specify the origin.

Ok.. Now if someone needs to get a local copy and work on this remote repository, what he/she needs to do? Only few steps are involved.

First we need to clone the repository into a directory. Navigate or create a new directory where you need the local clone of the repository and issue the following command.
$ git clone <remote repository location> <Path you want the clone to be>
If you omit the <Path you want the clone to be>, it will be in the current directory.

Depending on how the firewall on your computer or local area network (LAN) is configured, you might get an error trying to clone a remote repository over the network. Git uses SSH by default to transfer changes
over the network, but it also uses the Git protocol (signified by having git:// at the beginning of the URI) on port 9418. Check with your local network administrator to make sure communication on ports 22—the port SSH communicates on—and 9418 are open if you have trouble communicating with a remote repository.
$ git clone --depth 50 <remote repository location>  // Create a shallow clone with the last fifty commits

Then, if we do the modification to the files or added files, next we need to stage it for commits. Issue the following commands.
$ git add *            // Stages and add all your changes, This is similar to git add -A 
// If you specify git add -u will only previously added and updated files will be staged. -p parameter will present you with sections so that you are given option to include a change or not.
$ git commit –m 'My commit comment'     // Commit the changes to the local repository
$ git pull        // checks whether there are any unsinced updates in the remote location and if exist syncs local and the server
$ git push        // Add the changes made by you to the server