Thursday, 11 October 2018

Git Command with Practical Examples on Linux – Part 2

Git Command, LPI Tutorial and Material, LPI Certification, LPI Learning

In previous article we have learned basic Git workflow. In this article we’ll be focusing on some advanced features like miscellaneous repository actions, branching and tagging.

Miscellaneous repository actions


Rename

As name suggests rename operation instructs Git that traced file has been modified. To rename file execute following command:

$ git mv README NEW-README

Now let us check the repository status:

$ git status -s
R  README -> NEW-README

In above output letter R appears before README, which indicates file has been renamed. Next column shows the old and new file name.

NOTE: To make these changes in remote repository execute git push command.

Move

Move operation is used to move file from one directory location to another. To move file execute following command:

$ mkdir new-dir
$ git mv README new-dir/

In this example, we have created new directory and move file to that directory. Now let us check the repository status:

$ git status -s
R  README -> new-dir/README

Above output shows that file has been moved to new directory.

NOTE: To make these changes in remote repository execute git push command.

Delete

As name suggests, delete operation removes file/directory from Git. To remove file execute following command:

$ git rm README

Now let us check the repository status:

$ git status -s
 D README

In above output letter D appears before the README which indicates that file has been removed from repository.

NOTE: To make these changes in remote repository execute git push command.

RESET

This operation reset current HEAD to the specified state. In Git, HEAD is the reference pointer which points to the latest commit.

Let us understand this with example:

$ touch AUTHORS
$ git add AUTHORS
$ git status -s
A  AUTHORS

In above example, we have created AUTHORS file and added it to changeset. Now this file will be part of next commit. Git reset command will adjust HEAD reference and remove file from changeset

$ git reset HEAD AUTHORS

Now, let us check the repository status:

$ git status -s
?? AUTHORS

Git Command, LPI Tutorial and Material, LPI Certification, LPI Learning

As expected, above output shows that file has been removed from changeset hence ?? symbol appears before filename.

Working with branches


Branch in version control system is a independent line of development. Unlike other version control system branching is really lightweight in Git. In this section we’ll discuss various branch related features.

Create branch

To create new branch execute following command:

$ git branch my-feature-branch

In above command my-feature-branch is branch name. This command will create local branch.

To create remote branch execute git push command as follows:

$ git push origin my-feature-branch

List branch

To list branch execute branch command without any argument.

$ git branch
* master
  my-feature-branch

For instance, above output lists local branches. Asterisk symbol represents current branch. In our case it is master branch.

To list local as well as remote branches execute following command:

$ git branch -a
* master
  my-feature-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

In above output, branches highlighted in red colour are remote branches.

Delete branch

To delete local branch use -d option as follows:

$ git branch -d my-feature-branch

Deleted branch my-feature-branch (was 220bf4d).

To delete remote branch execute following command:

$ git push origin :my-feature-branch

Note that we have use colon(:) with branch name which instructs git to delete remote branch

Switch branch

To switch in between branches execute checkout command as follows:

$ git checkout my-feature-branch

Switched to branch ‘my-feature-branch’

Now let us check the current branch:

$ git branch
  master
* my-feature-branch

Checkout branch

So far we have used separate command to create and switch to branch. However, we can achieve this is single command as follows:

$ git checkout -b my-feature-branch

Switched to a new branch ‘my-feature-branch’

Now let us check the current branch:

$ git branch
  master
* my-feature-branch

Restore working tree


Git allows us to discard uncommitted changes. We can achieve this using checkout command.  Let us understand this with example.

First modify existing file and check repository status:

$ echo "Update README" >> README
$ git status -s
 M README

Now to discard changes of README file, execute following command:

$ git checkout -- README
$ git status -s

As expected our working tree is clean hence last command is not showing any output.

Merge branch

As name suggests merge operation applies changes from one branch to another. Let us understand this step by step.

First create a new branch and commit changes in this branch:

$ git checkout -b my-feature-branch

Switched to a new branch ‘my-feature-branch’

$ echo "Update README" >> README.md
$ git add README.md
$ git commit -m "Updated README"
[my-feature-branch 42e28aa] Updated README
 1 file changed, 1 insertion(+)

Let us switch to master branch and apply changes to master branch

$ git checkout master

Switched to branch ‘master’

Your branch is up to date with ‘origin/master’.

$ git merge my-feature-branch
Updating 220bf4d..42e28aa
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

Now let us verify that changes are applied to master branch:

$ git log -1 --oneline
42e28aa (HEAD -> master, my-feature-branch) Updated README

Working with tags


Tag is a reference pointer to a particular commit in Git history. Often tag is used to mark release version of product. In this section we’ll discuss basic tag operations like create, list and delete

Create tag

Creating tag is really simple. To create tag execute following command:

$ git tag my-tag

Above command will create tag with name  my-tag. To create tag in remote repository execute push command as follows:

$ git push origin my-tag

List tag

To list tag execute Git tag command without any argument as follows:

$ git tag
my-tag

Delete tag

To delete tag execute Git tag command with -d argument as follows:

$ git tag -d my-tag
Deleted tag 'my-tag' (was 220bf4d)

To delete tag from remote repository execute following command:

$ git push origin :my-tag

Related Posts

0 comments:

Post a Comment