GIT | Visualpath DevOps Material

GIT remote

The git remote command lets you create, view, and delete connections to other repositories. Remote connections are more like bookmarks rather than direct links into other repositories. Instead of providing real-time access to another repository, they serve as convenient names that can be used to reference a not-so-convenient URL.
Usage :

$ git remote
List the remote connections you must other repositories.
$ git remote -v
Same as the above command, but include the URL of each connection.
$ git remote add "name""url"
Create a new connection to a remote repository. After adding a remote, you’ll be able to use name as a convenient shortcut for url in other Git commands.
$ git remote rm "name"
Remove the connection to the remote repository called name.
$ git remote rename "old-name""new-name"
Rename a remote connection from old-name to new-name

Git fetch

The git fetch command imports commits from a remote repository into your local repo. The resulting commits are stored as remote branches instead of the normal local branches that we’ve been working with. This gives you a chance to review changes before integrating them into your copy of the project. Usage:

$ git fetch remoteName
Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository.
$ git fetch remoteName branch
Same as the above command, but only fetch the specified branch.

Note: If you are performing git fetch then you have to do git merge explicitly to merge the remote branch changes to local branch shown as bellow:

Git pull

Merging upstream changes into your local repository is a common task in Git-based collaboration workflows. We already know how to do this with git fetch followed by git merge, but git pull rolls this into a single command.
Usage:


$ git pull remote
Fetch the specified remote’s copy of the current branch and immediately merge it into the local copy. This is the same as git fetch remote followed by git merge origin/current-branch.
$ git pull --rebase remote
Same as the above command, but instead of using git merge to integrate the remote branch with the local one, use git rebase.

Git push

Pushing is how you transfer commits from your local repository to a remote repo. It's the counterpart to git fetch, but whereas fetching imports commits to local branches, pushing exports commits to remote branches. This has the potential to overwrite changes, so you need to be careful how you use it. These issues are discussed below. 
Usage:
$ git push remote branch
Push the specified branch to remote, along with all of the necessary commits and internal objects. This creates a local branch in the destination repository. To prevent you from overwriting commits, Git won’t let you push when it results in a non-fast-forward merge in the destination repository.

$ git push remote --force 
Same as the above command, but force the push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing.

$ git push remote --all
Push all of your local branches to the specified remote.

$ git push remote --tags
Tags are not automatically pushed when you push a branch or use the --all option. The --tags flag sends all your local tags to the remote repository.  


Git Branching and Merging

A branch, at its core, is a unique series of code changes with a unique name. Each repository can have one or more branches.
By default, the first branch is called "master". 
Viewing branches
Prior to creating new branches, we want to see all the branches that exist. We can view all existing branches by typing the following: 
$ git branch -a  


Adding the "-a" to the end of our command tells GIT that we want to see all branches that exist, including ones that we do not have in our local workspace.

$ git branch -r  

Adding the "-r" to the end of our command tells GIT that we want to see all remote branches that exist
The asterisk next to "master" in the first line of the output indicates that we are currently on that branch. The second line simply indicates that on our remote, named origin, there is a single branch, also called master.
Now that we know how to view branches, it time create our first one.

Creating branches
We are going to treat the default "master" branch as our production and therefore need to create a single branch for development, or pre-production.
To create a new branch, named development, type the following: 
$ git branch branchName
Switched to a branch
Switched to a new branch 'development'
$ git checkout branchName  
Creating branch and switch to that branch same time
Assuming we do not yet have a branch named "pre-prod", the output would be as follows: Switched to a new branch 'pre-prod' 
$git checkout develop 
Making changes to our develop branch
On this branch, we are going to modify a new blank file, named "file1.txt". Until we merge it to the master branch (in the following step), it will not exist there.
This file now modified on the develop branch; as we're about to find out, it doesn't exist on the master branch.
First, we are going to confirm that we are currently on the develop branch. We can do this by typing the following:
$ git branch 

Merging code between branches
Suppose you’ve decided that work is complete in "development" branch and ready to be merged into your master branch. In order to do that, you’ll merge your "development" branch code into master and all the logs and data will be index in master. All you have to do is check out the branch you wish to merge into and then run the git merge command: 

Type Of Merge

Once you’ve finished developing a feature in an isolated branch, it's important to be able to get it back into the main code base. Depending on the structure of your repository, Git has several distinct algorithms to accomplish this:
a fast-forward merge or a 3-way merge

fast-forward merge 
A fast-forward merge can occur when there is a linear path from the current branch tip to the target branch. Instead of “actually” merging the branches, all Git has to do to integrate the histories is move (i.e., “fast forward”) the current branch tip up to the target branch tip. This effectively combines the histories, since all of the commits reachable from the target branch are now available through the current one. For example, a fast forward merge of some-feature into master would look something like the 

However, a fast-forward merge is not possible if the branches have diverged. When there is not a linear path to the target branch, Git has no choice but to combine them via a 3-way merge. 
3-way merges use a dedicated commit to tie together the two histories. The nomenclature comes from the fact that Git uses three commits to generate the merge commit: the two branch tips and their common ancestor. 
Content In development branch:
Content In master branch:
Note: If both the branches have its own changes that time it follow 3-way handshake and generate a merge commit as bellow:
Pushing git Branches
Delete Local Branch

To delete the local branch use one of the following:
$ git branch -d branch_name 
$ git branch -D branch_name  

Note:The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch.
You could also use -D, which is an alias for --delete --force, which deletes the branch "irrespective of its merged status." 

Delete Remote Branch
you can delete a remote branch using:
$ git push remote_name --delete branch_name  

Comments