In this chapter we shall learn about below git commands:
* “git fetch” command is used to pull the latest changes from the remote repository to your local repository.
* It will not merge with your changes. Hence your working repository will remain safe from merge conflicts.
* You will use “git fetch” to know the snapshot what others are working on.
To fetch a remote repository use:
“git fetch <remote_repository_url>”
* “git pull” is used to pull a remote repository.
* If you have made changes in local repo and do a “git pull”, then the latest changes will be merged.
* So there might be changes of merge conflict.
* But if you are working on a repository and need to check other commit, then “git fetch” is a safe option.
* Ideally “git pull” = “git fetch” + “git merge”
To pull a remote repository use:
“git pull <remote_repository_url>”
* GIT is very much different from SVN. In SVN, you will have a single repository and you will be committing those changes in that single repository.
* But in GIT, every developer will be having their own local repository. Each developer will make changes in their repository and then push to the remote repository.
* The following operations are performed on remote repository “clone, fetch, push, pull”.
* “git remote” is used to add, view, delete connections to remote repository
Now let us see how to perform “git remote” and related operations in series of examples:
1. To add a remote repository use “git add <name> <repo_url>”
2. To know the remote repository, use “git remove -v”
3. To fetch remote repository, use “git fetch <repo_name>”. RepoName should be the name that you used to save the repo url.
4. To remove a remote repo, use “git remote rm <repo_name>”
* Sometimes, in a project, you will need to include the code of another project.
* Inclusion can be done in many different ways, for example, download the code and include the code directly into your project.
* But one definite downside is, if there are any update to that project’s code, your project will be missing that.
* In such case, you will need to use “git submodule” command. This command will create a link to the other project code, so it will get updated automatically whenever there is an updated to other project code.
* For example, you need to use openSSL security library in your project, so you will create a link to that project in your project with “git submodule” command.
In below example, I have created a submodule link to openSSL repository by using below command:
git submodule add https://github.com/openssl/openssl.git
“git push” command is used to push the local changes to remove repository.
git push <repo name> <branch name>
git push origin master