Git Tutorial
Git Tutorial
This command configures the user. The Git config command is the first
and necessary command used on the Git command line. This command
sets the author name and email address to be used with your commits.
Git config is also used in other scenarios.
Syntax
Syntax
Git Init
The git init command is the first command that you will run on Git. The git
init command is used to create a new blank repository. It is used to make
an existing project as a Git project. Several Git commands run inside the
repository, but init command can be run outside of the repository.
The above command will create an empty .git repository under a directory
named NewDirectory.
Git Add
The git add command is used to add file contents to the Index (Staging
Area).This command updates the current content of the working tree to
the staging area. It also prepares the staged content for the next commit.
Every time we add or update any file in our project, it is required to
forward updates to the staging area.
1. $ touch newfile.txt
1. $ git status
The above command will display the untracked files from the repository.
These files can be added to our repository. As we know we have created a
newfile.txt, so to add this file, run the below command:
1. $ git add newfile.txt
Git Add All
We can add more than one files in Git, but we have to run the add
command repeatedly. Git facilitates us with a unique option of the add
command by which we can add all the available files at once. To add all
the files from the repository, run the add command with -A option. We can
use '.' Instead of -A option. This command will stage all the files at a time.
It will run as follows:
1. $ git add -A
Or
1. $ git add .
Git add files
Git add command is a straight forward command. It adds files to the
staging area. We can add single or multiple files at once in the staging
area. It will be run as:
The above command is added to the git staging area, but yet it cannot be
shared on the version control system. A commit operation is needed to
share it. Let's understand the below scenario.
1. $ touch newfile.txt
1. $ git status
The above command will display the untracked files from the repository.
These files can be added to our repository. As we know we have created a
newfile.txt, so to add this file, run the below command:
1. $ git add -A
Or
1. $ git add .
The above command will add all the files available in the repository.
Consider the below scenario:
We can either create four new files, or we can copy it, and then we add all
these files at once. Consider the below output:
In the above output, all the files are displaying as untracked files by Git.
To track all of these files at once, run the below command:
1. $ git add -A
The above command will add all the files to the staging area. Remember,
the -A option is case sensitive. Consider the below output:
In the above output, all the files have been added. The status of all files is
displaying as staged.
Removing Files from the Staging Area
The git add command is also used to remove files from the staging area. If
we delete a file from the repository, then it is available to our repository
as an untracked file. The add command is used to remove it from the
staging area. It sounds strange, but Git can do it. Consider the below
scenario:
We have deleted the newfile3.txt from the repository. The status of the
repository after deleting the file is as follows:
As we can see from the above output, the deleted file is still available in
the staging area. To remove it from the index, run the below command as
follows:
1. $ git add -u
Add Files by Wildcard
Git allows us to add all the same pattern files at once. It is another way to
add multiple files together. Suppose I want to add all java files or text
files, then we can use pattern .java or .txt. To do so, we will run the
command as follows:
The above command will stage all the Java files. The same pattern will be
applied for the text files.
Git Commit
It is used to record the changes in the repository. It is the next command
after the git add. Every commit contains the index data and the commit
message. Every commit forms a parent-child relationship. When we add a
file in Git, it will take place in the staging area. A commit command is
used to fetch updates from the staging area to the repository.
The staging and committing are co-related to each other. Staging allows
us to continue in making changes to the repository, and when we want to
share these changes to the version control system, committing allows us
to record these changes.
1. $ git commit
Git commit -a
The commit command also provides -a option to specify some commits. It
is used to commit the snapshots of all changes. This option only consider
already added files in Git. It will not commit the newly created files.
Consider below scenario:
We have made some updates to our already staged file newfile3 and
create a file newfile4.txt. Check the status of the repository and run the
commit command as follows:
1. $ git commit -a
it commit -m
The -m option of commit command lets you to write the commit message
on the command line. This command will not prompt the text editor. It will
run as follows:
We can also use the -am option for already staged files. This command
will immediately make a commit for already staged files with a commit
message. It will run as follows:
The above command will prompt the default text editor and allow us to
edit the commit message.
Git Clone
In Git, cloning is the act of making a copy of any target repository. The
target repository can be remote or local. You can clone your repository
from the remote repository to create a local copy on your system. Also,
you can sync between the two locations.
Git Clone Command
The git clone is a command-line utility which is used to make a local copy
of a remote repository. It accesses the repository through a remote URL.
Syntax:
Syntax:
o Git stash
o Git stash save
o Git stash list
o Git stash apply
o Git stash changes
o Git stash pop
o Git stash drop
o Git stash clear
o Git stash branch
Stashing Work
The git stash command is used as:
Syntax:
1. $ git stash
Output:
In the given output, the work is saved with git stash command. We can
check the status of the repository.
As you can see, my work is just stashed in its current position. Now, the
directory is cleaned. At this point, you can switch between branches and
work on them.
Syntax:
Output:
Syntax:
If we have more than one stash, then It will display all the stashes
respectively with different stash id. Consider the below output:
Syntax:
Output:
The above output restores the last stash. Now, if you will check the status
of the repository, it will show the changes that are made on the file.
Consider the below output:
From the above output, you can see that the repository is restored to its
previous state before stash. It is showing output as "Changes not
staged for commit."
In case of more than one stash, you can use "git stash apply" command
followed by stash index id to apply the particular commit. It is used as:
Syntax:
Output:
If we don't specify a stash, Git takes the most recent stash and tries to
apply it.
Syntax:
The above command will show the file that is stashed and changes made
on them. Consider the below output:
Output:
The above output illustrates that there are two files that are stashed, and
two insertions performed on them.
We can exactly track what changes are made on the file. To display the
changed content of the file, perform the below command:
Syntax:
Here, -p stands for the partial stash. The given command will show the
edited files and content, consider the below output:
Output:
The above output is showing the file name with changed content. It acts
the same as git diff command. The git diff command will also show the
exact output.
Git Stash Pop (Reapplying Stashed Changes)
Git allows the user to re-apply the previous commits by using git stash
pop command. The popping option removes the changes from stash and
applies them to your working file.
The git stash pop command is quite similar to git stash apply. The main
difference between both of these commands is stash pop command that
deletes the stash from the stack after it is applied.
Syntax:
The above command will re-apply the previous commits to the repository.
Consider the below output.
Output:
The only way to revert it is if you do not close the terminal after deleting
the stash. The stash drop command will be used as:
Syntax:
Output:
In the above output, the most recent stash (stash@{0}) has been
dropped from given three stashes. The stash list command lists all the
available stashes in the queue.
Syntax:
Assume that I have two stashes available in my queue, and I don't want to
drop my most recent stash, but I want to delete the older one. Then, it will
be operated as:
In the above output, the commit stash@{1} has been deleted from the
queue.
Output:
All the stashes are deleted in the above output. The git stash list
command is blank because there are no stashes available in the
repository.
The git stash branch command allows the user to stash work on a
separate branch to avoid conflicts. The syntax for this branch is as follows:
Syntax:
The above command will create a new branch and transfer the stashed
work on that. Consider the below output:
Output:
Git Ignore
In Git, the term "ignore" is used to specify intentionally untracked files
that Git should ignore. It doesn't affect the Files that already tracked by
Git.
Sometimes you don't want to send the files to Git service like GitHub. We
can specify files in Git to ignore.
Tracked:
Tracked files are such files that are previously staged or committed.
Untracked:
Untracked files are such files that are not previously staged or committed.
Ignored:
Ignored files are such files that are explicitly ignored by git. We have to
tell git to ignore such files.
o dependency caches
o compiled code
o build output directories, like /bin, /out, or /target
o runtime file generated, like .log, .lock, or .tmp
o Hidden system files, like Thumbs.db or.DS_Store
o Personal IDE config files, such as .idea/workspace.xml
There is no explicit git ignore command; instead, the .gitignore file must
be edited and committed by hand when you have new files that you wish
to ignore. The .gitignore files hold patterns that are matched against file
names in your repository to determine whether or not they should be
ignored.
Step1: Create a file named .gitignore if you do not have it already in your
directory. To create a file, use the command touch or cat. It will use as
follows:
1. $ touch .gitignore
Or
1. $ cat .gitignore
The above command will create a file named .gitignored. We can track it
on the repository. Consider the below image:
As you can see from the above image, a .gitignore file has been created
for my repository.
Step2: Now, add the files and directories to the .gitignore file that you
want to ignore. To add the files and directory to the .git ignore the file,
open the file and type the file name, directory name, and pattern to
ignore files and directories. Consider the below image:
In the above file, I have given one format and a directory to ignore. The
above format *.txt will ignore all the text files from the repository,
and /newfolder/* will ignore the newfolder and its sub-content. We can
also give only the name of any file to ignore.
Step3: Now, to share it on Git, we have to commit it. The .gitignore file is
still now in staging area; we can track it by git status command. Consider
the below output:
Now to stage it, we have to commit it. To commit it, run the below
command:
The above command will share the file .gitignore on Git. Consider the
below output.
The rules for the patterns that can be put in the .gitignore file are as
follows:
Global .gitignore:.
As we know that we can create multiple .gitignore files for a project. But
Git also allows us to create a universal .gitignore file that can be used for
the whole project. This file is known as a global .gitignore file. To create a
global .gitignore, run the below command on terminal:
The above command will create a global .gitignore file for the repository.
How to List the Ignored Files?
In Git, We can list the ignored files. There are various commands to list
the ignored files, but the most common way to list the file is the ls
command. To list the ignored file, run the ls command as follows:
Or
The above command will list all available ignored files from the repository.
In the given command, -I option stands for ignore and --exclude-
standard is specifying the exclude pattern. Consider the below output:
From the above output, we can see that the ls command is listing the
available ignored files from the repository.
Git Fork
A fork is a rough copy of a repository. Forking a repository allows you to
freely test and debug with changes without affecting the original project.
One of the excessive use of forking is to propose changes for bug fixing.
To resolve an issue for a bug that you found, you can:
We can't fork our own repository. Only shared repositories can be fork. If
someone wants to fork the repository, then he must log in with his
account. Let's understand the below scenario in which a user pune2016
wants to contribute to our project GitExample2. When he searches or put
the address of our repository, our repository will look like as follows:
The above image shows the user interface of my repository from other
contributors. We can see the fork option at the top right corner of the
repository page. By clicking on that, the forking process will start. It will
take a while to make a copy of the project for other users. After the
forking completed, a copy of the repository will be copied to your GitHub
account. It will not affect the original repository. We can freely make
changes and then create a pull request for the main project. The owner of
the project will see your suggestion and decide whether he wants to
merge the changes or not. The fork copy will look like as follows:
Generally, people working on the same project clone the repository and
the external contributors fork the repository.
A pull request can merge the changes made on the fork repository. We
can create a pull request to propose changes to the project.
Comparatively, changes made on the cloned repository can be merged by
pushing. We can push the changes to our remote repository.
Git Repository
In Git, the repository is like a data structure used by VCS to store
metadata for a set of files and directories. It contains the collection of the
files as well as the history of changes made to those files. Repository in
Git is considered as your project folder. A repository has all the project-
related data. Distinct projects have distinct repositories.
Initializing a Repository
If you want to share your project on a version control system and control it
with Git. Then, browse your project's directory and start the git command
line (Git Bash for Windows) here. To initialize a new repository, run the
below command:
Syntax:
1. $ git init
Output:
The above command will create a new subdirectory named .git that holds
all necessary repository files. The .git subdirectory can be understood as
a Git repository skeleton. Consider the below image:
In the above output, the list of all untracked files is displayed by the git
status command. To share these files on the version control system, we
have to track it with git add command followed by a commit. To track the
files, operate git add command as follows:
Syntax:
Output:
In the above output, I have added three of my existing files by git add
command and commit it for sharing.
We can also create new files. To share the new file, follow the same
procedure as described above; add and commit it for sharing. Now, you
have a repository to share.
We can get nearly all data from server with git clone command. It can be
done as:
Syntax:
Git Index
The Git index is a staging area between the working directory and
repository. It is used to build up a set of changes that you want to commit
together. To better understand the Git index, then first understand the
working directory and repository.
There are three places in Git where file changes can reside, and these are
working directory, staging area, and the repository. To better understand
the Git index first, let's take a quick view of these places.
Working directory:
When you worked on your project and made some changes, you are
dealing with your project's working directory. This project directory is
available on your computer's filesystem. All the changes you make will
remain in the working directory until you add them to the staging area.
Staging area:
The staging area can be described as a preview of your next commit.
When you create a git commit, Git takes changes that are in the staging
area and make them as a new commit. You are allowed to add and
remove changes from the staging area. The staging area can be
considered as a real area where git stores the changes.
Repository:
In Git, Repository is like a data structure used by GIt to store metadata for
a set of files and directories. It contains the collection of the files as well
as the history of changes made to those files. Repositories in Git is
considered as your project folder. A repository has all the project-related
data. Distinct projects have distinct repositories.
You can check what is in the index by the git status command. The git
status command allows you to see which files are staged, modified but not
yet staged, and completely untracked. Staged files mean, it is currently in
the index. See the below example.
Syntax:
1. $ git status
Output:
And finally, Git creates your working directory to match the content of the
commit that HEAD is pointing.
Git Head
The HEAD points out the last commit in the current checkout branch. It is
like a pointer to any reference. The HEAD can be understood as the
"current branch." When you switch branches with 'checkout,' the HEAD
is transferred to the new branch.
Syntax:
Output:
In the above output, you can see that the commit id for the Head is given.
It means the Head is on the given commit.
Now, check the commit history of the project. You can use the git log
command to check the commit history. See the below output:
As we can see in the above output, the commit id for most recent commit
and Head is the same. So, it is clear that the last commit has the Head.
We can also check the status of the Head by the commit id. Copy the
commit id from the above output and paste it with the git
show command. Its result is same as git show head command if the
commit id is last commit's id. See the below output:
The HEAD is capable of referring to a specific revision that is not associated with
a branch name. This situation is called a detached HEAD.
As you can see in the given example, Head does not point the most recent
commit. It is called a detached head state. It is always recommended, do
not commit on detached Head.
Git Origin Master
The term "git origin master" is used in the context of a remote repository.
It is used to deal with the remote repository. The term origin comes from
where repository original situated and master stands for the main branch.
Let's understand both of these terms in detail.
Git Master
Master is a naming convention for Git branch. It's a default branch of Git.
After cloning a project from a remote server, the resulting local repository
contains only a single local branch. This branch is called a "master"
branch. It means that "master" is a repository's "default" branch.
Do not mess with the master. If you edited the master branch of a group
project, your changes will affect everyone else and very quickly there will
be merge conflicts.
Git Origin
In Git, The term origin is referred to the remote repository where you want
to publish your commits. The default remote repository is called origin,
although you can work with several remotes having a different name at
the same time. It is said as an alias of the system.
The origin is a short name for the remote repository that a project was
initially being cloned. It is used in place of the original repository URL.
Thus, it makes referencing much easier.
Some commands in which the term origin and master are widely used are
as follows:
Git has two types of branches called local and remote. To use git pull and
git push, you have to tell your local branch that on which branch is going
to operate. So, the term origin master is used to deal with a remote
repository and master branch. The term push origin master is used to
push the changes to the remote repository. The term pull origin
master is used to access the repository from remote to local.
Git Remote
In Git, the term remote is concerned with the remote repository. It is a
shared repository that all team members use to exchange their changes.
A remote repository is stored on a code hosting service like an internal
server, GitHub, Subversion, and more. In the case of a local repository, a
remote typically does not provide a file tree of the project's current state;
as an alternative, it only consists of the .git versioning data.
The developers can perform many operations with the remote server.
These operations can be a clone, fetch, push, pull, and more. Consider the
below image:
Check your Remote
To check the configuration of the remote server, run the git
remote command. The git remote command allows accessing the
connection between remote and local. If you want to see the original
existence of your cloned repository, use the git remote command. It can
be used as:
Syntax:
1. $ git remote
Output:
The given command is providing the remote name as the origin. Origin is
the default name for the remote server, which is given by Git.
Git remote supports a specific option -v to show the URLs that Git has
stored as a short name. These short names are used during the reading
and write operation. Here, -v stands for verbose. We can use --
verbose in place of -v. It is used as:
Syntax:
1. $ git remote -v
Or
Output:
Syntax:
Output:
In the above output, I have added a remote repository with an existing
repository as a short name "hd". Now, you can use "hd" on the command
line in place of the whole URL. For example, you want to pull the
repository, consider below output:
I have pulled a repository using its short name instead of its remote URL.
Now, the repository master branch can be accessed through a short
name.
To fetch the data from your remote projects, run the below command:
1. $ git clone<remote>
The git fetch origin fetches the updates that have been made to the
remote server since you cloned it. The git fetch command only downloads
the data to the local repository; it doesn't merge or modify the data until
you don't operate. You have to merge it manually into your repository
when you want.
To pull the repository, run the below command:
The git pull command automatically fetches and then merges the remote
data into your current branch. Pulling is an easier and comfortable
workflow than fetching. Because the git clone command sets up your local
master branch to track the remote master branch on the server you
cloned.
To update the main branch of the project, use the below command:
Generally, the term origin stands for the remote repository, and master is
considered as the main branch. So, the entire statement "git push origin
master" pushed the local content on the master branch of the remote
location.
Syntax:
Or
1. $ git remote -v
Output:
The above output will list the available remote server. Now, perform the
remove operation as mentioned above. Consider the below output:
Syntax:
Output:
In the above output, I have renamed my default server name origin to hd.
Now, I can operate using this name in place of origin. Consider the below
output:
In the above output, I have pulled the remote repository using the server
name hd. But, when I am using the old server name, it is throwing an error
with the message "'origin' does not appear to be a git repository." It
means Git is not identifying the old name, so all the operations will be
performed by a new name.
Syntax:
Output:
The above output is listing the URLs for the remote repository as well as
the tracking branch information. This information will be helpful in various
cases.
We can change the remote URL simply by using the git remote set
command. Suppose we want to make a unique name for our project to
specify it. Git allows us to do so. It is a simple process. To change the
remote URL, use the below command:
The remote set-url command takes two types of arguments. The first
one is <remote name >, it is your current server name for the repository.
The second argument is <newURL>, it is your new URL name for the
repository. The <new URL> should be in below
format: https://github.com/URLChanged
1. $ git remote -v
Git Tags
Tags make a point as a specific point in Git history. Tags are used to mark
a commit stage as relevant. We can tag a commit for future reference.
Primarily, it is used to mark a project's initial point like v1.1.
Tags are much like branches, and they do not change once initiated. We
can have any number of tags on a branch or different branches. The
below figure demonstrates the tags on various branches.
In the above image, there are many versions of a branch. All these
versions are tags in the repository.
o Annotated tag
o Light-weighted tag
Both of these tags are similar, but they are different in case of the amount
of Metadata stores.
Now, you are on your desired branch, say, master. Consider the below
output:
You can create a tag by using the git tag command. Create a tag with
some name say v1.0, v1.1, or any other name whatever you want. To
create a tag, run the command as follows:
Syntax:
The above command will create a mark point on the master branch
as projectv1.0.
o git tag
o git show
o git tag -l ".*"
1. $ git tag
Output:
As we can see from the above output, the git tag command is listing the
available tags from the repository.
Syntax:
The above command will display the tag description, consider the below
command:
Output:
In the above output, the git show tag is displaying the description of
tag projectv1.0, such as author name and date.
Syntax:
The above command is displaying a list of the tags that started with a
word pro.
o Annotated tag
o Light-weighted tag
Annotated Tags
Annotated tags are tags that store extra Metadata like developer name,
email, date, and more. They are stored as a bundle of objects in the Git
database.
If you are pointing and saving a final version of any project, then it is
recommended to create an annotated tag. But if you want to make a
temporary mark point or don't want to share information, then you can
create a light-weight tag. The data provided in annotated tags are
essential for a public release of the project. There are more options
available to annotate, like you can add a message for annotation of the
project.
Syntax:
The above command will create a tag with a message. Annotated tags
contain some additional information like author name and other project
related information. Consider the below image:
The above command will create an annotated tag projectv1.1 in the
master branch of my project's repository.
Light-Weighted Tag:
Git supports one more type of tag; it is called as Light-weighted tag. The
motive of both tags is the same as marking a point in the repository.
Usually, it is a commit stored in a file. It does not store unnecessary
information to keep it light-weight. No command-line option such as -a,-
s or -m are supplied in light-weighted tag, pass a tag name.
Syntax:
The above command will create a light-weight tag. Consider the below
example:
It will display a reduced output than an annotated tag. Consider the below
output:
Git Push Tag
We can push tags to a remote server project. It will help other team
members to know where to pick an update. It will show as release
point on a remote server account. The git push command facilitates with
some specific options to push tags. They are as follows:
Syntax:
The above command will push the specified tag name as a release point.
Consider the below example:
Syntax:
Or
Output:
Tags have been pushed to remote server origin; thus, the release point is
also updated. Consider the below snapshot of the repository:
The release point is updated in the above output according to tags. You
can see that releases updated as 2 releases.
Syntax:
Or
Syntax:
Or
The above command will delete the specified tag from the remote server.
Consider the below output:
The projectv1.0 tag has been deleted from the remote server origin.
Syntax:
The above command will delete both the tags from the local repository.
We can also delete multiple tags from the remote server. To delete tags
from the server origin, run the below command:
The above command will delete both tags from the server.
Syntax:
The above command will create a new branch with the same state of the
repository as it is in the tag. Consider the below output:
The above command will create a new branch and transfer the status of
the repository to new_branchv1.1 as it is on tag projectv1.1.
If you want to go back to your history and want to create a tag on that
point. Git allows you to do so. To create a tag from an older commit, run
the below command:
Suppose I want to create a tag for my older commit, then the process will
be as follows:
1. $ git status
The above output is showing the older commits. Suppose I want to create
a tag for my commit, starting with 828b9628. Copy the particular
reference of the commit. And pass it as an argument in the above
command. Consider the below output:
Upstream and Downstream
The term upstream and downstream refers to the repository. Generally,
upstream is from where you clone the repository, and downstream is any
project that integrates your work with other works. However, these terms
are not restricted to Git repositories.
Git set-upstream
The git set-upstream allows you to set the default remote branch for your
current local branch. By default, every pull command sets the master as
your default remote branch.
Sometimes we are trying to push some changes to the remote server, but
it will show the error like "error: failed to push some refs to 'https :<
remote repository Address>." There may be the reason that you have
not set your remote branch. We can set the remote branch for the local
branch. We will implement the following process to set the remote server:
1. $ git remote -v
1. $ git branch -a
The above command will list the branches on the local and remote
repository. To learn more about branches, click here. Now push the
changes to remote server and set the particular branch as default remote
branch for the local repository. To push the changes and set the remote
branch as default, run the below command:
The above command will set the master branch as the default remote
branch. To better understand the origin master click here.
We can also set the default remote branch by using the git branch
command. To do so, run the below command:
Git Checkout
In Git, the term checkout is used for the act of switching between different
versions of a target entity. The git checkout command is used to switch
between branches in a repository. Be careful with your staged files and
commits when switching between branches.
The git checkout command operates upon three different entities which
are files, commits, and branches. Sometimes this command can be
dangerous because there is no undo option available on this command.
It checks the branches and updates the files in the working directory to
match the version already available in that branch, and it forwards the
updates to Git to save all new commit in that branch.
Checkout Branch
You can demonstrate how to view a list of available branches by executing
the git branch command and switch to a specified branch.
1. $ git branch
Now, you have the list of available branches. To switch between branches,
use the below command.
Syntax:
Output:
As you can see in the given output that master branch has switched to
TestBranch.
Syntax:
Output:
As you can see in the given output, branch3 is created and switched from
the master branch.
In the latest versions of Git, you can check out the remote branch like a
local branch.
Syntax:
Output:
In the above output, first, the fetch command is executed to fetch the
remote data; after that, the checkout command is executed to check out a
remote branch.
The earlier versions of Git require the creation of a new branch based on
the remote. In earlier versions, below command is used to check out the
remote branch.
Git Revert
In Git, the term revert is used to revert some changes. The git revert
command is used to apply revert operation. It is an undo type command.
However, it is not a traditional undo alternative. It does not delete any
data in this process; instead, it will create a new change with the opposite
effect and thereby undo the specified commit. Generally, git revert is a
commit.
It can be useful for tracking bugs in the project. If you want to remove
something from history then git revert is a wrong choice.
Moreover, we can say that git revert records some new changes that are
just opposite to previously made commits. To undo the changes, run the
below command:
Syntax:
1. $ git revert
Git Revert Options:
Git revert allows some additional operations like editing, no editing,
cleanup, and more. Let's understand these options briefly:
-n/--no edit: This option will not open a text editor. It will directly revert
the last commit.
As you can see from the above output that I have made changes in
newfile2.txt. We can undo it by git revert command. To undo the changes,
we will need the commit-ish. To check the commit-ish, run the below
command:
1. $ git log
The above command will revert my last commit. Consider the below
output:
As you can see from the above output, the changes made on the
repository have been reverted.
A merge in Git is a commit that has at least two parents. It brings together
multiple lines of development. In a work-flow where features are
developed in branches and then merged into a mainline, the merge
commits would typically have two parents.
How to Revert a Merge
Usually, reverting a merge considered a complicated process. It can be
complex if not done correctly. We are going to undo a merge operation
with the help of git revert command. Although some other commands like
git reset can do it. Let's understand how to revert a merge. Consider the
below example.
I have made some changes to my file design2.css on the test and merge
it with test2. Consider the below output:
1. $ git log
The above command will display the commit history. Consider the below
output:
From the above output, copy your merging commit that you to want to
revert and run the below command:
1. $ git revert <commit reference> -m 1
The above command will revert the merging operation. Here, -m 1 is used
for the first parent as the mainline. Merge commit has multiple parents.
The revert needs additional information to decide which parent of the
merge shall be considered as the mainline. In such cases, the parameter -
m is used. Consider the below output:
From the above output, we can see that the previous merge has been
reverted.
Git Revert
In Git, the term revert is used to revert some changes. The git revert
command is used to apply revert operation. It is an undo type command.
However, it is not a traditional undo alternative. It does not delete any
data in this process; instead, it will create a new change with the opposite
effect and thereby undo the specified commit. Generally, git revert is a
commit.
It can be useful for tracking bugs in the project. If you want to remove
something from history then git revert is a wrong choice.
Moreover, we can say that git revert records some new changes that are
just opposite to previously made commits. To undo the changes, run the
below command:
Syntax:
1. $ git revert
Git Revert Options:
Git revert allows some additional operations like editing, no editing,
cleanup, and more. Let's understand these options briefly:
< commit>: The commit option is used to revert a commit. To revert a
commit, we need the commit reference id. The git log command can
access it.
-n/--no edit: This option will not open a text editor. It will directly revert
the last commit.
The no-commit option is beneficial for reverting more than one commits
effect to your index in a row.
1. $ git log
The above command will revert my last commit. Consider the below
output:
As you can see from the above output, the changes made on the
repository have been reverted.
A merge in Git is a commit that has at least two parents. It brings together
multiple lines of development. In a work-flow where features are
developed in branches and then merged into a mainline, the merge
commits would typically have two parents.
I have made some changes to my file design2.css on the test and merge
it with test2. Consider the below output:
1. $ git log
The above command will display the commit history. Consider the below
output:
From the above output, copy your merging commit that you to want to
revert and run the below command:
The above command will revert the merging operation. Here, -m 1 is used
for the first parent as the mainline. Merge commit has multiple parents.
The revert needs additional information to decide which parent of the
merge shall be considered as the mainline. In such cases, the parameter -
m is used. Consider the below output:
From the above output, we can see that the previous merge has been
reverted.
Git Reset
The term reset stands for undoing changes. The git reset command is
used to reset the changes. The git reset command has three core forms of
invocation. These forms are as follows.
o Soft
o Mixed
o Hard
If we say in terms of Git, then Git is a tool that resets the current state of
HEAD to a specified state. It is a sophisticated and versatile tool for
undoing changes. It acts as a time machine for Git. You can jump up
and forth between the various commits. Each of these reset variations
affects specific trees that git uses to handle your file in its content.
The working directory lets you change the file, and you can stage into the
index. The staging area enables you to select what you want to put into
your next commit. A commit object is a cryptographically hashed version
of the content. It has some Metadata and points which are used to switch
on the previous commits.
Let's understand the --hard option with an example. Suppose I have added
a new file to my existing repository. To add a new file to the repository,
run the below command:
1. $ git add <file name>
1. $ git status
To check the status of the Head and previous commits, run the below
command:
1. $ git log
The above output is displaying the status of the repository after the hard
reset. We can see there is nothing to commit in my repository because all
the changes removed by the reset hard option to match the status of the
current Head with the previous one. So the file newfile2.txt has been
removed from the repository.
There is a safer way to reset the changes with the help of git stash.
Let's create a new file say newfile2.txt. Check the status of the
repository. To check the status of the repository, run the below command:
1. $ git status
It will display the untracked file from the staging area. Add it to the index.
To add a file into stage index, run the git add command as:
The above command will add the file to the staging index. Consider the
below output:
In the above output, I have added a newfile2.txt to my local repository.
Now, we will perform the reset mixed command on this repository. It will
operate as:
1. $ git reset
The above command will reset the status of the Head, and it will not
delete any data from the staging area to match the position of the Head.
Consider the below output:
From the above output, we can see that we have reset the position of the
Head by performing the git reset -mixed command. Also, we have
checked the status of the repository. As we can see that the status of the
repository has not been changed by this command. So it is clear that the
mixed-mode does not clear any data from the staging area.
The above command will move the HEAD to the particular commit. Let's
understand it with an example.
I have made changes in my file newfile2.txt and commit it. So, the current
position of Head is shifted on the latest commit. To check the status of
Head, run the below command:
1. $ git log
As you can see from the above output, the HEAD has been shifted to a
particular commit by git reset --soft mode.
o --soft
o --mixed
o --Hard
Git Rm
In Git, the term rm stands for remove. It is used to remove individual files
or a collection of files. The key function of git rm is to remove tracked files
from the Git index. Additionally, it can be used to remove files from both
the working directory and staging index.
The files being removed must be ideal for the branch to remove. No
updates to their contents can be staged in the index. Otherwise, the
removing process can be complex, and sometimes it will not happen. But
it can be done forcefully by -f option.
If we want to remove the file from our repository. Then it can be done by
the git rm command. Let's take a file say newfile.txt to test the rm
command. The git rm command will be operated as:
The above command will remove the file from the Git and repository. The
git rm command removes the file not only from the repository but also
from the staging area. If we check the status of the repository, then it will
show as deleted. Consider the below output:
In the above output, the file newfile.txt has been removed from the
version control system. So the repository and the status are shown as
deleted. If we use only the rm command, then it will not permanently
delete the file from the Git. It can be tracked in the staging area. Consider
the below output:
In the above output, the file newfile2.txt has been deleted. But when we
check the status of the repository, we can track the file in the staging
area. It means the newfile2 yet not deleted from the staging area, and it is
also available in the repository. We can get it back on the version control
system by committing it. To commit the file, first, add it to the index and
then commit it. To add this file in the index, run the below command:
The above command will add the file to the index. To commit it, run the
below command:
It will commit the file and make it available to the version control system.
Consider the below output:
In the above output, we are retrieving the file from the staging area to our
directory. The newfile2.txt is re-added to our repository.
Git Rm Cached
Sometimes you want to remove files from the Git but keep the files in
your local repository. In other words, you do not want to share your file on
Git. Git allows you to do so. The cached option is used in this case. It
specifies that the removal operation will only act on the staging index, not
on the repository. The git rm command with cached option will be uses as:
The above command will remove a file from the version control system.
The deleted file will remain in the repository. Somehow this command will
act as rm command. Let's understand it with an example.
The above command will delete the file from the version control system,
but still, it can be tracked in the repository. It also can be re-added on the
version control system. To check the file status, use the status command
as:
1. $ git status
The above command will reset the position of the head. So that it will get
the position of its just previous point. Consider the below output:
From the above output, we can see that the file has been successfully
reset to its previous position.
There is another way to undo the git rm command. We can also do it by
git checkout command. A checkout has the same effect and restores the
latest version of a file from HEAD. It will be used as follows:
1. $ git checkout.
Limits of Git Rm command
The git rm is operated only on the current branch. The removing process
is only applied to the working directory and staging index trees. It is not
persisted in the repository history until a new commit is created.
Git Cherry-pick
Cherry-picking in Git stands for applying some commit from one branch
into another branch. In case you made a mistake and committed a change
into the wrong branch, but do not want to merge the whole branch. You
can revert the commit and apply it on another branch.
Cherry-pick is a useful tool, but always it is not a good option. It can cause
duplicate commits and some other scenarios where other merges are
preferred instead of cherry-picking. It is a useful tool for a few situations.
It is in contrast with different ways such as merge and rebase command.
Merge and rebase can usually apply many commits in another branch.
Why Cherry-Pick
Suppose you are working with a team of developers on a medium to large-
sized project. Some changes proposed by another team member and you
want to apply some of them to your main project, not all. Since managing
the changes between several Git branches can become a complex task,
and you don't want to merge a whole branch into another branch. You
only need to pick one or two specific commits. To pick some changes into
your main project branch from other branches is called cherry-picking.
Git cherry-pick is helpful to apply the changes that are accidentally made
in the wrong branch. Suppose I want to make a commit in the master
branch, but by mistake, we made it in any other branch. See the below
commit.
In the above example, I want to make a commit for the master branch,
but accidentally I made it in the new branch. To make all the changes of
the new branch into the master branch, we will use the git pull, but for
this particular commit, we will use git cherry-pick command. See the
below output:
In the given output, I have used the git log command to check the commit
history. Copy the particular commit id that you want to make on the
master branch. Now switch to master branch and cherry-pick it there. See
the below output:
Syntax:
Output:
From the given output, you can see that I have pasted the commit id with
git cherry-pick command and made that commit into my master branch.
You can check it by git log command.
Scenario2: Made the changes proposes by another team member.
Usage of cherry-pick
o It is a handy tool for team collaboration.
o It is necessary in case of bug fixing because bugs are fixed and tested in
the development branch with their commits.
o It is mostly used in undoing changes and restoring lost commits.
o You can avoid useless conflicts by using git cherry-pick instead of other
options.
o It is a useful tool when a full branch merge is not possible due to
incompatible versions in the various branches.
o The git cherry-pick is used to access the changes introduced to a sub-
branch, without changing the branch.
Git log
The advantage of a version control system is that it records changes.
These records allow us to retrieve the data like commits, figuring out
bugs, updates. But, all of this history will be useless if we cannot navigate
it. At this point, we need the git log command.
Git log is a utility tool to review and read a history of everything that
happens to a repository. Multiple options can be used with a git log to
make history more specific.
Generally, the git log is a record of commits. A git log contains the
following data:
The solution to this problem is to press the q (Q for quit). It will quit you
from the situation and back you to the command line. Now, you can
perform any of the commands.
1. $ git log
The above command will display the last commits. Consider the below
output:
The above command is listing all the recent commits. Each commit
contains some unique sha-id, which is generated by the SHA algorithm. It
also includes the date, time, author, and some additional details.
We can perform some action like scrolling, jumping, move, and quit on the
command line. To scroll on the command line press k for moving up, j for
moving down, the spacebar for scrolling down by a full page to scroll up
by a page and q to quit from the command line.
<
So, usually we can say that the --oneline flag causes git log to display:
As we can see more precisely from the above output, every commit is
given only in one line with a seven-digit sha number and commit
message.
The above command will display the files that have been modified.
Consider the below output:
From the above output, we can see that all listed commits are
modifications in the repository.
Or
1. $ git log -p
Generally, we can say that the --patch flag is used to display:
o Modified files
o The location of the lines that you added or removed
o Specific changes that have been made.
The above output is displaying the modified files with the location of lines
that have been added or removed.
To make the output more specific, you can combine this command with --
oneline option. It will operate as follows:
By Amount:
We can limit the number of output commit by using git log command. It is
the most specific command. This command will remove the complexity if
you are interested in fewer commits.
To limit the git log's output, including the -<n> option. If we want only the
last three commit, then we can pass the argument -3 in the git log
command. Consider the below output:
As we can see from the above output, we can limit the output of git log.
The above command will display all the commits made after the given
date. Consider the below output:
We can also pass the applicable reference statement like "yesterday," "1
week ago", "21 days ago," and more. It will run as:
The above command will display the commits which have been made 21
days ago. Consider the below output:
We can also track the commits between two dates. To track the commits
that were created between two dates, pass a statement reference --
before and --after the date. Suppose, we want to track the commits
between "2019-11-01" and "2019-11-08". We will run the command as
follows:
The above command will display the commits made between the dates.
Consider the below output:
The above output is displaying the commits between the given period. We
can use --since and --until instead of --after and --before. Because they are
synonyms, respectively.
By Author:
The above command will display all the commits made by the given
author. Consider the below output:
From the above output, we can see that all the commits by the
author ImDwivedi1 are listed.
The above statement will display all commits whose author includes the
name, Stephen. The author's name doesn't need to be an exact match; it
just has the specified phrase.
As we know, the author's email is also involved with the author's name, so
that we can use the author's email as the pattern or exact search.
Suppose, we want to track the commits by the authors whose email
service is google. To do so, we can use wild cards as "@gmail.com."
Consider the below statement:
The above command will display the commits by authors as given in the
pattern. Consider the below output:
By Commit message:
To filter the commits by the commit message. We can use the grep
option, and it will work as the author option.
The above output is displaying all the commits that contain the word
commit in its commit message.
There are many other filtering options available like we can filter by file
name, content, and more.
Git Diff
Git diff is a command-line utility. It's a multiuse Git command. When it is
executed, it runs a diff function on Git data sources. These data sources
can be files, branches, commits, and more. It is used to show changes
between commits, commit, and working tree, etc.
However, we can also track the changes with the help of git log command
with option -p. The git log command will also work as a git diff command.
Let's understand different scenarios where we can utilize the git diff
command.
The usual use of git diff command that we can track the changes that
have not been staged.
Suppose we have edited the newfile1.txt file. Now, we want to track what
changes are not staged yet. Then we can do so from the git diff command.
Consider the below output:
From the above output, we can see that the changes made on newfile1.txt
are displayed by git diff command. As we have edited it as "changes are
made to understand the git diff command." So, the output is displaying
the changes with its content. The highlighted section of the above output
is the changes in the updated file. Now, we can decide whether we want
to stage this file like this or not by previewing the changes.
Scenerio2: Track the changes that have staged but not committed:
The git diff command allows us to track the changes that are staged but
not committed. We can track the changes in the staging area. To check
the already staged changes, use the --staged option along with git diff
command.
To check the untracked file, run the git status command as:
1. $ git status
The above command will display the untracked file from the repository.
Now, we will add it to the staging area. To add the file in the staging area,
run the git add command as:
The above command will add the file in the staging area. Consider the
below output:
Now, the file is added to the staging area, but it is not committed yet. So,
we can track the changes in the staging area also. To check the staged
changes, run the git diff command along with --staged option. It will be
used as:
The above command will display the changes of already staged files.
Consider the below output:
The given output is displaying the changes of newfile1.txt, which is
already staged.
Git, let us track the changes after committing a file. Suppose we have
committed a file for the repository and made some additional changes
after the commit. So we can track the file on this stage also.
Now, we have changed the newfile.txt file again as "Changes are made
after committing the file." To track the changes of this file, run the git diff
command with HEAD argument. It will run as follows:
The above command will display the changes in the terminal. Consider the
below output:
The above command is displaying the updates of the file newfile1.txt on
the highlighted section.
We can track the changes between two different commits. Git allows us to
track changes between two commits, whether it is the latest commit or
the old commit. But the required thing for this is that we must have a list
of commits so that we can compare. The usual command to list the
commits in the git log command. To display the recent commits, we can
run the command as:
1. $ git log
The above command will display all the commits of a specified file.
Consider the below output:
The above output is displaying all the commits of newfile1.txt. Suppose
we want to track the changes between
commits e553fc08cb and f1ddc7c9e7. The git diff command lets track
the changes between two commits. It will be commanded as:
The above command will display the changes between two commits.
Consider the below output:
The above output is displaying all the changes made
on newfile1.txt from commit e553fc08cb (most recent) to
commit f1ddc7c9e7 (previous).
We can track changes of the branch with the git status command, but few
more commands can explain it in detail. The git diff command is a widely
used tool to track the changes.
The above command will display the differences between branch 1 and
branch 2. So that you can decide whether you want to merge the branch
or not. Consider the below output:
The above output is displaying the differences between my repository
branches test and test2. The git diff command is giving a preview of both
branches. So, it will be helpful to perform any operation on branches.
Git Status
The git status command is used to display the state of the repository and
staging area. It allows us to see the tracked, untracked files and changes.
This command will not show any commit records or information.
Mostly, it is used to display the state between Git Add and Git
commit command. We can check whether the changes and files are
tracked or not.
1. $ git status
Output:
Let's track the file and will see the status after adding a file to the
repository. To track the file, run the add command. Consider the below
output:
From the above output, we can see that the status after staging the file is
showing as "changes to be committed".
Before committing blindly, we can check the status. This command will
help us to avoid the changes that we don't want to commit. Let's commit
it and then check the status. Consider the below output:
We can see that the current status after committing the file is clean as it
was before.
The above command will add the text to the specified file, now check the
status of the repository. Consider the below output:
The above command will delete the specified file from the repository.
Now, check the status of the repository. Consider the below output:
To create a branch on the remote repository, typically on a service like GitHub or GitLab,
you would need to push your local branch to the remote repository after creating it locally.
Here's the typical workflow to create and push a branch to the remote repository:
This sequence of commands creates a new branch locally, switches to it, makes changes,
commits those changes, and then pushes the branch to the remote repository. After pushing
the branch, it will be available on the remote repository for others to see and work with.
Git Branch
A branch is a version of the repository that diverges from the main
working project. It is a feature available in most modern version control
systems. A Git project can have more than one branch. These branches
are a pointer to a snapshot of your changes. When you want to add a new
feature or fix a bug, you spawn a new branch to summarize your changes.
So, it is complex to merge the unstable code with the main code base and
also facilitates you to clean up your future history before merging with the
main branch.
Git Master Branch
The master branch is a default branch in Git. It is instantiated when first
commit made on the project. When you make the first commit, you're
given a master branch to the starting commit point. When you start
making a commit, then master branch pointer automatically moves
forward. A repository can have only one master branch.
Master branch is the branch in which all the changes eventually get
merged back. It can be called as an official working version of your
project.
Operations on Branches
We can perform various operations on Git branches. The git branch
command allows you to create, list, rename and delete branches.
Many operations on branches are applied by git checkout and git merge
command. So, the git branch is tightly integrated with the git
checkout and git merge commands.
Create Branch
You can create a new branch with the help of the git branch command.
This command will be used as:
Syntax:
List Branch
You can List all of the available branches in your repository by using the
following command.
Either we can use git branch - list or git branch command to list the
available branches in the repository.
Syntax:
or
1. $ git branch
Output:
Here, both commands are listing the available branches in the repository.
The symbol * is representing currently active branch.
Delete Branch
You can delete the specified branch. It is a safe operation. In this
command, Git prevents you from deleting the branch if it has unmerged
changes. Below is the command to do this.
Syntax:
1. $ git branch -d<branch name>
Output:
This command will delete the existing branch B1 from the repository.
The git branch d command can be used in two formats. Another format
of this command is git branch D. The 'git branch D' command is used
to delete the specified branch.
Syntax:
Output:
Switch Branch
Git allows you to switch between the branches without making a commit.
You can switch between two branches with the git checkout command.
To switch between the branches, below command is used:
Syntax:
Output:
You can switch to the master branch from any other branch with the help
of below command.
Syntax:
Output:
As you can see in the above output, branches are switched from branch1
to master without making any commit.
Rename Branch
We can rename the branch with the help of the git branch command. To
rename a branch, use the below command:
Syntax:
Output:
As you can see in the above output, branch4 renamed as renamedB1.
Merge Branch
Git allows you to merge the other branch with the currently active branch.
You can merge two branches with the help of git merge command. Below
command is used to merge the branches:
Syntax:
Output:
In the above figure, there are two branches master and feature. We can
see that we made some commits in both functionality and master branch,
and merge them. It works as a pointer. It will find a common base commit
between branches. Once Git finds a shared base commit, it will create a
new "merge commit." It combines the changes of each queued merge
commit sequence.
The above command will merge the specified commit to the currently
active branch. You can also merge the specified commit to a specified
branch by passing in the branch name in <commit>. Let's see how to
commit to a currently active branch.
Copy the particular commit you want to merge on an active branch and
perform the merge operation. See the below output:
In the above output, we have merged the previous commit in the active
branch test2.
To merge a specified commit into master, first discover its commit id. Use
the log command to find the particular commit id.
1. $git log
Open new files, and you will notice that the new line that we have
committed to the test branch is now copied on the master branch.
Scenario 3: Git merge branch.
Git allows merging the whole branch in another branch. Suppose you have
made many changes on a branch and want to merge all of that at a time.
Git allows you to do so. See the below example:
In the given output, I have made changes in newfile1 on the test branch.
Now, I have committed this change in the test branch.
Now, switch to the desired branch you want to merge. In the given
example, I have switched to the master branch. Perform the below
command to merge the whole branch in the active branch.
As you can see from the given output, the whole commits of branch test2
have merged to branch master.
Update it in the local repository with the help of git add command.
Now commit the changes and update it with the remote repository. See
the below output:
Now, my remote repository will look like this:
It will show the status of the file like edited by whom and when.
Now, at the same time, user2 also update the index file as follows.
User2 has added and committed the changes in the local repository. But
when he tries to push it to remote server, it will throw errors. See the
below output:
In the above output, the server knows that the file is already updated and
not merged with other branches. So, the push request was rejected by the
remote server. It will throw an error message like [rejected] failed to
push some refs to <remote URL>. It will suggest you to pull the
repository first before the push. See the below command:
In the given output, git rebase command is used to pull the repository
from the remote URL. Here, it will show the error message like merge
conflict in <filename>.
Resolve Conflict:
To resolve the conflict, it is necessary to know whether the conflict occurs
and why it occurs. Git merge tool command is used to resolve the conflict.
The merge command is used as follows:
1. $ git mergetool
The above output shows the status of the conflicted file. To resolve the
conflict, enter in the insert mode by merely pressing I key and make
changes as you want. Press the Esc key, to come out from insert mode.
Type the: w! at the bottom of the editor to save and exit the changes. To
accept the changes, use the rebase command. It will be used as follows:
To see that which is the first edited text of the merge conflict in your file,
search the file attached with conflict marker <<<<<<<. You can see the
changes from the HEAD or base branch after the line <<<<<<<
HEAD in your text editor. Next, you can see the divider like =======. It
divides your changes from the changes in the other branch, followed by
>>>>>>> BRANCH-NAME. In the above example, user1 wrote "<h1>
Git is a version control</h1>" in the base or HEAD branch and user2
wrote "<h2> Git is a version control</h2>".
Decide whether you want to keep only your branch's changes or the other
branch's changes, or create a new change. Delete the conflict
markers <<<<<<<, =======, >>>>>>> and create final changes
you want to merge.
Git Rebase
Rebasing is a process to reapply commits on top of another base trip. It is
used to apply a sequence of commits from distinct branches into a final
commit. It is an alternative of git merge command. It is a linear process of
merging.
Suppose you have made three commits in your master branch and three
in your other branch named test. If you merge this, then it will merge all
commits in a time. But if you rebase it, then it will be merged in a linear
manner. Consider the below image:
The above image describes how git rebase works. The three commits of
the master branch are merged linearly with the commits of the test
branch.
How to Rebase
When you made some commits on a feature branch (test branch) and
some in the master branch. You can rebase any of these branches. Use
the git log command to track the changes (commit history). Checkout to
the desired branch you want to rebase. Now perform the rebase command
as follows:
Syntax:
If there are some conflicts in the branch, resolve them, and perform below
commands to continue changes:
1. $ git status
The above command is used to continue with the changes you made. If
you want to skip the change, you can skip as follows:
Output:
Now you are on the master branch. I have added the changes to my file,
says newfile.txt. The below command is used to add the file in the
repository.
Output:
Output:
As we can see in the log history, there is a new commit in the master
branch. If I want to rebase my test2 branch, what should I do? See the
below rebase branch scenario:
Rebase Branch
If we have many commits from distinct branches and want to merge it in
one. To do so, we have two choices either we can merge it or rebase it. It
is good to rebase your branch.
From the above example, we have committed to the master branch and
want to rebase it on the test2 branch. Let's see the below commands:
This command will switch you on the test2 branch from the master.
Output:
Now you are on the test2 branch. Hence, you can rebase the test2 branch
with the master branch. See the below command:
This command will rebase the test2 branch and will show as Applying:
new commit on test2 branch. Consider the below output:
Output:
Git Interactive Rebase
Git facilitates with Interactive Rebase; it is a potent tool that allows
various operations like edit, rewrite, reorder, and more on existing
commits. Interactive Rebase can only be operated on the currently
checked out branch. Therefore, set your local HEAD branch at the sidebar.
Git interactive rebase can be invoked with rebase command, just type -
i along with rebase command. Here 'i' stands for interactive. Syntax of
this command is given below:
Syntax:
1. $ git rebase -i
Output:
After the given output, it will open an editor with available options.
Consider the below output:
Output:
When we perform the git interactive rebase command, it will open your
default text editor with the above output.
o Pick
o Reword
o Edit
o Squash
o Fixup
o Exec
o Break
o Drop
o Label
o Reset
o Merge
The above options perform their specific tasks with git-rebase. Let's
understand each of these options in brief.
Pick (-p):
Pick stands here that the commit is included. Order of the commits
depends upon the order of the pick commands during rebase. If you do
not want to add a commit, you have to delete the entire line.
Reword (-r):
The reword is quite similar to pick command. The reword option paused
the rebase process and provides a chance to alter the commit message. It
does not affect any changes made by the commit.
Edit (-e):
The edit option allows for amending the commit. The amending means,
commits can be added or changed entirely. We can also make additional
commits before rebase continue command. It allows us to split a large
commit into the smaller commit; moreover, we can remove erroneous
changes made in a commit.
Squash (-s):
The squash option allows you to combine two or more commits into a
single commit. It also allows us to write a new commit message for
describing the changes.
Fixup (-f):
Exec (-x):
The exec option allows you to run arbitrary shell commands against a
commit.
Break (-b):
The break option stops the rebasing at just position. It will continue
rebasing later with 'git rebase --continue' command.
Drop (-d):
Label (-l):
The label option is used to mark the current head position with a name.
Reset (-t):
Merging creates a final commit at merging. Git rebase does not create
any commit at rebasing.
It creates a graphical history that might be a bit complex It creates a linear history
to understand. that can be easily
understood.
Merging can be performed on both public and private It is the wrong choice to
branches. use rebasing on public
branches.
Merging integrates the content of the feature branch with Rebasing of the master
the master branch. So, the master branch is changed, and branch may affect the
feature branch history remains consistence. feature branch.
Git Squash
In Git, the term squash is used to squash the previous commits into one. It
is not a command; instead, it is a keyword. The squash is an excellent
technique for group-specific changes before forwarding them to others.
You can merge several commits into a single commit with the compelling
interactive rebase command.
If you are a Git user, then you must have realized the importance of
squashing a commit. Especially if you are an open-source contributor,
then many times, you have to create a PR (pull request) with squashed
commit. You can also squash commits if you have already created a PR.
The squash is not any command; instead, it's one of many options
available to you under git interactive rebases. The squash allows us to
rewrite history. Suppose we have made many commits during the project
work, squashing all the commits into a large commit is the right choice
than pushing. Let's understand how to squash two commits.
Suppose we want to squash the last commits. To squash commits, run the
below command:
The above command will open your default text editor and will squash the
last three commits. The editor will open as follows:
From the above image, we can see previous commits shown at the top of
the editor. If we want to merge them into a single commit, then we have
to replace the word pick with the squash on the top of the editor. To
write on the editor, press 'i' button to enter in insert mode. After editing
the document, press the :wq to save and exit from the editor.
On pressing enter key, a new window of the text editor will be opened to
confirm the commit. We can edit the commit message on this screen.
When we exit the editor, it will show the description of updates. Consider
the below output:
The above output is listing the description of changes that have been
made on the repository. Now, the commits have been squashed. Check
the commit history for confirmation with the help of the git log. Consider
the below output:
Step 4: Push the squashed commit
Now, we can push this squashed commit on the remote server. To push
this squashed commit, run the below command:
Or
The above command will push the changes on the remote server. We can
check this commit on our remote repository. Consider the below image:
As you can see from the above image. A new commit has been added to
my remote repository.
Drawbacks of Squashing
There are no significant drawbacks of squashing. But we can consider
some facts that may affect the project. These facts are as follows:
Git Fetch
Git "fetch" Downloads commits, objects and refs from another repository.
It fetches branches and tags from one or more repositories. It holds
repositories along with the objects that are necessary to complete their
histories to keep updated remote-tracking branches.
The "git fetch"command
The "git fetch" command is used to pull the updates from remote-
tracking branches. Additionally, we can get the updates that have been
pushed to our remote branches to our local machines. As we know, a
branch is a variation of our repositories main code, so the remote-tracking
branches are branches that have been set up to pull and push from
remote repository.
Syntax:
Output:
In the above output, the complete repository has fetched from a remote
URL.
Syntax:
Output:
In the given output, the specific branch test has fetched from a remote
URL.
Syntax:
1. $ git fetch -all
Output:
In the above output, all the branches have fetched from the repository
Git-Example.
Syntax:
Output:
In the above output, new features of the remote repository have updated
to my local system. In this output, the branch test2 and its objects are
added to the local repository.
The git fetch can fetch from either a single named repository or URL or
from several repositories at once. It can be considered as the safe version
of the git pull commands.
The git fetch downloads the remote content but not update your local
repo's working state. When no remote server is specified, by default, it will
fetch the origin remote.
So basically,
Fetch downloads only new data from a Pull is used to update your current HEAD
remote repository. branch with the latest changes from the
remote server.
Fetch is used to get a new view of all the Pull downloads new data and directly
things that happened in a remote integrates it into your current working copy
repository. files.
Fetch never manipulates or spoils data. Pull downloads the data and integrates it
with the current working file.
It protects your code from merge In git pull, there are more chances to create
conflict. the merge conflict.
It is better to use git fetch command It is not an excellent choice to use git pull if
with git merge command on a pulled you already pulled any repository.
repository.
Git Pull / Pull Request
The term pull is used to receive data from GitHub. It fetches and merges
changes from the remote server to your working directory. The git pull
command is used to pull a repository.
Pull request is a process for a developer to notify team members that they
have completed a feature. Once their feature branch is ready, the
developer files a pull request via their remote server account. Pull request
announces all the team members that they need to review the code and
merge it into the master branch.
The below figure demonstrates how pull acts between different locations
and how it is similar or dissimilar to other related commands.
The "git pull" command
The pull command is used to access the changes (commits)from a remote
repository to the local repository. It updates the local branches with the
remote-tracking branches. Remote tracking branches are branches that
have been set up to push and pull from the remote repository. Generally,
it is a collection of the fetch and merges command. First, it fetches the
changes from remote and combined them with the local repository.
Syntax:
In which:
1. https://github.com/ImDwivedi1/GitExample2.git
Copy the highlighted URL. This URL is used to Clone the repository.
<Refspec>: A ref is referred to commit, for example, head (branches),
tags, and remote branches. You can check head, tags, and remote
repository in .git/ref directory on your local repository. Refspec specifies
and updates the refs.
It is essential to understand how it works and how to use it. Let's take an
example to understand how it works and how to use it. Suppose I have
added a new file say design2.css in my remote repository of project
GitExample2.
To create the file first, go to create a file option given on repository sub-
functions. After that, select the file name and edit the file as you want.
Consider the below image.
To pull these changes in your local repository, perform the git pull
operation on your cloned repository. There are many specific options
available for pull command. Let's have a look at some of its usage.
Syntax:
1. $ git pull
Output:
In the given output, the newly updated objects of the repository are
fetched through the git pull command. It is the default version of the git
pull command. It will update the newly created file design2.css file and
related object in the local repository. See the below image.
As you can see in the above output, the design2.css file is added to the
local repository. The git pull command is equivalent to git fetch origin
head and git merge head. The head is referred to as the ref of the
current branch.
Syntax:
Output:
In the above output, the remote branch edited has copied.
If you have updated any file locally and other team members updated it
on the remote. So, when will you fetch the repository, it may create a
conflict.
We can say force pull is used for overwriting the files. If we want to
discard all the changes in the local repository, then we can overwrite it by
influentially pulling it. Consider the below process to force pull a
repository:
Step1: Use the git fetch command to download the latest updates from
the remote without merging or rebasing.
Step2: Use the git reset command to reset the master branch with
updates that you fetched from remote. The hard option is used to
forcefully change all the files in the local repository with a remote
repository.
In the above syntax, the term origin stands for the repository location
where the remote repository situated. Master is considered as the main
branch of the project.
It will overwrite the existing data of the local repository with a remote
repository.
You can check the remote location of your repository. To check the
remote location of the repository, use the below command:
1. $ git remote -v
The given command will result in a remote location like this:
The output displays fetch and push both locations. Consider the below
image:
Once you've created a pull request, you can push commits from your
branch to add them to your existing pull request.
Now, the pull request is created by you. People can see this request. They
can merge this request with the other branches by selecting a merged
pull request.
Git Push
The push term refers to upload local repository content to a remote
repository. Pushing is an act of transfer commits from your local
repository to a remote repository. Pushing is capable of overwriting
changes; caution should be taken when pushing.
Moreover, we can say the push updates the remote refs with local refs.
Every time you push into the repository, it is updated with some
interesting changes that you made. If we do not specify the location of a
repository, then it will push to default location at origin master.
The "git push" command is used to push into the repository. The push
command can be considered as a tool to transfer commits between local
and remote repositories. The basic syntax is given below:
--all: The word "all" stands for all branches. It pushes all branches.
Generally, the term origin stands for the remote repository, and master
is considered as the main branch. So, the entire statement "git push
origin master" pushed the local content on the master branch of the
remote location.
Syntax:
1. $ git status
It shows the status of the untracked image abc.jpg. Now, add the image
and commit the changes as:
The image is wholly tracked in the local repository. Now, we can push it to
origin master as:
Output:
The file abc.jpg is successfully pushed to the origin master. We can track
it on the remote location. I have pushed these changes to my GitHub
account. I can track it there in my repository. Consider the below image:
In the above output, the pushed file abc.jpg is uploaded on my GitHub
account's master branch repository.
Or
We can also omit the branch in this command. The command will be
executed as:
We can omit both the remote and branch. When the remote and the
branch both are omitted, the default behavior is determined
by push.default setting of git config. The command will be executed as:
1. $ git push -f
How to Safe Force Push Repository:
There are several consequences of force pushing a repository like it may
replace the work you want to keep. Force pushing with a lease option is
capable of making fail to push if there are new commits on the remote
that you didn't expect. If we say in terms of git, then we can say it will
make it fail if remote contains untracked commit. It can be executed as:
1. $ git push -v
Or
If we compare the above output with the default git option, we can see
that git verbose gives descriptive output.
Syntax:
Output:
In the above output, the git push origin command is used with -delete
option to delete a remote branch. I have deleted my remote
branch edited from the repository. Consider the below image:
The above image displays the list of active branches after deleting
command. Here, you can see that the branch edited has removed from
the repository.
1. A .git subdirectory with all the Git associated revision history of your repo.
2. A working tree, or find out copies of your project files.
13) What is the difference between git pull and git fetch?
Git pull command pulls innovation or commits from a specific branch from
your central repository and updates your object branch in your local
repository.
Git fetch is also used for the same objective, but it works in a slightly
different method. When you behave a git fetch, it pulls all new commits
from the desired branch and saves it in a new branch in your local
repository. If you need to reflect these changes in your target branch, git
fetch should be followed with a git merge. Your target branch will only be
restored after combining the target branch and fetched branch. To make
it simple for you, remember the equation below:
1. The correct activity will devastate the express that was recently saved in a
commit. If only the commit message gets changed, that's not a problem.
But if the contents are being modified, chances of excluding something
important remains more.
2. Abusing "git commit- amends" can cause a small commit to increase and
acquire inappropriate changes.
23) In Git, how would you return a commit that has just
been pushed and made open?
There can be two answers to this question and ensure that you
incorporate both because any of the below choices can be utilized relying
upon the circumstance:
Remove or fix the bad document in another commit and push it to the
remote repository. This is a unique approach to correct a mistake. Once
you have necessary changes to the record, commit it to the remote
repository for that I will utilize
Make another commit that fixes all changes that were made in the terrible
commit. to do this, I will utilize a command
Task Branching
Release Branching
Once the create branch has procured enough features for a discharge,
you can clone that branch to frame a Release branch. Making this branch
begins the following discharge cycle so that no new features can be
included after this point, just bug fixes, documentation age, and other
release oriented assignments ought to go in this branch. When it is
prepared to deliver, the release gets converged into master and labeled
with a form number. Likewise, it should be converged once again into
creating a branch, which may have advanced since the release was
started.
To know whether a branch has been merged into master or not you can
utilize the below commands:
git branch - merged It records the branches that have been merged into
the present branch.
git branch - no merged It records the branches that have not been
merged.
27) How might you fix a messed up submit?
To fix any messed up commit, you will utilize the order "git commit?
correct." By running this direction, you can set the wrecked commit
message in the editor.
o Pikacode
o Visual Studio Online
o GitHub
o GitEnterprise
o SourceForge.net
a. Git Cola
b. Smart git
c. Git-g
d. Git GUI
e. Giggle
f. qGit