Insert How to use Git Repository - Ubuntu ~ Git version control and GitFlow

How to use Git Repository - Ubuntu

Environment:

Ubuntu

Working with Git:

Create Project Folder:

You need to create a project folder where you want git to be initialized. If you already have a folder, skip this step.

$ mkdir ~/my_blog

Above command will create a directory called "my_blog" in your home directory.

To start a new Git repository:

$ cd <project folder>

$ Git init

Creates a new and empty git repository in the project folder.








There is a .git subdirectory under my_blog (which is the project folder) where all the project files will be stored under this folder.

To work on existing project:

$ cd <project folder>

$ git clone <remote repository url>

Above command will get all the contents and history of the project from remote repository(assuming you already have a remote git repository url) to your local repository where you can view the code, make changes. After executing git clone command, git will automatically create a project folder in your local directory where you ran git clone command. Folder name is same as the url name you mentioned.

$ git clone https://src.git.com/stash/scm/sampleproject.git
Initialized empty Git repository in /home/developer/sampleproject/.git/
remote: Counting objects: 100, done.
remote: Compressing objects: 100% (86/86), done.
remote: Total 100 (delta 35), reused 0 (delta 0)
Receiving objects: 100% (100/100), 9.51 KiB, done.
Resolving deltas: 100% (35/35), done.

$ cd sampleproject
ls
README  lib
ls -a
.        ..       .git     README   lib
$ cd .git
$ ls
HEAD        description info        packed-refs
branches    hooks       logs        refs
config      index       objects
 
Adding files:

Now that you’ve a git repository, you can add/edit project files.

When you add a new file to the git repository, to proceed to next steps you need to find out git status. 

pwd
/home/developer/git/my_blog
$ ls –a
.  ..  .git  à No new files added or edited
ls
test  à after adding new file
git status
On branch master
Initial commit
Untracked files:
  (use "git add <file>..." to include in what will be committed)
test
nothing added to commit but untracked files present (use "git add" to track)

Now you need to add newly created file to the git staging area. "Staging area" is a place where all new/updated files are stored and when you finish editing or adding files, you can commit them all at once.

To add files to staging area:

$ git add <filename>
$ git add test

If you need to add all files, use " git add * "

$ git status
On branch master
Initial commit
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
new file:   test

To store all the staged files into git database, you need to execute “git commit” command with a message.










All our changes have been committed. 









Understanding Git Branches:

To get list of branches, run “git branch –a” (-a: shows remote branches as well). You can see list of branches as shown below:
Initially, you’ll see only one branch “master” as you didn't add our remote repository yet.











After adding remote repositories, below are the list of branches that are displayed after executing “git branch –a” command. See section "Working with remote repository" to know about how to add a remote repository.

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

Note: * prefix tells, you’re currently on branch “master”.

master:
It is a branch in the local repository.

remotes/origin/HEAD:
It is the default branch for the remote named “origin”.

remotes/origin/master:
It is a branch named master on the remote named “origin”.

Working with remote repository:

Now, you’ve done everything in your local repository (local directory in your machine). To share your work with others or to view others code, you need a remote repository. First, you need to add remote repository before you start sharing your code to others or view others code.

$ git remote add origin <url>
$ git push origin master

Now that you have a branch in remote repository, others should be able to clone this repository (see section “To work on existing project”).

$ git fetch

When you want to know changes that others made, “git fetch” command will get all new changes from remote repository. But it will not integrate those changes into your local repository. 

$ git pull

Pulls all new changes from remote repository and integrate them into your local repository.

$ git push

If you need to push your changes to remote repository so that others can view your code, you need to execute “git push”.





 

No comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews