Saturday, March 14, 2015

Git: Create Local and Remote repo in same host - Linux

As part of learning Git, I wanted to set up a local and remote repo in the same machine. Since Iam a newbie, there may be mistakes or better approaches available to do the same stuff in a better way.

First I started with creating a local repo

Local Repo

$ mkdir my-first-piece-of-software
$ cd my-first-piece-of-software/
$ git init


Create a file called today.txt

$ git status

$ git add today.txt

$ git status

$ git commit "My first git push"

Let us create a new branch called blue-feature. A branch is nothing but a context(it is not a separate directory or so).

$ git checkout -b blue-feature

Create a file called third.txt

$ git branch

Switch to master branch
$ git checkout master

$ git branch

When you type "ls" command, the file third.txt shall be not seen.

Merge the branch blue-feature with the master branch

$ git branch

Now we are in the master branch
$ git merge blue-feature

Now the file third.txt shall be shown under the context  of master branch 

Create a Remote Repo(Central Repo) in the same machine


$ cd ~

Don't forget to  add .git extension

$ mkdir remote-repo.git

$ cd remote-repo.git

$ git init --bare

Come back to Local Repo in the same machine


$ cd /home/testuser/testgit/my-first-piece-of-software
$ git checkout master

$ git branch -r

$ git remote add  origin ssh://testuser@localhost/home/testuser/testgit/remote-repo.git

'git remote add' means to add a reference to the remote repository.
'origin' is a name to refer to the remote repository

$ git branch -r

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

$ git remote show origin
The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is a1:2b:b6:31:4f:a9:e5:3e:b0:70:70:f7:89:30:23:b6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
testuser@localhost's password:
* remote origin
  Fetch URL: ssh://testuser@localhost/home/testuser/testgit/remote-repo.git
  Push  URL: ssh://testuser@localhost/home/testuser/testgit/remote-repo.git
  HEAD branch: (unknown)

Now push to publish the "commits" to the remote repository, for master branch
$ git push origin master
testuser@localhost's password:
Counting objects: 17, done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (17/17), 1.63 KiB, done.
Total 17 (delta 1), reused 0 (delta 0)
To ssh://testuser@localhost/home/testuser/testgit/remote-repo.git
 * [new branch]      master -> master

Show the remote repository branches
$ git branch -r
  origin/master

Show the local repository branches 
$ git branch
  blue-feature
  green-feature
* master
  red-feature

Switch to local repository branch "blue-feature"
$ git checkout blue-feature
Switched to branch 'blue-feature'

$ git branch
* blue-feature
  green-feature
  master
  red-feature

$ git remote show
origin

$ git push origin blue-feature
testuser@localhost's password:
Total 0 (delta 0), reused 0 (delta 0)
To ssh://testuser@localhost/home/testuser/testgit/remote-repo.git
 * [new branch]      blue-feature -> blue-feature

Show remote repository branches
$ git branch -r
  origin/blue-feature
  origin/master


$ git ls-remote --heads origin
testuser@localhost's password:
0b5c193fbca660b28f92d94b8597689ae4ce1d9d        refs/heads/blue-feature
0b5c193fbca660b28f92d94b8597689ae4ce1d9d        refs/heads/master

$ git remote show origin
testuser@localhost's password:
* remote origin
  Fetch URL: ssh://testuser@localhost/home/testuser/testgit/remote-repo.git
  Push  URL: ssh://testuser@localhost/home/testuser/testgit/remote-repo.git
  HEAD branch (remote HEAD is ambiguous, may be one of the following):
    blue-feature
    master
  Remote branches:
    blue-feature tracked
    master       tracked
  Local refs configured for 'git push':
    blue-feature pushes to blue-feature (up to date)
    master       pushes to master       (up to date)

$ git branch -r
  origin/blue-feature
  origin/master

Now Clone the remote repo


$ git clone ssh://testuser@localhost/home/testuser/testgit/remote-repo.git

No comments:

Post a Comment