More GIT for newbies

More GIT for newbies, developers who know GIT can never lose their code. Productivity and deliver time reduces by knowing more and more GIT.

Get to know more GIT with examples.

git init
Creates an empty Git repository – basically a .git directory with subdirectories for objects. An initial HEAD file that references the HEAD of the master branch is also created.

srinidhigs@Blog$ git s
fatal: Not a git repository (or any of the parent directories): .git
srinidhigs@Blog$ git init
Initialized empty Git repository in /Users/srinidhigs/Desktop/Srinidhi/code/Blog/.git/
srinidhigs@Blog (master)$

git remote add alias your_git_url
Creates alias for the url to the GIT repository.

git remote -v
Gets all corresponding aliases w.r.t url where your code is linked to GIT repository.

srinidhigs@Blog (master)$ git remote -v
srinidhigs@Blog (master)$ git remote add origin git@github.com:srinidhi-lwt/Practice.git
srinidhigs@Blog (master)$ git remote -v
origin git@github.com:srinidhi-lwt/Practice.git (fetch)
origin git@github.com:srinidhi-lwt/Practice.git (push)
srinidhigs@Blog (master)$

git checkout .
Undos changes in all the files.

srinidhigs@Blog (demo-branch)$ git status
On branch demo-branch
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git checkout — …” to discard changes in working directory)

modified: app/helpers/application_helper.rb
modified: app/models/application_record.rb

no changes added to commit (use “git add” and/or “git commit -a”)
srinidhigs@Blog (demo-branch)$ git checkout .
srinidhigs@Blog (demo-branch)$ git status
On branch demo-branch
nothing to commit, working tree clean
srinidhigs@Blog (demo-branch)$

git diff
Displays changes made in respective files.

srinidhigs@Blog (demo-branch)$ git diff
diff –git a/app/models/application_record.rb b/app/models/application_record.rb
index 10a4cba..ac5f4d3 100644
— a/app/models/application_record.rb
+++ b/app/models/application_record.rb
@@ -1,3 +1,5 @@
class ApplicationRecord < ActiveRecord::Base self.abstract_class = true end
+
+===== lorem ipsum =====

\ No newline at end of file
srinidhigs@Blog (demo-branch)$

git clean -n
Shows what all files would be removed.

git clean -f
Removes all untracked files.

srinidhigs@Blog (demo-branch)$ git status
On branch demo-branch
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git checkout — …” to discard changes in working directory)

modified: app/models/application_record.rb

Untracked files:
(use “git add …” to include in what will be committed)

app/models/Post.rb
app/models/User.rb

no changes added to commit (use “git add” and/or “git commit -a”)
srinidhigs@Blog (demo-branch)$ git clean -n
Would remove app/models/Post.rb
Would remove app/models/User.rb
srinidhigs@Blog (demo-branch)$ git clean -f
Removing app/models/Post.rb
Removing app/models/User.rb
srinidhigs@Blog (demo-branch)$

git reset
Unstages files which are staged in commit.

srinidhigs@Blog (demo-branch)$ git status
On branch demo-branch
Changes to be committed:
(use “git reset HEAD …” to unstage)

modified: app/models/application_record.rb

srinidhigs@Blog (demo-branch)$ git reset
Unstaged changes after reset:
M app/models/application_record.rb
srinidhigs@Blog (demo-branch)$ git status
On branch demo-branch
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git checkout — …” to discard changes in working directory)

modified: app/models/application_record.rb

no changes added to commit (use “git add” and/or “git commit -a”)
srinidhigs@Blog (demo-branch)$

git reset --hard
Undos the latest commit.

git log
Displays all commit hash and commit message in that branch.

srinidhigs@Blog (demo-branch)$ git log

commit 7e405cfb555708b721faafc2d71e2cb3d91670b8

Author: Srinidhi G S
Date: Sat Aug 19 01:03:47 2017 +0530

initial commit
srinidhigs@Blog (demo-branch)$

git show commit_hash
Displays changes in that particular commit_hash.

srinidhigs@Blog (demo-branch)$ git show 7e405cfb555708b721faafc2d71e2cb3d91670b8

commit 7e405cfb555708b721faafc2d71e2cb3d91670b8

Author: Srinidhi G S
Date: Sat Aug 19 01:03:47 2017 +0530

initial commit

diff –git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..bab620d
— /dev/null
+++ b/.gitignore
@@ -0,0 +1,21 @@
+# See https://help.github.com/articles/ignoring-files for more about ignoring files.
+#
+# If you find yourself ignoring temporary files generated by your text editor
+# or operating system, you probably want to add a global ignore instead:
+# git config –global core.excludesfile ‘~/.gitignore_global’

git stash
Stores all changes in a temporary directory.

srinidhigs@Blog (demo-branch)$ git status
On branch demo-branch
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git checkout — …” to discard changes in working directory)

modified: app/models/application_record.rb

no changes added to commit (use “git add” and/or “git commit -a”)
srinidhigs@Blog (demo-branch)$ git stash
Saved working directory and index state WIP on demo-branch: 7e405cf initial commit
HEAD is now at 7e405cf initial commit
srinidhigs@Blog (demo-branch)$

git stash pop
Pops out all changes previously in temporary directory. We can pop our stashed changes in another branch as well.

srinidhigs@Blog (demo-branch)$ git checkout master
Switched to branch ‘master’
srinidhigs@Blog (master)$ git stash pop
On branch master
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git checkout — …” to discard changes in working directory)

modified: app/models/application_record.rb

no changes added to commit (use “git add” and/or “git commit -a”)
Dropped refs/stash@{0} (182c45fd91d69623511736aecf1d598fbff61eec)
srinidhigs@Blog (master)$

rm -rf .git
Removes .git directory, hence tracking code using GIT is removed. No more the branch is displayed in terminal.

srinidhigs@Blog (master)$ rm -rf .git
srinidhigs@Blog$

Leave a Reply

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