July 2018
M T W T F S S
« Jun    
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories

WordPress Quotes

Life shrinks or expands in proportion to one's courage.
Anais Nin

Recent Comments

July 2018
M T W T F S S
« Jun    
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Short Cuts

2012 SERVER (64)
2016 windows (9)
AIX (13)
Amazon (30)
Ansibile (18)
Apache (125)
Asterisk (2)
cassandra (2)
Centos (208)
Centos RHEL 7 (254)
chef (3)
cloud (2)
cluster (3)
Coherence (1)
DB2 (5)
DISK (25)
DNS (9)
Docker (24)
Eassy (11)
EXCHANGE (3)
Fedora (6)
ftp (5)
GIT (3)
GOD (2)
Grub (1)
Hacking (10)
Hadoop (6)
horoscope (23)
Hyper-V (10)
IIS (15)
IPTABLES (15)
JAVA (6)
JBOSS (32)
jenkins (1)
Kubernetes (2)
Ldap (5)
Linux (189)
Linux Commands (167)
Load balancer (5)
mariadb (14)
Mongodb (4)
MQ Server (21)
MYSQL (82)
Nagios (5)
NaturalOil (13)
Nginx (29)
Ngix (1)
openldap (1)
Openstack (6)
Oracle (34)
Perl (3)
Postfix (19)
Postgresql (1)
PowerShell (2)
Python (3)
qmail (36)
Redis (12)
RHCE (28)
SCALEIO (1)
Security on Centos (29)
SFTP (1)
Shell (64)
Solaris (58)
Sql Server 2012 (4)
squid (3)
SSH (10)
SSL (14)
Storage (1)
swap (3)
TIPS on Linux (28)
tomcat (59)
Uncategorized (29)
Veritas (2)
vfabric (1)
VMware (28)
Weblogic (38)
Websphere (71)
Windows (19)
Windows Software (2)
wordpress (1)
ZIMBRA (17)

WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.

Who's Online

31 visitors online now
8 guests, 23 bots, 0 members

Hit Counter provided by dental implants orange county

Git Fundamentals

Git Fundamentals

 

Topics To be Covered

  1. Installation

2. Setup

3. Creating a Project

4. Checking the status of the repository

5. Making changes

6. Staging the changes

7. Staging and committing

8. Commiting the changes

9. Changes, not files

10. History

11. Aliases

12. Getting older versions

13. Tagging versions

14. Discarding local changes (before staging)

15. Cancel Staged changes (before committing)

16. Cancelling commits


1. Installing Git

Installing on Linux

If you want to install the basic Git tools on Linux via a binary installer, you can generally do so through the basic package-management tool that comes with your distribution. If you’re on Fedora for example, you can use yum:

$ sudo yum install git-all

If you’re on a Debian-based distribution like Ubuntu, try apt-get:

$ sudo apt-get install git-all

For more options, there are instructions for installing on several different Unix flavors on the Git website, at http://git-scm.com/download/linux.

Installing on Mac

There are several ways to install Git on a Mac. The easiest is probably to install the Xcode Command Line Tools. On Mavericks (10.9) or above you can do this simply by trying to run git from the Terminal the very first time. If you don’t have it installed already, it will prompt you to install it.

If you want a more up to date version, you can also install it via a binary installer. An OSX Git installer is maintained and available for download at the Git website, at https://sourceforge.net/projects/git-osx-installer/

You can also install it as part of the GitHub for Mac install. Their GUI Git tool has an option to install command line tools as well. You can download that tool from the GitHub for Mac website, athttp://mac.github.com.

Installing on Windows

There are also a few ways to install Git on Windows. The most official build is available for download on the Git website. Just go to https://git-for-windows.github.io/

Another easy way to get Git installed is by installing GitHub for Windows. The installer includes a command line version of Git as well as the GUI. It also works well with Powershell, and sets up solid credential caching and sane CRLF settings. We’ll learn more about those things a little later, but suffice it to say they’re things you want. You can download this from the GitHub for Windows website, at http://windows.github.com

2. One time Git Environment Setup

Now that you have Git on your system, you’ll want to do a few things to customize your Git environment. You should have to do these things only once; they’ll stick around between upgrades. You can also change them at any time by running through the commands again.

Git comes with a tool called git config that lets you get and set configuration variables that control all aspects of how Git looks and operates. These variables can be stored in three different places:

$ git config?—?global user.name “Aravind G V”

$ git config?—?global user.email aravind_gv@intuit.com

$ git config?—?global core.editor “edit -w”

3. Creating a Project

Create a new repository

Create a New Folder or Do in Existing Folder if you want to add it to vcs

mkdir training
cd training
touch hello.txt

Create a repository

So you have a directory that contains one file. Run the git init in order to create a git repo from that directory.

RUN:

git init

RESULT:

$ git init
Initialized empty Git repository in /Users/agv/git-training

This creates a new subdirectory named .git that contains all of your necessary repository files?—?a Git repository skeleton. At this point, nothing in your project is tracked yet

Add the page to the repository

Now let’s add the “Hello, World” page to the repository.

RUN:

git add hello.txt
git commit -m "First Commit"

You will see …

RESULT:

$ git add hello.txt
$ git commit -m "First Commit"
[master (root-commit) 2fc4372] First Commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hello.txt

Checking the status of the repository

Use the git status command, to check the current state of the repository.

RUN:

git status

RESULT:

$ git status
# On branch master
nothing to commit (working directory clean)

The command checks the status and reports that there’s nothing to commit, meaning the repository stores the current state of the working directory, and there are no changes to record.

We will use the git status, to keep monitoring the states of both the working directory and the repository.

Making changes

Let’s add some thing in text file

vi hello.txt
Test status

Checking the status

Check the working directory’s status.

RUN:

git status

You will see …

RESULT:

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
modified:   hello.txt
no changes added to commit (use "git add" and/or "git commit -a")

The first important aspect here is that git knows hello.txt file has been changed, but these changes are not yet committed to the repository.

Another aspect is that the status message hints about what to do next. If you want to add these changes to the repository, use git add. To undo the changes use git checkout.

6. Staging the changes

Adding changes

Now command git to stage changes. Check the status

RUN:

git add hello.txt
git status

You will see …

RESULT:

$ git add hello.txt
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   hello.txt
#

Changes to the hello.txt have been staged. This means that git knows about the change, but it is not permanent in the repository. The next commit will include the changes staged.

Should you decide not to commit the change, the status command will remind you that you can use the git reset command to unstage these changes.

Staging and committing

A staging step in git allows you to continue making changes to the working directory, and when you decide you wanna interact with version control, it allows you to record changes in small commits.

Suppose you have edited three files (a. html, b. html, and c. html). After that you need to commit all the changes so that the changes to a. html and b. html were a single commit, while the changes to c. html were not logically associated with the first two files and were done in a separate commit.

In theory you can do the following:

touch a.html b.html c.html
git add a.html
git add b.html
git commit -m "Changes for a and b"
git add c.html
git commit -m "Unrelated change to c"

Separating staging and committing, you get the chance to easily customize what goes into a commit

8. Committing the changes

Well, enough about staging. Let’s commit the staged changes to the repository.

When you previously used git commit for committing the first hello.html version to the repository, you included the -m flag that gives a comment on the command line. The commit command allows interactively editing comments for the commit. And now, let’s see how it works.

If you omit the -m flag from the command line, git will pop you into the editor of your choice from the list (in order of priority):

  • GIT_EDITOR environment variable
  • core.editor configuration setting
  • VISUAL environment variable
  • EDITOR environment variable

I have the EDITOR variable set to emacsclient (available for Linux and Mac).

Let us commit now and check the status.

RUN:

git commit

You will see the following in your editor:

RESULT:

|
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   hello.html
#

On the first line, enter the comment: “Added hi tag”. Save the file and exit the editor (to do it in default editor, press ESC and then type:wq and hit Enter). You should see …

RESULT:

git commit
Waiting for Emacs...
[master 569aa96] Added h1 tag
 1 files changed, 1 insertions(+), 1 deletions(-)

“Waiting for Emacs…” is obtained from the emacsclient program sending the file to a running emacs program and waiting for it to be closed. The rest of the data is the standard commit messages.

02Checking the status

At the end let us check the status.

RUN:

git status

You will see …

RESULT:

$ git status
# On branch master
nothing to commit (working directory clean)

The working directory is clean, you can continue working

Changes, not files

Understanding that git works with the changes, not the files.

Most version control systems work with files. You add the file to source control and the system tracks changes from that moment on.

Git concentrates on the changes to a file, not the file itself. A git add file command does not tell git to add the file to the repository, but to note the current state of the file for it to be commited later.

We will try to investigate the difference in this lesson.

First Change:

FILE: HELLO.txt

First Change

Add this change

Now add this change to the git staging.

RUN:

git add hello.html

Second change:

FILE: HELLO.txt

Second Change

Check the current status

RUN:

git status

You will see …

RESULT:

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   hello.txt
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   hello.txt
#

Please note that hello.txt is listed in the status twice. The first change (the addition of default tags) is staged and ready for a commit. The second change (adding HTML headers) is unstaged. If you were making a commit right now, headers would not have been saved to the repository.

Let’s check.

05Commit

Commit the staged changes (default values), then check the status one more time.

RUN:

git commit -m "Added second "
git status

You will see …

RESULT:

$ git commit -m "Added second"
[master 8c32287] Added standard HTML page tags
 1 files changed, 3 insertions(+), 1 deletions(-)
$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   hello.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

The status command suggests that hello.html has unrecorded changes, but is no longer in the buffer zone.

Adding the second change

Add the second change to the staging area, after that run the git status command.

RUN:

git add .
git status

Note: The current directory (‘.’) will be our file to add. This is the most convenient way to add all the changes to the files of the current directory and its folders. But since it adds everything, it is a good idea to check the status prior to doing an add ., to make sure you don’t add any file that should not be added.

I wanted you to see the “add .” trick, and we will continue adding explicit files later on just in case.

You will see …

RESULT:

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   hello.html
#

The second change has been staged and is ready for a commit.

Commit the second change

RUN:

git commit -m "Added second change"

History

To learn to view the project’s history.

Getting a list of changes made is a function of the git log command.

RUN:

git log

You will see …

RESULT:

commit ef44671e9b7aef27027f9d2b438e366d3133102b
Author: Aravind G V <aravind_g@?—?— >
Date: Wed Apr 6 13:09:55 2016 +0530

XIADMIN windows scripts

commit 70d87e94e7937f8a6ce89d4cd6001d99abfb4e77
Author: Neelam Malik <Neelam_Malik@?—?>
Date: Wed Apr 6 11:21:54 2016 +0530

added app_stop

commit 77ac1140018714a6b26c27535c6279eff40c87e9
Author: Neelam Malik <aravind_gv@?—?>
Date: Wed Apr 6 11:21:32 2016 +0530

added app_status

commit 36fb85091deabfe3e590a101aa1a1aca1adfa8c2
Author: Neelam Malik ?@?—?.om>
Date: Wed Apr 6 11:21:04 2016 +0530

added app_start

added app_stop

commit e4be69a36c3a37203e2b539f97eb77b3f253fe99
Author: Neelam Malik <aravind_gv@?—?>
Date: Wed Apr 6 11:18:42 2016 +0530

One line history

You fully control over what the log shows. I like the single line format:

RUN:

git log --pretty=oneline

You will see …

RESULT:

$ git log --pretty=oneline
fa3c1411aa09441695a9e645d4371e8d749da1dc Added HTML header
8c3228730ed03116815a5cc682e8105e7d981928 Added standard HTML page tags
43628f779cb333dd30d78186499f93638107f70b Added h1 tag
911e8c91caeab8d30ad16d56746cbd6eef72dc4c First Commit

Controlling the display of entries

There are many options to choose which entries appear in the log. Play around with the following parameters:

git log --pretty=oneline --max-count=2
git log --pretty=oneline --since='5 minutes ago'
git log --pretty=oneline --until='5 minutes ago'
git log --pretty=oneline --author=<your name>
git log --pretty=oneline --all

Details are provided in the git-log instruction.

Getting fancy

This is what I use to review the changes made within the last week. I will add?—?author=alex if I want to see only the changes made by me.

git log --all --pretty=format:"%h %cd %s (%an)" --since='7 days ago'

The ultimate format of the log

Over time, I found the following log format to be the most suitable.

RUN:

git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short

It looks like this:

RESULT:

$ git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short
* fa3c141 2011-03-09 | Added HTML header (HEAD, master) [Alexander Shvets]
* 8c32287 2011-03-09 | Added standard HTML page tags [Alexander Shvets]
* 43628f7 2011-03-09 | Added h1 tag [Alexander Shvets]
* 911e8c9 2011-03-09 | First Commit [Alexander Shvets]

Let’s look at it in detail:

  • — pretty=”…” defines the output format.
  • %h is the abbreviated hash of the commit
  • %d commit decorations (e.g. branch heads or tags)
  • %ad is the commit date
  • %s is the comment
  • %an is the name of the author
  • — graph tells git to display the commit tree in the form of an ASCII graph layout
  • — date=short keeps the date format short and nice

So, every time you want to see a log, you’ll have to do a lot of typing. Fortunately, we will find out about the git aliases in the next lesson.

Other tools

Both gitx (for Mac) and gitk (for any platform) can help to explore log history.

Aliases

Command aliases (optional)

Common aliases

For Windows users:

RUN:

git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
git config --global alias.type 'cat-file -t'
git config --global alias.dump 'cat-file -p'

Also, for users of Unix/Mac:

git status, git add, git commit, and git checkout are common commands so it is a good idea to have abbreviations for them.

Add the following to the .gitconfig file in your $HOME directory.

.GITCONFIG

[alias]
  co = checkout
  ci = commit
  st = status
  br = branch
  hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
  type = cat-file -t
  dump = cat-file -p

We’ve already talked about commit and status commands. In the previous lesson we covered the log command and will get to know the checkout command very soon. The most important thing to learn from this lesson is that you can type git st wherever you had to typegit status. Best of all, the git hist command will help you avoid the really long log command.

Go ahead and try using the new commands.

If your shell supports aliases, or shortcuts, you can add aliases on this level, too. I use:

alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias go='git checkout '
alias gk='gitk --all&'
alias gx='gitx --all'
alias got='git '
alias get='git '

The go abbreviation for git checkout is very useful, allowing me to type:

go <branch>

to checkout a particular branch.

Also, I often mistype git as get or got so I created aliases for them too.

Getting older versions

To learn how to checkout any previous snapshot into the working directory.

Going back in history is very simple. The checkout command can copy any snapshot from the repo to the working directory.

Getting hashes for the previous versions

RUN:

git hist

Note: Do not forget to define hist in your .gitconfig file? If you do not remember how, review the lesson on aliases.

RESULT:

$ git hist
* 7358571 2016-06-23 | Second Test (HEAD -> master) [Aravind G V]
* 2fc4372 2016-06-23 | First Commit [Aravind G V]

Check the log data and find the hash for the first commit. You will find it in the last line of the git hist data. Use the code (its first 7 chars are enough) in the command below. After that check the contents of the hello.html file.

RUN:

git checkout <hash>
cat hello.txt

Note: Many commands depend on the hash values in the repository. Since my hash values will be different from yours, substitute in the appropriate hash value for your repository everytime you see <hash> or <treehash> in the command.

You will see …

RESULT:

$ git checkout 911e8c9
Note: checking out '911e8c9'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
  git checkout -b new_branch_name
HEAD is now at 911e8c9... First Commit
$ cat hello.txt
Hello, World

The checkout command output totally clarifies the situation. Older git versions will complain about not being on a local branch. But you don’t need to worry about that right now.

Note that the content of the hello.html file is the default content.

Returning to the latest version in the master branch

RUN:

git checkout master
cat hello.txt

‘master’ is the name of the default branch. By checking out a branch by name, you go to its latest version.

Tagging versions

To learn how to tag commits for future references

Let’s call the current version of the hello program version 1 (v1).

Creating a tag of the first

RUN:

git tag v1

Now, the current version of the page is referred to as v1.

Tags for previous versions

Let’s tag the version prior to the current version with the name v1-beta. First of all we will checkout the previous version. Instead of looking up the hash, we are going to use the ^ notation indicating “the parent of v1”.

If the v1^ notation causes troubles, try using v1~1, referencing the same version. This notation means “the first version prior to v1”.

RUN:

git checkout v1^
cat hello.html

RUN:

$ git checkout v1^
Note: checking out 'v1^'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
  git checkout -b new_branch_name
HEAD is now at 8c32287... Added standard HTML page tags
$ cat hello.html

.

RUN:

git tag v1-beta

Check out by the tag name

Now try to checkout between the two tagged versions.

RUN:

git checkout v1
git checkout v1-beta

RESULT:

$ git checkout v1
Previous HEAD position was 8c32287... Added standard HTML page tags
HEAD is now at fa3c141... Added HTML header
$ git checkout v1-beta
Previous HEAD position was fa3c141... Added HTML header
HEAD is now at 8c32287... Added standard HTML page tags

04Viewing tags with the tag command

You can see the available tags using the git tag command.

RUN:

git tag

RESULT:

$ git tag
v1
v1-beta

Viewing tags in logs

You can also check for tags in the log.

RUN:

git hist master --all

RESULT:

$ git hist master --all
* fa3c141 2011-03-09 | Added HTML header (v1, master) [Alexander Shvets]
* 8c32287 2011-03-09 | Added standard HTML page tags (HEAD, v1-beta) [Alexander Shvets]
* 43628f7 2011-03-09 | Added h1 tag [Alexander Shvets]
* 911e8c9 2011-03-09 | First Commit [Alexander Shvets]

You can see tags (v1 and v1-beta) listed in the log together with the name of the branch (master). The HEAD shows the commit you checked out (currently v1-beta).

Discarding local changes (before staging)

To learn how to discard the working directory changes

Checking out the Master branch

Make sure you are on the lastest commit in the master brach before you continue.

RUN:

git checkout master

Change hello.txt

It happens that you modify a file in your local working directory and sometimes wish just to discard the committed changes. Here is when the checkout command will help you.

Make changes to the hello.html file in the form of an unwanted comment.

FILE: HELLO.txt

test-1
test-2
no need of this line

Check the status

First of all, check the working directory’s status.

RUN:

git status

Undoing the changes in the working directory

Use the checkout command in order to checkout the repository’s version of the hello.html file.

RUN:

git checkout hello.txt
git status
cat hello.txt

Cancel Staged changes (before committing)

To learn how to undo changes that have been staged

Edit file and stage changes

Make changes to the hello.html file in the form of an unwanted comment

FILE: HELLO.txt

test-1
test-2
no need of this line

Stage the modified file.

RUN:

git add hello.txt

Check the status

Check the status of unwanted changes .

RUN:

git status

RESULT:

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   hello.txt
#

Status shows that the change has been staged and is ready to commit.

Reset the buffer zone

Fortunately, the displayed status shows us exactly what we should do to cancel staged changes.

RUN:

git reset HEAD hello.txt

RESULT:

$ git reset HEAD hello.html
Unstaged changes after reset:
M   hello.html

The reset command resets the buffer zone to HEAD. This clears the buffer zone from the changes that we have just staged.

The reset command (default) does not change the working directory. Therefore, the working directory still contains unwanted comments. We can use the checkout command from the previous tutorial to remove unwanted changes from working directory.

Switch to commit version

RUN:

git checkout hello.html
git status

RESULT:

$ git status
# On branch master
nothing to commit (working directory clean)

Our working directory is clean again.

Removing a commit from a branch

To learn to delete the branch’s latest commits

Revert is a powerful command of the previous section that allows you to cancel any commits to the repository. However, both original and cancelled commits are seen in the history of the branch (when using git log command).

Often after commit is already made, we realize that it was a mistake. It would be nice to have undo option command allowing deleting incorrect commit immediately. This command would prevent the appearance of unwanted commit in the git log history.

The reset command

We have already used reset command to match the buffer zone and the selected commit (HEAD commit was used in the previous lesson).

When a commit reference is given (ie, a branch, hash, or tag name), the reset command will…

  1. Overwrite the current branch so it will point to the correct commit
  2. Optionally reset the buffer zone so it will comply with the specified commit
  3. Optionally reset the working directory so it will match the specified commit

02Check our history

Let us do a quick scan of our commit history.

RUN:

git hist

We see that the last two commits in this branch are “Oops” and “Revert Oops”. Let us remove them with reset command.

Mark this branch first

Let us mark the last commit with tag, so you can find it after removing commits.

RUN:

git tag oops

Reset commit to previous Oops

At the history log (see above), the commit tagged «v1» is committing previous wrong commit. Let us reset the branch to that point. As the branch has a tag, we can use the tag name in the reset command (if it does not have a tag, we can use the hash value).

RUN:

git reset --hard v1
git hist

RESULT:

$ git reset --hard v1
HEAD is now at fa3c141 Added HTML header

Our master branch is pointing at commit v1 and “Revert Oops” and “Oops” commits no longer exist in the branch. The?—?hard parameter points out that the working directory must be updated to reflect the new branch head.

Nothing is ever lost

What happens to the wrong commits? They are still in the repository. Actually, we still can refer to them. At the beginning of the lesson, we created «oops» tag for the canceled commit. Let us take a look at all commits.

RUN:

git hist --all

We can see that the wrong commits haven’t gone. They are not listed in the master branch anymore but still remain in the repository. They would be still in the repository if we did not tag them, but then we could reference them only by their hash names. Unreferenced commits remain in the repository until the garbage collection software is run by system.

Reset dangers

Resets on local branches are usually harmless. The consequences of any “accident” can be reverted by using the proper commit.

Though, other users sharing the branch can be confused if the branch is shared on remote repositories.

Removing the tag

Oops tag has performed it’s function. Let us remove that tag and permit the garbage collector to delete referenced commit.

RUN:

git tag -d oops
git hist --all

Changing commits

Goals

  • To learn how to modify an already existing commit

Change the page and commit

Post an author comment on the page.

git add hello.html
git commit -m "Add an author comment"

Change the previous commit

We do not want to create another commit for some update. Let us change the previous commit and add an update.

git add hello.html
git commit --amend -m "Add an author/email comment"

View history

git hist

Moving files

Now we will create the structure of our repository. Let us move the page in the lib directory

RUN:

mkdir lib
git mv hello.html lib
git status

Moving files with git, we notify git about two things

  1. The hello.html file was deleted.
  2. The lib/hello.html file was created.

Both facts are staged immediately and ready for a commit. Git status command reports the file has been moved.

One more way to move files

A positive fact about git is that you don’t need to remember about version control to the moment when you need to commit code. What could happen if we were using the operating system command line instead of the git command to move files?

The following set of commands turned out to be identical to our last actions. It requires more work with same result.

We can do:

mkdir lib
mv hello.txt lib
git add lib/hello.txt
git rm hello.txt

Commit new directory

Let us commit this movement.

RUN:

git commit -m "Moved hello.html to lib"

Creating a Branch

Goals

  • To learn how to create a local branch in the repository

It is time to make our hello world more expressive. Since it may take some time, it is best to move these changes into a new branch to isolate them from master branch changes.

Create a branch

Let us name our new branch «testbranch».

RUN:

git checkout -b testbranch
git status

Note: git checkout -b <branch name> is the shortcuts for git branch <branch name> followed by agit checkout <branch name>.

Note that the git status command reports that you are in the testbranch branch.Make some changes and stage the changes.

Navigating Branches

Now your project has two branches:

RUN:

git hist --all

Switching to the Master branch

To switch between branches simply use the git checkout command.

RUN:

git checkout master
cat lib/hello.txt

Changes to master branch

To learn how to work with several branches with different (sometimes conflicting) changes.

At the time you are changing the style branch, someone decided to change the master branch. He added a README file.

Commit changes of README file in the master branch.

RUN:

git checkout master
git add README
git commit -m "Added README"

View the different branches

Now we have a repository with two different branches. To view branches and their differences use log command as follows.

RUN:

git hist --all

We have opportunity to see?—?graph of git hist in action. Adding the?—?graph option to git log causes the construction of a commit tree with the help of simple ASCII characters. We see both branches (style and master) and that the current branch is master HEAD. The Added index.html branch goes prior to both branches.

The?—?all flag guarantees that we see all the branches. By default, only the current branch is displayed.

Merging to a single branch

Merging brings changes from two branches into one. Let us go back to the testbranch branch and merge it with master.

RUN:

git checkout style
git merge master
git hist --all

Creating and Resolving conflict

Return to the master and create conflict

Return to the master branch and make some changes:

git checkout master
vi lib/hello.txt
Test Status
Creating conflict
git add lib/hello.html
git commit -m 'Life is great!'

(Warning: make sure you’ve used single-quotes to avoid problems with bash and ! character)

View branches

git hist --all

Merge the master branch with testbranch

Let us go back to the testbranch branch and merge it with a new master branch.

git checkout testbranch
git merge master

If you open the lib/hello.html you will see conflict errors

Resolution of the conflict

You need to resolve the conflict manually. Make changes to lib/hello.html to achieve the following result.

Now that you’ve got a good handle on Git, let’s look at GitHub. I’m keen not to overwhelm you, so I’ve made an annotated screenshot of a GitHub project, so that you can quickly become familiar with the most common features. Yes, GitHub is more than simply a project repository, but that’s where you’re likely going to spend most of your time on the site.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

Blue Captcha Image
Refresh

*

Protected by WP Anti Spam