Git

GIT – VCS – version control system
To track changes in files/folders
To collaborate in teams
Free and open source

GITHUB – website to upload your repositories online
 – provides backup
 – provides the visual interface to your repo
 – makes collaboration easier

GIT  != GITHUB

The staging area is also called as Index.

The .git folder is hidden in git initialize folder and contains the below folders and files.

Folders are – branches   hooks info objects refs

Files are – config  description HEAD

Check if git is already installed.

git –version

Add your GitHub email and username to git.

git config –global user.email “yourGitHub@email.com”
  git config –global user.name “yourGitHubusername”


Add file/folders to git – tracking

Goto the location of the folder/project
   – git init
  – git status
  – git add
  – git commit -m “…..”
  – git remote add origin “location of remote repo”
  – git push -u origin master
  – git log
  – git –help

Create branch
  git branch “branch name”

Checkout branch
  git checkout “branch name”

Merge new branch in the master branch
  git merge “branch name”

Delete branch
  git branch -d “branch name”    — delete from local
  git push origin —delete “branch name”   — delete from remote

How to trigger notification email from GitHub whenever there is any change/commit in the project?

Github – Repository – Settings – integration & services – add email

Test and validate by making some change in the project.

What are tags/releases?

Tag operation allows giving meaningful names to a specific version in the repository. We can tag our project code so that later we can access it easily.

In Git using the tag, we can create the specific point in history for project or repository or data. For example, if our project is stable and we want to release it then we can create the tag for it like V1.0  

in Git you don’t tag branches. You are tagging commits. It is correct to say that tag is just an annotated pointer to a commit.

If you want to tag a particular commit, then use the appropriate COMMIT ID instead of the HEAD pointer.

git tag

Suppose we committed or pushed changes 3 times to the repo and then we thought our code is stable then we can create the tag at that point. So that we can use or restore it.

After that maybe we can add new functionality and committed. And we think this the stable state after adding new functionality we can add the tag with the new version for that point.

Commit number de46fo3 and tag v1.0 points to the same commit this is how it works.

Note:- Checksum number is nothing but commit ID and it is 14 digits number.It is generated by the SHA-1 algorithm.

Checkout the branch where you want to create tags. Eg. git checkout master

Create the tag.  Eg git tag v1.0. // This is the lightweight tag

git tag -a v1.1 -m “Tag for first release v1.1” // Annotated tags

We can write annotated tags with -a we can specify the version and with -m we can specify the message. Annotated tags stored as a complete git object in the git repo.

Display the tag –

git tag  or git tag –list or git tag -l // display all tags

git show tag-name // display all info of that tag

Push tag to the remote repo.

git push origin tag-name // specific tag is pushed to the remote repo

git push –tags or git push origin –tags // pushed all tags

 

Deleted the tags

  git tag -d tag-name // delete tag from local

git tag –delete tag-name // delete tag from local

git push origin -d tag-name // delete the tag from remote

git push origin –delete tag-name // delete tag from remote

Why should I create TAGs

  1. To mark release points for code or data
  2. To create historic restore points.

When to create TAGs

  1. Our code is stable and wants to create a release point for it.
  2. If we want to create a historic point for code/data so that we can use that code/data in future time or restore the data.


We cannot checkout tags in git. We can create a branch from a tag and checkout the branch
git checkout -b “branch name” “tag name”
eg. git checkout -b ReleaseVer1 v1.0


Creating TAGS from past commits
git tag “tag name” “reference of commit”
eg. git tag  v1.2 5fcdb03

Git allows a team of people to work together, all using the same files. And it helps the team cope with the confusion that tends to happen when multiple people are editing the same files.

There are many ways it can be set up and configured, but at my job, here’s how we use it: when a new employee starts, he downloads all the files from Github, which is an online service we’re all connected to.

So he has his local version of the files, I have my local version, our boss has his local version, etc.

When I make a change to some files, I go through the following process in the Terminal. (There are GUI clients for Git, but I prefer working on the command line.)

> git pull

That pulls the latest changes down from github. If there are conflicts between those and my local ones, it tells me what they are, file-by-file, line-by-line, and I now have a chance to reconcile those differences.

After editing the files or creating new ones, I run this command:

> git add .

Which adds all of my local changes to git, so that git knows about them. The dot after add specifically means to add all the changes I’ve made, e.g. new files I’ve added to my local folder or changes I’ve made to existing files. If I want, I can add only specific files, e.g.

> git add myNewFile.js

I now write a comment about the adds I just made.

> git commit -m “Fixed a major bug which stopped reports from printing.”

Finally, I upload my changes to the server.

> git push

Now, when my colleagues do a …

> git pull

… they will get my changes, and they will be notified if any of them conflict with their local versions.

There are all kinds of cool, useful commands for rolling back changes to a particular time or state. But probably the most useful thing about Git is branching. Let’s say my team is working on code for an Asteroids game, and I get the idea for making spinning asteroids. This will involve making some major changes to the existing asteroids code, and I’m a little scared to do that. No worries, I can just make a branch.

First of all, I’ll check which branches exist:

              git branch

              output is master*

So there’s currently only one branch on my local machine, called master. The star by it means that’s the branch I’m currently working in. I’ll go ahead and create a new one:

> git branch spinningAsteroids

That creates a copy of all the files in master. I’ll now move into that branch.

            git checkout spinningAsteroids

            git branch

            Output is       master

                                    spinningAsteroids*

I now spend a couple of hours in spinningAsteroids, doing whatever coding I need to do, not worrying about messing things up because I’m in my own branch. Meanwhile, I get a tech support call. They’ve found a critical bug and I need to fix it asap. No worries…

> git checkout master

… fix bug …

  1.    git pull
  2.    git add .
  3.    git commit -m “Fixed critical bug with high scores.”
  4.    git push

Now I can resume my work with spinningAsteroids.

  1.  git checkout spinningAsteroids
  2.  git branch 
  3.                 masters pinningAsteroids*

… work, work, work …

Okay, I’m now happy with my spinning asteroids, and I want to merge that new code into the main code base, so…

  1. git checkout master
  2. git branch
  3.    master*     spinningAsteroids
  4. git merge spinningAsteroids

Now the code from my branch is merged into the main code-base. I can now upload it.

  1.   > git pull
  2.  > git add .
  3.  > git commit -m “added new cool feature! Spinning asteroids!!!”
  4.  > git push

There are many ways to use Git. This is just one of them.

Cloning repo to custom location:

  1. $ mkdir test ………. create folder name test for cloning git repo
  1. either go to the created directory using command  $ cd test   and then clone the repo

         $ git clone git_repo_url

  1. Once cloned goto the directory $ cd test

      Create the new branch, add the new file, commit changes and push it in Git Repo.

  1. Create the new branch and add the file to it

         $ git checkout -b new_branch_name

  1. $ git status or git branch ………. check whether you are in the newly created branch
  1. Add the new file or make changes to existing file.

          $ sudo vim sample.txt

  1. $ git status  …………. check for any changes.
  1. $ git add sample.txt  …….. stage changes so that they can be committed.
  1. $ git commit -m “message”  ……. once changes are staged commit them
  1. $ git push origin new_branch_name  ……..push the changes to git repo.
  1. $ git branch ……..check new branch added to git

Switch between different branches

  1. $ git fetch origin  ………. pull origin from git upstream
  1. $ git branch -r  ……… check branches
  1. $ git checkout master  ………. switch to master branch
  1. $ git checkout other_branch   ……. switch to branch other_branch

Git Other Commands

The most commonly used git commands are:

  add Add file contents to the index

  bisect Find by binary search the change that introduced a bug

  branch List, create or delete branches

  checkout Checkout a branch or paths to the working tree

  clone Clone a repository into a new directory

  commit Record changes to the repository

  diff Show changes between commits, commit and working tree, etc

  fetch Download objects and refs from another repository

  grep Print lines matching a pattern

  init Create an empty Git repository or reinitialize an existing one

  log Show commit logs

  merge Join two or more development histories together

  mv Move or rename a file, a directory, or a symlink

  pull Fetch from central repo and merge with the local branch

  push Update remote refs along with associated objects

  rebase Forward-port local commits to the updated upstream head

  reset Reset current HEAD to the specified state

  rm Remove files from the working tree and from the index

  show Show various types of objects

  status Show the working tree status

  tag  Create, list, delete or verify a tag object signed with GPG

   help   git  help, git help -a and git help -g

‘git help -a’ and ‘git help -g’ lists available subcommands and some concept guides.

Yum install git

git config –global user.name “ravi”      // –global it adds user globally

git config –global user.gmail “raviguravcse@gmail.com

which vim

git config –system core.editor “/usr/bin/vim”   // add vim as editor for git

Cat /etc/gitconfig

Git Interview Questions and Answers

What is meant by distributed VCS?

Git is Distributed Revision Control System which means, every developer checking out code from central repository/server will have their own cloned repository installed on their machine.

Everyone has a local copy of the entire work history. This means that it is not necessary to be online to change revisions or add changes to the work.

DVCS is faster because you don’t need to communicate with the remote server for each and every command. You do everything locally which gives you benefit to work faster.

If the main server goes down or it crashes in DVCS, you can still get the backup or entire history of the code from your local repository or server where the full revision of the code is already saved.

What is meant by centralized VCS?

There is a single “central” copy of your project somewhere (probably on a server), and programmers will “commit” their changes to this central copy.

Centralized VCSs keep the history of changes on a central server from which everyone requests the latest version of the work and pushes the latest changes to.

This means that everyone sharing the server also shares everyone’s work.

VCS is slower because you need to communicate with the remote server for each and every command.

If the main server goes down or it crashes in VCS you will loose all data.

What is GIT?

GIT is a distributed version control system and source code management (SCM) system which is used to handle small and large projects with speed and efficiency.

Git can track changes to a file and allows you to revert back to any particular change.

Its distributed architecture provides many advantages over other Version Control Systems (VCS) like SVN one major advantage is that Git does not depend on a central server to store all the versions of a project’s files. Instead, every developer “clones” a copy of a repository called as  “Local repository” and local repo contains all the project data.

There is a central cloud repository as well where developers can commit changes and share it with other teammates.

What is a repository in GIT?

A repository contains a directory named .git, where git keeps all of its metadata for the repository. The content of the .git directory is private to git.

Git configuration file?

The .git/config file in each repository is used to store the configuration for that repository.

What is the command you can use to write a commit message?

git commit -a

Example: We can commit the message for the added files(test1,test2) using “git commit -m “” ” command. But we have added two new files(test3,test4) and modified old file(test1). Now we want to add test3 file only later we will add test4. Using “git commit -a” command we can write commit message for new and also modified file.

What language is used in GIT?

GIT is fast, and ‘C’ language makes this possible by reducing the overhead of runtimes associated with higher languages.

What are the advantages/benefits of using GIT?

Distributed System: GIT is a Distributed Version Control System (DVCS). We can keep control over our private. We can work disconnected too.

Flexible Workflow: GIT enables you to make your own work process.

Fast: GIT is quick as compared to other VCS tools. The reason is it is written in the C language.

Data Integrity: Since GIT utilizes SHA1, information isn’t less demanding to degenerate.

Free: It is free for individual utilize. Such huge numbers of beginners utilize it for their underlying activities. It likewise works exceptionally well with substantial size task.

Collaboration: Because of git team can collaborate there work easily.

Data redundancy and replication:

High availability:

Only one.git directory per repository:

Superior disk utilization and network performance:

Any sort of projects can use GIT:

What are the disadvantages of GIT?

GIT has not very many weaknesses. These are the situations when GIT is hard to utilize. Some of these are:

Binary Files: If your project has non-text files that are updated frequently (images for websites or MS Office documents), then git becomes bloated and slow.

Steep Learning Curve: Freshers need some time to learn GIT. Because there are many commands with many options. So new learner needs to spend more time to understand the git things.

Slow remote speed: Sometimes the utilization of remote stores in ease back because of system dormancy. Still, GIT is superior to different VCS in speed.

Git doesn’t track renames.

Git is slower on windows.

GIT does not support ‘commits’ across multiple branches or tags.

What are Git Design objectives?

Distributed workflow (decentralized)

Easy merging (merge deemed more frequent than commit)

Integrity (protection against accidental/malicious corruptions)

Speed & scalability

What is the difference between GIT and SVN?

  1. Git is a distributed VCS  and SVN is a non-distributed VCS.
  2. The content in Git is stored as metadata SVN stores content as a file.
  3. Git is less preferred for handling extremely large files or frequently changing binary files while SVN can handle multiple projects stored in the same repository.
  4. GIT does not support ‘commits’ across multiple branches or tags.  Subversion allows the creation of folders at any location in the repository layout.
  5. Gits are unchangeable, while Subversion allows committers to treat a tag as a branch and to create multiple revisions under a tag root.
  6. Git does not have the global revision number feature like SVN has.SVN revision no is a snapshot of source code.
  7. In git we make changes locally, check in (commit) to our local copy of the repository and then push changes to another repo or central repo when we want to publish them want to share with others. In SVN for check-in or commit we need to contact to directly central repo.
  8. With git, we can push changes anywhere even in team-mates local git repo. So both teammate can work on code or feature without impacting central repo. With SVN all users share the same repo. If two users want to share their code then only the way is they can commit code to the central repo and then each doing a sync.
  9. Git creates .git folder only in the root directory, whereas .svn will be created for each folder, in case of SVN.
  10. Git is difficult to learn for some developers, because it has huge concepts, whereas SVN has less concepts and easy to learn.

Why GIT better than Subversion?

GIT is an open source version control system; it will allow you to run ‘versions’ of a project, which show the changes that were made to the code over time also it allows you keep the backtrack if necessary and undo those changes.  Multiple developers can checkout, and upload changes and each change can then be attributed to a specific developer.

What is the function of ‘GIT PUSH’ in GIT?

The git push is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo. … Remote branches are configured using the git remote command. Pushing has the potential to overwrite changes.

What does ‘git pull’ command in GIT do internally?

In GIT, git pull inside completes a git get first and after that completes a git blend. So pull is a blend of two orders: bring and combine.

What is staging?

Staging is a step before the commit process in git. A file is in staging means preparing for a commit.

What is “Staging Area” or “Index” in GIT?

Before completing the commits, it can be formatted and reviewed in an intermediate area known as ‘Staging Area’ or ‘Index’.

Every change in the file or code first verified in the staging area. Sometimes we can call as “stage file” and then that change is committed to the repository.

10)   What is GIT stash?

Use Git Stash feature to save your local changes temporarily.

“git stash save”

Sometimes when you’ve been working on part of your project and you want to switch branches for some time to work on something else. The problem is, you don’t want to do a commit of half-done work and you don’t want to lose changes then we can use the git stash.

GIT stash takes the current state of the working directory and index and puts in on the stack for later and gives you back a clean working directory.

Explain git stash pop

Git stash pop applies the top stashed element and removes it from the stack. git stash apply does the same, but leaves it in the stash stack.

What is the function of ‘git stash apply’?

When you want to continue working where you have left your work, ‘git stash apply’ command is used to bring back the saved changes onto the working directory.

What is Git stash drop?

Git ‘stash drop’ command is used to remove the stashed item. It will remove the last added stash item by default, and it can also remove a specific item if you include it as an argument.

If you want to remove a particular stash item from the list of stashed items you can use the below commands:

git stash list: It will display the list of stashed items like:

stash@{0}: WIP on master: 049d078 added the index file

stash@{1}: WIP on master: c264051 Revert “added file_size”

stash@{2}: WIP on master: 21d80a5 added number to log

If you want to remove an item named stash@{0} use command git stash drop stash@{0}.

Name a few Git repository hosting services

  • Pikacode
  • Visual Studio Online
  • GitHub
  • Git Enterprise
  • SourceForge.net

Difference between Git clone vs git pull vs git fetch vs git rebase?

Git Pull

Git pull is a combination of git fetch and git merge. In the simplest terms, git pull does a git fetch followed by a git merge

                                         git pull == git fetch + git merge

Git pull command is used to download repository changes from a remote repository and merge those changes into your local repository.

Git pull is useful when you want to update your repository with the changes made in the remote repository.

“Git pull” tries to merge remote changes with your local ones, a so “merge conflict” can occur. It’s highly recommended to start a “git pull” only with a clean working copy. This means that you should not have any uncommitted local changes before you pull. Use Stash feature of git to save your local changes temporarily.

git pull –rebase = git fetch + git rebase

Git Fetch

Fetch is similar to pull, except it won’t do any merging.

Git fetch does not integrate the changes into your local repository.

When you perform a git fetch, it pulls all new commits from the desired branch and stores it in a new branch in your local repository. If you want to reflect these changes in your target branch, git fetch must be followed with a git merge. Your target branch will only be updated after merging the target branch and fetched branch.

Fetch is great for getting a fresh view on all the things that happened in a remote repository.

Git Clone

Git clone will clone a repo into a newly created directory.

Clone: Get a working copy of the remote repository.

Git clone creates remote-tracking branches for each branch in the cloned repository (visible using git branch -r) and creates and checks out an initial branch that is forked from the cloned repository’s currently active branch.

Git clone will set up additional remote tracking branches, not just master.

What is the function of the git clone?

The git clone command creates a copy of an existing Git repository.  To get a copy of a central repository, ‘cloning’ is the most common way used by programmers.

What is the difference between ‘git remote’ and ‘git clone’?

git remote is used to refer to a remote repository or your central repository.

For e.g: When you want to add a remote repository as your origin, you use this command

‘git remote add’  just creates an entry in your git config that specifies a name for a particular URL. You must have an existing git repo to use this.

For example,

 # mkdir foo
 # cd foo
 # git init
 # git remote add origin REMOTEURL
 # git pull origin master

While ‘git clone’ creates a new git repository by copying an existing one located at the URI.

What is the difference between Git reset and Git revert?

Git Revert is used to undo a previous commit. In git, you can’t alter or erase an earlier commit.

Most of the times a commit is reverted when it is causing the problem during testing.

In simple terms to revert a particular commit git revert <commit-id>.This internally creates a new(separate) commit for reverting a previous commit. Hence, adds a new commit history to the project, but it does not modify the existing one.

Git Reset is used to undo changes in your working directory that haven’t been committed yet.

Git reset keeps the entire working tree to the last committed state.

Git Reset alters the existing commit history.

What is the difference between Git merge and Git rebase?

What is the difference between Git Clone and Git fork?

A “fork” is the typical nickname for a server-side clone.

When you are Forking a repository you are creating a copy of repository under the GitHub Id. Any changes made to the original repository will be reflected back to your forked repository. However, if you make any changes to your forked repository you will have to explicitly create a pull request to the original repository. When your pull request is approved by the administrator of the original repository, then your changes will be merged with the existing original code-base. Until then, your changes will be reflected only in the copy you forked.

A fork is a copy of a repository that allows you to freely experiment with changes without affecting the original project.

Forking a project is as easy as clicking the Fork button in the header of a repository. Once the process is complete, you’ll be taken right to your forked copy of the project so you can start collaborating!

When you create a new repository on GitHub, it exists as a remote location where your project is stored. You can clone your repository to create a local copy on your computer so that you can sync between both the local and remote locations of the project.

Unlike forking, you won’t be able to pull down changes from the original repository you cloned from, and if the project is owned by someone else you won’t be able to contribute back to it unless you are specifically invited as a collaborator. If you are invited as a collaborator you can pull down the latest changes from remote repo and you can integrate your changes with the remote repo.

Cloning is ideal for instances when you need a way to quickly get your own copy of a repository where you may not be contributing to the original project.

To clone a repository, head over to the main page of a project and click the Clone or download button to get the repository’s HTTPS or SSH URL. Then, you can perform the clone using the `git clone` command in your command line interface of choice.

Will my fork contains the same data as the original project?

Forking a repository will copy the main data such as files and code. Issues, branches, pull requests and other features, however, will not copy over to your fork. Instead, your fork will start the same way as a newly created repository, but with all of the content present at the time of forking, so you can work on it as a fresh project.

When should I fork a repository?

If you want a link to exist between your copy of a project and the original repository, you should create a fork. This will allow you to make changes to your fork, then open a pull request to the original to propose your changes. Forking is ideal for open-source collaboration, as it allows for anyone to propose changes to a project that the original repository maintainer can choose to integrate.

If I want to back up my repository, should I clone it?

Cloning a repository is a great way to create a backup. However, while your clone will copy over Git data like files and commit history, it won’t bring over issues, pull requests, and other GitHub elements.

When should I clone a repository?

Do you require a copy of a project, and do not need to sync your copy with the original project? If so, a clone is more suitable because it does not share a connection with the original repository.

What is the difference between git pull origin master and git pull origin/master?

git pull origin master will pull changes from the origin remote, master branch and merge them into the local checked-out branch.

git pull origin/master will pull changes from the locally stored branch origin/master and merge that to the local checked-out branch.

How to check available local and remote branches?

You can see what branches are available with git branch and git branch -r to see the “remote branches”.

How will you know in GIT if a branch has been already merged into master?

“Git branch –merged” lists the branches that have been merged into the current branch

“Git branch –no merged” lists the branches that have not been merged

What is the function of ‘git config’?

The ‘git config’ command is a convenient way to set configuration options for your Git installation.  The behavior of a repository, user info, preferences etc. can be defined through this command.

Git identifies commits using the username so git config command can include username to change the git configuration.

git config –global user.name “Your Name”  //add username.

git config –global user.email “Your E-mail Address”  //add email id.

How can we see the configuration settings of GIT installation?

‘git config –list’ prints all the GIT arrangement settings in GIT installation.

What does commit object contain?

A set of files, representing the state of a project at a given point of time

Reference to parent commit objects

An SHAI name, a 40 character string that uniquely identifies the commit object.

How can you create a repository in Git?

In Git, to create a repository, create a directory for the project if it does not exist, and then run the command “git init”. By running this command .git directory will be created in the project directory, the directory does not need to be empty.

What is ‘head’ in git and how many heads can be created in a repository?

A ‘head’ is simply a reference to a commit object. In every repository, there is a default head referred as “Master”.  A repository can contain any number of heads.

What is the purpose of branching in GIT?

The purpose of branching in GIT is that you can create your own branch and jump between those branches. It will allow you to go to your previous work keeping your recent work intact.

What is the common branching pattern in GIT?

The common way of creating a branch in GIT is to maintain one as “Main“ branch and create another branch to implement new features. This pattern is particularly useful when there are multiple developers working on a single project.

How can you bring a new feature into the main branch?

To bring a new feature in the main branch, you can use a command “git merge” or “git pull command”.

What is a ‘conflict’ in git?

A ‘conflict’ arises when the commit that has to be merged has some change in one place, and the current commit also has a change at the same place. Git will not be able to predict which change should take precedence.

How can conflict in git resolved?

To resolve the conflict in git, edit the files to fix the conflicting changes and then add the resolved files by running “git add” after that to commit the repaired merge,  run “git commit”. Git remembers that you are in the middle of a merger, so it sets the parents of the commit correctly.

To delete a branch what is the command that is used?

git push –delete origin master(remote_name) branch_name // Remote

git branch -d branch_name // Local

What is another option for merging in git?

“Rebasing” is an alternative to merging in git.

What is the syntax for “Rebasing” in Git?

The syntax used for rebase is “git rebase [new-commit] “

What is GIT version control?

With the help of GIT version control, you can track the history of a collection of files and includes the functionality to revert the collection of files to another version.  Each version captures a snapshot of the file system at a certain point in time. A collection of files and their complete history are stored in a repository.

Nothing that is committed to version control is ever lost. it’s always possible to go back in time to know exactly who wrote what on a particular day, or what version of a program was used to generate a particular set of results. This means it can be used like the undo feature in an editor, and since all previous versions of files are saved.

Git is one of many version control systems. It is more complex than some alternatives, but it is widely used, both because it’s easy to set up and because of a hosting site called GitHub, which we will get to later.

Mention some of the best graphical GIT clients for LINUX?

Git Cola, Git-g, Smartgit, Giggle, Git GUI, qGit etc.

What is Subgit? Why use Subgit?

‘Subgit’ is a tool used for a migration from SVN to Git.

  1. a)   It is much better than git-svn
  2. b)   No requirement to change the infrastructure that is already placed
  3. c)    Allows to use all git and all sub-version features
  4. d)   Provides genuine stress –free migration experience.

What is the function of ‘git diff ’ in git?

‘git diff ’ shows the changes between commits, commit and working directory etc.

What is ‘git status’ is used for?

As ‘Git Status’ shows you the difference between the working directory and the index, it is helpful in understanding a git more comprehensively.

What is the difference between the ‘git diff ’and ‘git status’?

‘git diff’ is similar to ‘git status’, but it shows the differences between various commits and also between the working directory and index.

What is the function of ‘git checkout’ in git?

You can work on multiple features in a single repository by switching between them with git checkout.

What is the function of ‘git rm’?

To remove the file from the staging area and also of your disk ‘git rm’ is used.

What is the use of ‘git log’?

To find specific commits in your project history- by author, date, content or history ‘git log’ is used.

What is ‘git add’ is used for?

‘git add’ adds file changes in your existing directory to your index.

What is the function of ‘git reset’?

The function of ‘Git Reset’ is to reset your index as well as the working directory to the state of your last commit.

What is git Is-tree?

‘git Is-tree’ represents a tree object including the mode and the name of each item and the SHA-1 value of the blob or the tree.

How git instaweb is used?

‘Git Instaweb’ automatically directs a web browser and runs the web server with an interface into your local repository.

What does ‘hooks’ consist of in git?

This directory consists of Shell scripts which are activated after running the corresponding Git commands.  For example, git will try to execute the post-commit script after you run a commit.

Explain what is commit message?

Commit message is a feature of git which appears when you commit a change. Git provides you a text editor where you can enter the modifications made in commits.

How can you fix a broken commit?

To fix any broken commit, you will use the command “git commit—amend”. By running this command, you can fix the broken commit message in the editor.

Why is it advisable to create an additional commit rather than amending an existing commit?

There are a couple of reason

  1. a)   The amend operation will destroy the state that was previously saved in a commit.  If it’s just the commit message being changed then that’s not an issue. But if the contents are being amended then chances of eliminating something important remains more.
  2. b)   Abusing “git commit- amend” can cause a small commit to grow and acquire unrelated changes.

What is ‘bare repository’ in GIT?

Working repository created with git init is for… working.

It is where you will actually edit, add and delete files and git commit to saving your changes.

A bare repository created with git init –bare is for… sharing.

The central repository is bare, and developers local repositories are non-bare.

If you are collaborating with a team of developers, and need a place to share changes to a repo, then you will want to create a bare repository in a centralized place where all users can push their changes (often the easy choice is github.com). Because git is a distributed version control system, no one will directly edit files in the shared centralized repository.

Instead, developers will clone the shared bare repo, make changes locally in their working copies of the repo, then push back to the shared bare repo to make their changes available to other users.

A “bare” repository in Git just contains the version control information and no working files (no tree) and it doesn’t contain the special .git sub-directory.

What is the origin?

Origin is not the remote repository name. Git has the concept of “remotes”, which are simply URLs to other copies of your repository.

When you clone another repository, Git automatically creates a remote named “origin” and points to it.

You can change the remote in the Admin settings for your GitHub repo. You can also do it from the command-line via

git remote set-head origin trunk

We can delete remote(origin) using a command

git remote set-head origin -d

What is a merge conflict?

Merge means we can integrate some changes with our current working directory.

If two people working on the same file and they have modified the same line or one person want to delete it and other person wants to modify it.

So simply git don’t know what is correct and it will throw merge conflict error.

For example, if you and another person both edited the file style guide.md on the same lines in different branches of the same Git repository, you’ll get a merge conflict error when you try to merge these branches

You can return to the state before you started the merge at any time. This should give you the confidence that you can’t break anything.

“git merge –abort”

In case you’ve made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it.

Just roll back to the commit before the merge happened with “git reset –hard ” and start over again.

“git reset –hard “

In Git how do you revert a commit that has already been pushed and made public?

Approach 1

  • Remove or fix the bad file in a new commit and push it to the remote repository. This is the most natural way to fix an error. Once you have made necessary changes to the file, commit it to the remote repository for that I will use
  • git commit -m “commit message”

Approach 2

  • Create a new commit that undoes all changes that were made in the bad commit.to do this I will use a command
  • git revert

Describe branching strategies you have used?

  • Feature branching

A feature branch model keeps all of the changes for a particular feature inside of a branch. When the feature is fully tested and validated by automated tests, the branch is then merged into master.

  • Task branching

In this model, each task is implemented on its own branch with the task key included in the branch name. It is easy to see which code implements which task, just look for the task key in the branch name.

  • Release branching

Once the develop branch has acquired enough features for a release, you can clone that branch to form a Release branch. Creating this branch starts the next release cycle so no new features can be added after this point, only bug fixes, documentation generation, and other release-oriented tasks should go in this branch. Once it is ready to ship, the release gets merged into master and tagged with a version number. In addition, it should be merged back into the develop branch, which may have progressed since the release was initiated.

Branching strategies vary from one organization to another so I know basic branching operations like delete, merge, checking out a branch etc..

What are git hooks?

Git hooks are scripts that Git executed before or after events such as commit, push, and receive. Git hooks are a built-in feature – no need to download anything. Git hooks are run locally.

What are the different ways to start work in GIT?

We can begin work on GIT in the following ways:

New Project: git init.

Existing Project: git clone

How will you write a message with commit command in GIT?

git commit –m < message >

Why do we create branches in GIT?

In GIT we can make a different branch for each different reason.

Let us try to understand using simple timeline:

  1. You are working on a product for with you have the repo in Git
  2. The product is ready and you released version 1.0
  3. 10 clients started using your product
  4. After 1.0 release now you are working on new features for 2.0 and developers are committing code to the repo.
  5. After 1 month few bugs are reported by your clients in version 1.0
  6. Now you have to fix these bugs and release version 1.1. But where will you fix these bugs? Your developers already committed new changes (towards v 2.0) to the repo and you can not wait for release 2.0 to fix those bugs.

Now let us see how branching can solve this issue.

  1. You are working on a product for with you have “dev” branch in Git repo.
  2. The product is ready and you released version 1.0
  3. At the same time, you created the branch “v1.0”
  4. 10 clients started using your product
  5. After 1.0 release now you are working on new features for 2.0 and developers are committing code to “dev” branch.
  6. After 1 month few bugs are reported by your clients in version 1.0
  7. One of the developer has just checked out code from branch “v1.0”.
  8. Fixed the bugs committed the changes to branch “v1.0”
  9. Created the new branch “v1.1” from branch “v1.0”
  10. Released the product version 1.1 with bug fixes over 1.0.
  11. Most important these same bugs should not go in version 2.0 so merge changes done on branch “v1.0” to branch “dev”.
  12. The same process will be applicable for release 2.0 and so on.

What are the different kinds of branches that can be created in GIT?

We can make various types of branches for the following purposes in GIT:

Feature branches: These are utilized for building up a component.

Release branches: These are utilized for discharging code to create.

Hotfix branches: These are utilized for discharging a hotfix to generation for an imperfection or crisis settle.

How will you create a new branch in GIT?

git checkout –b < branch name >

git branch branch-name

How do you find a list of files that have changed in a particular commit?

Approach 1

git diff-tree -r {hash}

This will list all the files that were changed or added in that commit. The -r flag makes the command list individual files, rather than collapsing them into root directory names only.

Approach 2

git diff-tree –no-commit-id –name-only -r {hash}

Here –no-commit-id will suppress the commit hashes from appearing in the output, and –name-only will only print the filenames, instead of their paths.

What is Git bisect? How can you use it to determine the source of a (regression) bug?

Git bisect is used to find the bad or bug commit by using binary search. The command for Git bisect is

git bisect

Git bisect uses a binary search algorithm to find which commit in your project’s history introduced a bug.

There are two commits in git one is Good and another is Bad. Bad commit means in that commit bug was found.

Then Git bisect picks a commit between those two endpoints and asks you whether the selected commit is “good” or “bad”. It continues narrowing down the range until it finds the exact commit that introduced the change.

How do you configure a Git repository to run code sanity(intelligence) checking tools right before making commits, and preventing them if the test fails?

A sanity or smoke test determines whether it is possible and reasonable to continue testing.

This can be done with a simple script related to the pre-commit hook of the repository. The pre-commit hook is triggered right before a commit is made, even before you are required to enter a commit message. In this script, one can run other tools, such as linters and perform sanity checks on the changes being committed into the repository.

#!/bin/sh

files=$(git diff –cached –name-only –diff-filter=ACM | grep ‘.go$’)

if [ -z files ]; then

exit 0

fi

unfmtd=$(gofmt -l $files)

if [ -z unfmtd ]; then

exit 0

fi

echo “Some .go files are not fmt’d”

exit 1

This script checks to see if any .go file that is about to be committed needs to be passed through the standard Go source code formatting tool gofmt. By exiting with a non-zero status, the script effectively prevents the commit from being applied to the repository.

What is Git rebase and how can it be used to resolve conflicts in a feature branch before the merge?

Git rebase is a command which will merge another branch into the branch where you are currently working, and move all of the local commits that are ahead of the rebased branch to the top of the history on that branch.

If a feature branch was created from the master, and since then the master branch has received new commits, Git rebase can be used to move the feature branch to the tip of master. The command effectively will replay the changes made in the feature branch at the tip of master, allowing conflicts to be resolved in the process. When done with care, this will allow the feature branch to be merged into master with relative ease and sometimes as a simple fast-forward operation.

How to rebase master in git? Difference between rebase and merge. How to squash or fixup commits?

Rebasing is the process of moving a branch to a new base commit.

The golden rule of git rebase is to never use it on public branches. … The only way to synchronize the two master branches is to merge them back together, resulting in an extra merge commit and two sets of commits that contain the same changes.

What is Git repository hosting services you used?

Github, Bitbucket, Gitlab.

What is Git fork? What is the difference between fork and branch? How to create a tag?

A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.

A fork is really a Github (not Git) construct to store a clone of the repo in your user account. As a clone, it will contain all the branches in the main repo at the time you made the fork.

Create Tag:

  • Click the releases link on our repository page.
  • Click on Create a new release or Draft a new release.
  • Fill out the form fields, then click Publish release at the bottom.
  • After you create your tag on GitHub, you might want to fetch it into your local repository to git fetch.

How to rebase master in git? Difference between rebase and merge. How to squash or fixup commits?

Rebasing is the process of moving a branch to a new base commit.

The golden rule of git rebase is to never use it on public branches. … The only way to synchronize the two master branches is to merge them back together, resulting in an extra merge commit and two sets of commits that contain the same changes.

What is git cherry-pick?

Cherry picking in git means to choose a commit from one branch and apply it onto another.

Cherry-pick uses a diff to find the difference between branches.

This is in contrast with other ways such as merge and rebase which normally applies many commits onto another branch.

Make sure you are on the branch you want to apply the commit to. git checkout master Execute the following:

            git cherry-pick

For example, if you have to merge commit ref 63ad84c, you have to specify -m and use parent 1 as a base:

# git checkout release_branch

# git cherry-pick -m 1 63ad84c

Branching strategies Pros and Cons — Feature, Release branching, Git flow, Lean Git flow?

What are the different ways you can refer to a commit?

 

Create your website at WordPress.com
Get started
%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close