• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

How to Use GIT

GIT

The workflow decision is typically based on the size and scale of your project. To gain a basic understanding of git for now, a simple, single-branch workflow will suffice. By default, the first branch on any git project is called “master”. In a following tutorial in this series, you will learn how to create other branches.

Creating your workspace

Just like you want to have a good, clean work environment, the same idea applies to where you do your coding, especially if you’re going to contribute to a number of projects at the same time. A good suggestion might be to have a folder called git in your home directory which has sub-folders for each of your individual projects.

user@host ~ $ mkdir -p ~/git/testing ; cd ~/git/testing

The above commands will accomplish two things:

 -It creates a directory called “git” in our home directory and then creates a subdirectory inside of that called “testing” (this is where our project will actually be stored).

– It brings us to our project’s base directory.

 We need to create a few files that will be in our project.

In this step, you can either follow along and create a few dummy files for testing purposes or you can create files you wish that are going to be part of your project.

Create a test file to use in repository

user@host ~/git/testing $ touch file

All your project files are in your workspace, you need to start tracking your files with git.

The next step explains that process.

Desired to gain proficiency on Git? Explore the blog post on Git Training to become a pro in Git.

Converting an existing project into a workspace environment

  The files are in git workspace,  need to tell git that you want to use your current directory as a git environment.

user@host ~/git/testing $ git init

Initialized empty Git repository in /home/user/git/testing/.git/

 After Initializing  new empty repository, you can add your files.

Add all files and directories to newly created repository:

user@host ~/git/testing $ git add

In this case, no output is good output. Unfortunately, git does not always inform you if something worked.

Every time you add or make changes to files, you need to write a commit message.

The next section describes what a commit message is and how to write one.

Creating a commit message

user@host ~/git/testing $ git commit -m “Initial Commit” -a [master (root-commit) 1b830f8] initial commit 0 files changed create mode 100644 file

There are two important parameters of the above command. The first is -m, which signifies that our commit message (in this case “Initial Commit”) is going to follow. Secondly, the -a signifies that we want our commit message to be applied to all added or modified files. This is okay for the first commit, but generally you should specify the individual files or directories that we want to commit.

user@host ~/git/testing $ git commit -m “Initial Commit” file

To specify a particular file to commit. To add additional files or directories, you just add a space separated list to the end of that command.

Check out the top Git Interview Questions now!

Pushing changes to a remote server

Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, Change master to whatever branch you want to push your changes to execute.

user@host ~/git/testing $ git remote add origin ssh://git@git.domain.tld/repository.git

user@host ~/git/testing $ git remote -v

origin ssh://git@git.domain.tld/repository.git (fetch)

origin ssh://git@git.domain.tld/repository.git (push)

The first command adds a remote, called “origin”, and sets the URL to ssh://git@git.domain.tld/repository.git. If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with.

You can push code to a remote server by typing the following:

user@host ~/git/testing $ git push origin master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 266 bytes, done.
Total 3 (delta 1), reused 1 (delta 0)
To ssh://git@git.domain.tld/repository.git
   0e78fdf..e6a8ddc  master -> master

Now, you are able to push your changes to the selected remote server

“git push” tells git that we want to push our changes, “origin” is the name of our newly-configured remote server and “master” is the name of the first branch.

In the future, when you have commits that you want to push to the server, you can simply type “git push”.

Branching

Branches are used to develop features isolated from each other. The master branch is the “default” branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.

create a new branch named “feature_x” and switch to it using switch back to master and delete the branch againa branch is not available to others unless you push the branch to your remote repository

git checkout -b feature_x
git checkout master
git branch -d feature_x

git push origin <branch>

For an Indepth knowledge on Git, click on below

Summary
Review Date
Reviewed Item
How to Use GIT
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on How to Use GIT"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.