GIT is one of the most popular Vesion Control System. In comparison with SVN and CVS, Git keeps a copy of the document in local desktop too, along with a copy on the centralized server.
Repository, a term frequently used in version control systems is a data space on the Version Control System where all documents of a particular project is stored.
Distributed Version Control System will have a copy of the repository on the server and also in every client/user system. There will be constant push and pull operations that syncs both the repositories. Git is a Distributed Vesion Control System.
Git vs GitHub
- GitHub is the central repository and Git is the tool that creates local repositories. Git, a version control management tool assists in syncing the local repository with the GitHub. As a best practice, syncing the repositories should be controlled by the developer once he/she feels to share the code. GitHub is a Co-Hosted, centralized and service hosted online.
Git Operations and Commands
- Create a Central Repository on GitHub
- Access http://github.com and signup for a new account. Email verification will be required.
- Once you login to your GitHub account, select option to “Start a Project“.
- You will need to create a new repository for every project. Create a “Public” repository which is free, and remember to select the option to Initialize the repository by adding the README file.
- Now if you are using Windows OS, install “Git for Windows” from GitHub’s official website. Select all default options to make the installation easy.
- Installation of Git on CentOS 7 is present in one of my other post.
- Now create a folder in in your Windows desktop, and go to that folder.
- Right click anywhere in that folder and select “Git Bash here“. This will open a new command prompt kind of window. This is the Git Bash emulator. You do not need to do this if you are in Linux host’s shell.
- To create the local repository, type the command “git init“. This will create a “.git” folder in the present folder and create few other folders and files in them. This is a hidden folder visible from the Git Bash window by running the “ls -A” command.
- echo “# docker_test” >> README.md
- git add README.md
- git commit -m “first commit”
- git config –global user.email “<your email ID>“
- git config –global user.name “<Your name>“
- git commit -m “first commit”
- git remote add origin https://github.com/<account name>/<project name>.git
- git push -u origin master
- git pull origin master
- To download a copy of the central repository, run the command “git pull origin master“.
- Git has an intermediate layer that sits between the local workspace and the local repository. To commit a change made in local workspace to the local repository, and to upload to central repository, the file will have to be added in the index.
- The command “git status” shows the names of the files that are there in the index.
- To test upload, let us add a new file in the local workspace, for example named “test.txt“.
- In the Git Bash windows type the command “git status” to see the status of files in the index. The newly added file will be marked as “Untracked files“.
- Run the command “git add test.txt” to add the file in the index.
- To commit changes enter the command ” git commit -a -m “My first commit in local repo” “.
- The command “git add -A ” adds all files to the index, and command commit with “-a” switch commits any files that has been added to index at least once earlier, even if they are not added to index after modifying the file.
- The command “git fetch” is also a command similar to “git pull“
- To upload all changes in the Master branch, change location to “Master” and run the command “git push origin master“
Branching:
- By default you will be working with the “master” branch. Sometimes you may want to leave the master branch as is, and add files/changes to a new branch spinning out from the master branch.
- The command to create a new branch with name firstbranch is “git branch firstbranch “.
- The command “git checkout firstbranch” enables up to start working with a new branch, similar to changing to a new folder.
- Here you can add new files/updates and commit.
- After committing new changes in firstbranch branch, if you change back to the master branch and do an “ls” command you will not see the new file, and “git status” too will not mention about the new file.
Merging:
- Merging is a feature of Git to merge two branches.
- If you need to merge the files in a branch named “firstbranch” to the “master” branch, ensure to are working in the master branch presently, and then run the command “git merge firstbranch“.
Rebasing:
- The concept “Rebasing” is similar to Merging, integrating changes from one branch to another. A merge preserves the branch history, but the Rebase does not. Merge is usually used for combing branches that are already public, but the rebasing are done for codes not exposed to public yet.
- Merging creates a new commit object taking info from the latest commit from master and latest commit from the “firstbranch” based on the above example. When merging, the latest commit point in “firstbranch” still remains to continue further in future. In Rebase, the “firstbranch” cannot be used again. All commits from “firstbranch” is taken and placed on Master branch.
- To rebase all files from “firstbranch” to Master, go to Master branch by checking out to Master first and type “git rebase firstbranch“
Copying files to centralised repository in GitHub:
- This activity is done using the “push” command established via SSH.
- For this first generate an ssh key in the local desktop by running the command “ssh-keygen” and select default options.
- Make a note of the “.pub” file and location where the file is saved in local host.
- Copy the contents of this file to your GitHub account. For this, access your online account, go to Settings.
Select “SSH and GPG keys“, and press the button “New SSH Key“. - Give a “Title” and paste the contents of the SSH key to the text-field. Click on “Add SSH Key” button.
- Now go to the local host and Git Bash window.
- Establish and SSH connection using the command “ssh -T git@github.com“. This email address can be found by clicking the “Clone or Download” button in your repository page in GitHub online portal and selecting “Use SSH” link.
- Now if you go to your Online portal and settings page, you will see the key symbol close to your SSH key in green.
- To upload all changes in the Master branch, change location to “Master” and run the command “git push origin master“
Adding new branch repository in GitHub central repository:
- If you go to your Online repository, if you select “Branch” drop down list, you will see only Master branch as of now.
- To add a new branch named “firstbranch” in your Online portal too, the best practice is to go to your Git Bash window, change location to “firstbranch” and run the command “git push origin firstbranch“.
- Now if you check the online repository now, you will see the new repository listed and the files in it.
- To upload all changes in the Master branch, change location to “Master” and run the command “git push origin master“. Now if you check the online Master branch repository, you will see the the files in it.
Reverting to an older version:
- We will come across situations where we will have to revert back the changes to an older version of the file.
- Let us work by adding a new file, for example named sample.txt in the Master branch. This file should have content in the file with one line that says “Hello world 1“. Commit with “-m” switch with a description “commit 1”.
- Add another line in the file “Hello world 2” and commit with “-m” switch with a description “commit 2”.
- To revert back to initial stage, run the command “git log” and copy the hexadecimal code in the line that starts with commit with the block related to “commit 1”.
- Now run the command “git checkout <hex code> sample.txt “.
- Now if you check the content of the file, the content would had been rolled back.
Errors and Troubleshooting:
When running the command git pull master origin, I get an error Unable to find remote helper for ‘https’
- This may be the first time you are trying to download files from the repository
- Was curl-devel package installed while installing and configuring git?
When running the command git pull master origin, I see and error “fatal: refusing to merge unrelated histories” and does not download files from GitHub repository
- You would have added a file to the local repository / sandbox, added and committed a new file before first pulling files from the GitHub repository.
- You will have to run the command “git pull origin master –allow-unrelated-histories” to pull files and merge both repositories. Be sure of what your are doing!!
Good luck !!