Our next task is to upload our Hybrid Framework project into a local repository using Git and then publish our Git repository in a web-based Git called GitHub.
The question that normally comes when working with Git and GitHub – what is the difference between them?
Git is a version control system that stores data or code more like a set of snapshots of a miniature filesystem. Every time we commit or save the state of our project in Git, it basically takes a picture of what all our files look like at that moment and stores a reference to that snapshot. When we commit changes, it stores locally and if we push the commit, it also stores them remotely (if remote Git repository is set up). When setting remotely, Git act as Distributed Version Control System.
GitHub is a web-based Git. Its mostly used for code and offers all of the Distributed Version Control and Source Code Management functionalities of Git along with some added features. We will discuss more on it in later posts.
In this post, we will do a source version control of our Hybrid Framework project using Git and then learn some branching and merging techniques.
Download and Install Git
-
- Our first step is to download and install Git on our machine. To download Git, click git-scm downloads link to go to downloads page directly. Download the required files based on Mac/Windows/Linux machine. Mac installation is pretty easy and we just need to follow the prompts. We will use Terminal program in Mac for writing Git commands. For Windows, make sure that default options are selected. Git Bash is used in Windows for writing the Git commands.
- To check if Git is successfully installed on our machine, open Terminal/Command prompt and type in below commands.
which git git --version
- Next step is to do some configurations. Git can be configured at System level, User level, and Project level. Use below commands to do configurations at different levels.
// System Level git config --system // User Level git config --global //Project Level git config
- Here in our case, we will be setting some user-level configurations like setting up the user name, email etc..
// Configuring name, email git config --global user.name "Your name" git config --global user.email "Your email" // Some additional configurations // Configuring core editor in Mac git config --global core.editor "vim -wl1" // Configuring core editor in Windows git config --global core.editor "notepad.exe -wl1" // Turning on the different color options in the editor git config --global color.ui true // To list out all configurations git config --list // To view the configurations in .gitconfig file present in user directory cat .gitconfig
- Windows already has auto completion feature but in Mac, we have to install the auto-completion feature. This is optional but it will help if we are working with Mac. There are three steps involved in adding this feature – first, download the file from GitHub, rename the filename by adding “.” and then add it to “.bash_profile” file by adding few lines of code. Once auto-completion is installed, we can type in Git and type any character like ‘h’ and hit tab to auto-complete the command. Here it will open up
Git help
.// Get the auto completion file from GitHub curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash // Rename the git-completion.bash file to .git-completion.bash file mv ~/git-completion.bash ~/.git-completion.bash // Edit the .bash_profile file with below code vim .bash_profile // Type "i" to insert, to save hit Escape, type in: “:wq + Enter” the vim editor // Type in below code if [ -f ~/.git-completion.bash ]; then source ~/.git-completion.bash fi
- Once Git is installed and configured, out next task is to initialize our project. For initializing we will use
Git init
command inside the project folder. Here we will be initializing our Hybrid Framework project.// First step is to go project folder cd documents/workspace/HybridFramework // Initialize Hybrid Framework project by using below command git init
After successful initialization we will see as below and also a separate .git folder will be created inside our project folder. This .git folder will have all of our tracking information of our project. The .git folder contains many files and folders like HEAD, Config, description, refs etc..
- Now that our project is initialized, our next task is to do a first commit.
- To commit, first, we need to add all changes done in the present working directory by using the command
git add .
. Note that ‘.’ in the command refer to the current working directory. This is followed by commandgit commit -m "first commit"
. Here-m
refers to the message that we want to share on the commit. - To view the details of all commits, use the command
git log
. - To view the current status of our project, use the command
git status
. This shows the current status of our working directory, staging index and repository. Note Git follows a three-tier architecture – Working Directory, Staging Index, and Repository. In below screenshot note that the files in red are in the working directory and not moved to the staging area. The same files are added to staging index (files in green color) after executing the commandgit add
. After commit, the files are moved to Repository. - There are few more commonly used commands in git.
// To view Head is pointing to which branch // If HEAD is pointing to last commit of master branch cat .git/HEAD --> Shows ref: refs/heads/master // To view the hash value of last commit in master branch // Below command will display the hash value of the last commit in master branch cat .git/refs/heads/master --> Shows a hash value like '72eecc8707d97c874dd06aca5ad5198563fefcff' // For Add/Edit files git add git commit -m "Added/Edited files" // For finding changes between files or compare two files // This command compares the files in repository that head is pointing at to those present in working directory git diff // Compare two files between staging index and those present in repository git diff --staged // Delete files from repository // This will remove the file from the repository as well as from working directory. git rm filenames git commit -m "Deleted files" // Renaming files // This will rename the file from the repository as well as from working directory. git mv oldnamefile newfilename git commit -m "Renaming files" // Adding and committing to repository in one step git commit -am "adding committing files in one step" // Undo changes in current working directory. Checkouts the file from repository to working directory // -- means stay on current branch git checkout -- filename // Undo changes from staging to working directory or to unstage git reset HEAD filename // Undo changes to last commit in repository. First add it staging and then amend to last commit git add filename git commit --amend -m "adding to last commit" // Checkout from a particular commit or retrieving old version // Checks out the file into staging git checkout shavalue -- filename // Reset to a particular version can be done by resetting the head // --soft Does not touch the staging index file or the working directory at all but resets the head in repository git reset --soft shavalue // --mixed Resets the staging index and repository but not the working directory git reset --mixed shavalue // --hard Resets the staging index, working directory and repository git reset --hard shavalue // To permanently remove untracked files from working directory git clean -f git clean -n // To permanently remove a folder from repository git rm -r --cached FolderName git commit -m "Removed folder from repository"
- Till now we have seen Git identifies any changes to our project. However, sometimes we don’t want Git to identify some files like the log files or compiled source codes to be included in our repository. For such cases, we will create a
.gitignore
file. This file will contain the list of files that need to be ignored. In Windows, we can directly create a text file and name it as.gitignore
and save as all files in the project directory. For Mac, we will use a text editor from Terminal to create a.gitignore
file inside our project folder. - To create a
.gitignore
file in Mac, follow below instructions:1. Open Terminal and navigate to Project folder. 2. Type in nano .gitignore 3. Nono editor opens up in the Terminal 4. Here we are creating .gitignore file specific to HybridFramework project. We will be ignoring 'Target', 'RESULT_LOG', 'Screenshots', 'test-output' folders as displayed in below screenshot. 5. Hit Cntrl + X, Y, Return to save the .gitignore file inside project folder.
- One point to remember that the rules in our
.gitignore
file only apply to untracked files. Since ‘target’ folder and ‘system.properties’ files are already committed in our repository, first, we have to unstage them and then create a commit in order for the.gitignore
file to ignore the directories from the staging index.// Remove files from staging index git rm -r --cached target git rm -r --cached src/main/resources/system.properties git commit -m "Removed target and system.properties file"
- Once
.gitignore
file saved and target’ folder and ‘system.properties’ files unstaged, we will see only.gitignore
file as an untracked file. Remember to store the.gitignore
file in repository.// To add .gitignore into repository git add .gitignore git commit -m "Adding .gitignore into repository"
In the next post, we will concentrate more on the branching and merging techniques using Git and we will view how its reflected in Eclipse. Till then Happy Learning!