Total Pageviews

Monday, 30 January 2017

Git Commands and Administration !!!

In my previous post, I discussed about the installation of Git SCM. In this post, let's see some working with Git. How a developer works, commands they use and the concepts they follow. Git is a distributed VCS, it has many advantages compared to SVN. I am going to explain why we are maintaining a local and remote repo.

Git uses its own directory structure,

    1. master
             main workspace... contains stable code.

    2. branches
             development branches, usually for every Release.

    3. tags
              Usually these are read only, contains files related to every build.

We have already seen basic commands like git add, status, --version, commit in the previous post. Lets go to some other regular commands.

Git log command: full log output

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git log
commit e4c2887da9aad9dedbd325b0926f53773076b057
Author: userh d <userh d>
Date:   Sun Jan 29 22:32:57 2017 -0500
1st file
commit c5ef99904b07aabc9d31324c96cd4d62649370b1
Author: admin <admin@gitblit>
Date:   Sun Jan 29 22:30:30 2017 -0500
Initial commit
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $


Git Short log command: one liner output 



user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git shortlog
userh d (1):
      1st file
admin (1):
      Initial commit
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

Creating users, simple GUI task...





Clone the repo to aj workspace,



user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ cd ../aj_git/
user@userH-PC MINGW64 /f/Workspace_local/aj_git $
user@userH-PC MINGW64 /f/Workspace_local/aj_git $ git clone ssh://aj@10.0.0.229:29418/git_onp_repo.git
Cloning into 'git_onp_repo'...
Password authentication
Password:
remote: Counting objects: 6, done
remote: Finding sources: 100% (6/6)
remote: Getting sizes: 100% (4/4)
remote: Compressing objects: 100% (72/72)
remote: Total 6 (delta 0), reused 3 (delta 0)
Receiving objects: 100% (6/6), 435 bytes | 0 bytes/s, done.
Resolving deltas: 100% (1/1), done.
user@userH-PC MINGW64 /f/Workspace_local/aj_git $

user@userH-PC MINGW64 /f/Workspace_local/aj_git $ ls
git_onp_repo/
user@userH-PC MINGW64 /f/Workspace_local/aj_git $ cd git_onp_repo/
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ ls
first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ touch                  123
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ ls
123  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        123
nothing added to commit but untracked files present (use "git add" to track)
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

Lets Commit,

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git add 123
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   123
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $


user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git commit -m "add 123 file"
[master 9799557] add 123 file
 Committer: userh d <userh d>
1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 123
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

 Let's push these changes to remote repo,

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git push
Password authentication
Password:
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 267 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To ssh://10.0.0.229:29418/git_onp_repo.git
 ! [remote rejected] master -> master (User "aj" does not have push permissions for "git_onp_repo.git"!)
error: failed to push some refs to 'ssh://aj@10.0.0.229:29418/git_onp_repo.git'
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

 My aj user does not have permissions for this, so lets assign them....


Let's try now....

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git push
Password authentication
Password:
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 267 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote: Updating references: 100% (1/1)
To ssh://10.0.0.229:29418/git_onp_repo.git
   e4c2887..9799557  master -> master
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

These changes should not be there on any other user's repo, lets move onto our earlier repo....

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ cd ../../git_onp_repo/
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

We can pull/fetch the diff changes or the changes pushed by other developers onto remote repo to our local repos...

Difference between pull and fetch…

We can pull the remote repo changes to local using both pull and fetch, but when we used fetch they won’t be applied to “local workspace”. Whereas when we use “pull” they will get applied to local.

No Untracked files, so lets try pulling these diff changes between local and remote repo.

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git pull ssh://admin@10.0.0.229:29418/git_onp_repo.git
Password authentication
Password:
remote: Counting objects: 3, done
remote: Finding sources: 100% (2/2)
remote: Getting sizes: 100% (2/2)
remote: Total 2 (delta 0), reused 2 (delta 0)
Unpacking objects: 100% (2/2), done.
From ssh://10.0.0.229:29418/git_onp_repo
 * branch            HEAD       -> FETCH_HEAD
Updating e4c2887..9799557
Fast-forward
 123 | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 123
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  first_file  README.md           We got them, 123 file is pulled….
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $



Command to see available remote repos...

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git remote -v
origin  ssh://admin@10.0.0.229:29418/git_onp_repo.git (fetch)
origin  ssh://admin@10.0.0.229:29418/git_onp_repo.git (push)
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

Command to clean untracked files,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ touch 7 8 9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  7  8  9  abc  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        7
        8
        9
nothing added to commit but untracked files present (use "git add" to track)
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git clean -f
Removing 7
Removing 8
Removing 9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  abc  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

Commands to deal with staged files,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ touch 7 8 9

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git add .
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   7
        new file:   8
        new file:   9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

There may be situations where we are not supposed to commit these files, but in future we may need them. So, to store them without committing we do stashing.

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash
Saved working directory and index state WIP on master: ca8adff add abc
HEAD is now at ca8adff add abc
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash list
stash@{0}: WIP on master: ca8adff add abc
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ 


user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  abc  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

Now after few days, if we want those changes to be applied, (always be careful with the naming)

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash apply stash@{0}
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   7
        new file:   8
        new file:   9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  7  8  9  abc  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

We can commit these changes, if we are sure with the files....


user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:                     
  (use "git reset HEAD <file>..." to unstage)
        new file:   7
        new file:   8
        new file:   9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

To view the stash list,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash list
stash@{0}: WIP on master: ca8adff add abc

To drop/delete a stash,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash drop stash@{0}
Dropped stash@{0} (7c48e681cf4a8bd25bfe3ce133f42d08aff01d50)
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash list
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash pop
           No stash found.                          ---- If found, it will applied and deleted from the list
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

To create branches,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git branch b1
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git checkout b1
A       7
A       8
A       9
Switched to branch 'b1'
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git status
On branch b1
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   7
        new file:   8
        new file:   9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ ls
123  7  8  9  abc  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $

To push this change to git server,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git push --set-upstream origin b1
Password authentication
Password:
Total 0 (delta 0), reused 0 (delta 0)
remote: Updating references: 100% (1/1)
Branch b1 set up to track remote branch b1 from origin.
To ssh://10.0.0.229:29418/git_onp_repo.git
 * [new branch]      b1 -> b1
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $



To view the Git directory structure,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git ls-remote
Password authentication
Password:
ca8adff0f969129fef85dc20fc6d185b67c7d5fb        HEAD
ca8adff0f969129fef85dc20fc6d185b67c7d5fb        refs/heads/b1
ca8adff0f969129fef85dc20fc6d185b67c7d5fb        refs/heads/master
24c6ca01c3306070b5ace459827ca5e37353e2aa        refs/meta/gitblit/reflog
From ssh://admin@10.0.0.229:29418/git_onp_repo.git
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $

Lets create/delete another branch. 


user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git branch b2
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git branch
* b1
  b2
  master
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git push --set-upstream origin b2
Password authentication
Password:
Total 0 (delta 0), reused 0 (delta 0)
remote: Updating references: 100% (1/1)
Branch b2 set up to track remote branch b2 from origin.
To ssh://10.0.0.229:29418/git_onp_repo.git
 * [new branch]      b2 -> b2
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $



user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git branch -d b2
Deleted branch b2 (was ca8adff).
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git branch
* b1
  master
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $



To push this change to remote repo,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git push origin :b2
Password authentication
Password:
remote: Updating references: 100% (1/1)
To ssh://10.0.0.229:29418/git_onp_repo.git
 - [deleted]         b2
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git branch
* b1
  master
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $




Commands to work with patchsets. Patchsets are helpful to share the data/files which are not pushed to main remote repo. Dev A can share his files to Dev B, without pushing them to actual git server using patchsets.

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git checkout master
Your branch is up-to-date with 'origin/master'.
Switched to branch 'master'
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ touch aa ab ac ad
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git add .
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   aa
        new file:   ab
        new file:   ac
        new file:   ad
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $


Let’s create a patch to share these changes to other developer “aj”…


user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git format-patch origin/master --stdout >patch1.patch
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  aa  ab  abc  ac  ad  first_file  patch1.patch  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

I am simply copying, since we are the user aj. In general we do sharing through “scp”, “mails”, “shared dirs.” etc…

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ cp patch1.patch ../aj_git/git_onp_repo/
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

“aj” user in his workspace….

user@userH-PC MINGW64 ~$ cd F:
user@userH-PC MINGW64 /f $ cd Workspace_local/aj_git/git_onp_repo/
            user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ ls
123  abc  first_file  patch1.patch  README.md
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        patch1.patch
nothing added to commit but untracked files present (use "git add" to track)
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

As of now, he does not have the changes done by admin…. Only untracked file is the patch file, which we copied just now…


user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git apply patch1.patch

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        aa
        ab
        ac
        ad
        patch1.patch
nothing added to commit but untracked files present (use "git add" to track)
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ ls
123  aa  ab  abc  ac  ad  first_file  patch1.patch  patchset_1.patch  README.md
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

If we are OK with the files, we can commit them to our local repo…

These are the few commands which are helpful while working with Git SCM. Still I didn’t discuss few concepts related to branch merging, rebase and cherry pick. They are simple and can be implemented easily according to the situation..
 
######################################################################################

No comments:

Post a Comment