Git setup and Tutorial
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It has a strong support for non-linear development (thousands of parallel branches).
Assuming that you have created an account on the https://github.com/ following are the list of commands that are needed to work with Git:
1. Add your identity to Git:
Install Git on your machine and first thing to do is, Configure your Git account:
$ git config --global user.name "X Y" $ git config --global user.email “XY@example.com”
This information is required during every Git commit.The ‘global’ flag indicates that the configuration changes have been saved to your system and they stay remembered during every Git commit (from all your projects).
You can check your configuration settings using:
$ git config --list
2. Setting up the SSH keys:
SSH keys are used to verify trusted computers, therefore you must generate your public-private keypair for your secured Git interactions
$ ssh-keygen -t rsa -C "XY@example.com"
This will create a new ssh key, using the provided email as a label.
When you are prompted to enter a filename, better press Enter key and let the defaults be applied.
After that, you will be asked for a ‘passphrase’ which will act as the password for all operations that you do to the Remote repos. Fill in a password and keep it remembered.
Next, you will be given a fingerprint of the Public Key that will be generated after the successful execution of the above steps.
You have to now copy this Public key fingerprint and, Add it to your Github account under the: Settings –> Add SSH key option.
3. Creating a new Git repo:
- If you are starting a new project you need to use:
"$ git init" from your current project directory.
This will create a ‘.git’ directory in your project folder and initialize the version control.
4. Cloning a Git repo:
- If you want to get a copy of an existing Git repository (i.e a project), you need to issue the following command:
$ git clone firstname.lastname@example.org:causecode/xyz.git
This way you will seek all the files present on the Server for the project ‘xyz’ and, be a part of its updation.
5. Git workflow:
Understand certain terminologies and workflow associated with Git from the following diagram:
6. Adding a file to Git index:
Git Index is basically the Staging (ready for commit) area. In order to track new files, you need to add it to the Index using:
$ git add <filepath> $ "git add -i" for adding files in Interactive mode.
You can also remove a file from the Git index and your work directory as well:
$ git rm <filpath>
7. Get a Status check of your files:
We can check the status of our changes done, using:
$ git status
This will tell you on which branch you currently are and, will list the modifications done to the Tracked files. It also shows which files are modified but, not yet Staged. You will have to do a
"$ git add <filepath>" for staging changed files.
8. Committing staged files:
Once you are ready with all the changes (i.e Staged files) you want to push to the remote repo, you simply have to fire a:
"$ git commit" command
This will open an Editor (Vim or Emacs, as set in your Git config) for you and show all the files that will be a part of the commit. Now, your job is to enter a descriptive commit message and save the commit.
Alternately, you can include a Message directly within your commit:
$ git commit -m "Fix for Role modification feature"
9. Pushing files for commit
To send your committed changes to the Remote Git repository, you need to ‘push’ them using:
$ git push origin master
origin: an alias for your ‘remote’ repo.
You can replace ‘master’ with the name of the branch you are doing changes to. Remember, when you create a new branch locally; you should push that branch to remote first
"$ git push origin <branch>" and then, push your commits to that branch.
10. Branching :
Branches are used to work on a special feature or modify an existing without hampering the current chunk of code. After the new feature is tested and the changes are sure to be reflected, the branch with your new code can be pushed to the remote repo. The branch can then be summed up (merged) with the existing code (definitely after it’s thoroughly tested and reviewed). You can create a new branch using:
$ git checkout -b <new_feature>
‘b’ is the flag for branch, new_feature: a name of your new branch.
To switch between existing branches, you use the same command without the ‘-b’ option.
When you clone a repository into your local system, you will only get the ‘master’ branch of that project. To get some other branch from the remote repo:
$ git checkout -b <branch> origin/<branch>
Here we are fetching a Remote branch and creating a local branch with the same name.
10. Viewing a commit history:
After you have created several commits, or if you have cloned a repository with an existing commit history, you can look back to see what has happened using the command:
$ git log
You can even see a single commit from the Git Log, using the Commit Id by issuing:
"$ git show commit_id" command to view the files that were committed.
Also, you can see the commits originated from a specific Author using:
$ git log --author=xyz
11. Updating your repo:
In many cases, you might be working on the same piece of data that your colleagues are working on.In that case, you might update your local repo with the latest commit; then work forward.
So, in your current work directory, on the desired (or active) branch, you will issue:
$ git pull
You can also choose to Pull changes from a particular branch only:
$ git pull <branch>
Git does a behind-the-scenes Merge in this case.
Ok, if you want to merge one branch into another i.e obtain a feature in your code, you need to do:
"$ git merge <branch>" from your current branch.
Be careful, there might be Conflicts (edits made on the same Lines of code) during this phase which you will need to smartly Resolve.
12. Replacing or Undoing changes:
$ git checkout -- <filename>
This will replace all your local changes with the version available in the Git Index for that file.
13. Miscellaneous commands :
You can locally check the difference between the committed and the updated/latest version of a file using:
$ git diff <filename>
You can seek the help of any command by typing:
"$ git <command> --help" will open the Git Manual page for that command.
Tagging: Tags a specific commit with a simple, human readable handle that never moves. Example :
$ git tag 1.0.0 1b2efd3ff
You can easily get the commit Id with the commands used step 10. Here, 1b2efd3ff is the Id of a commit. This command is useful in cases where you want to create tags for software releases.
You can also modify your recently done commits, using:
$ git –amend
This command has an immense power to perform changes. You can refer to
https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog for an in-depth knowledge about various uses of this command.