Wednesday, July 14, 2021

git - an introduction

 Revert the change
- Rollback file changes
- Commit Amend
- Commit Revert
- Delete untracked files

1. Roll back changes



===================

checkout
- you want to download a file from server to your client system (workspace).
you get a file from repo.
- modify the file and upload to remote server (repo) is checkin.

all the record who,what,when modified is recorded (metadata).



Server (repo)
Client (Workspace)
  - Workspace1
  - workspace2

why they use git compare to other tool?

Git
- cameout if linux development community
- Linux Torvalds - 2005
- Initial goals
  - speed
  - supported for non -linear development (thousands of paraller parallel branches)
  - Fully distributed
  - Able to handle large projects like linux efficiently

Distributed vs Centralized

Centralized model (CVS,Subversion,perforce)
Distributed model (Git, mercurial)


Central VCS Server
- Computer A
- Computer B

* network issue/latency

- Git takes snapshots
- Git uses checksums
  commit ID(SHA-1 Hash)
  Tree object: ID -> snapshot of the filesystem
  Author: Bill Johnson
  Commit Message: Initial Commit

Workspace is a folder where you will be working. This is going to be your local repository.
Inside your workarea, you are going to have three areas.

  Working area
  Staging area
  repo

When you do checkout, all the files are stored in working directory.
This is where you create and save the files.
But the process work little differently.
In git, you don't directly checkin. You checkout, modify the code/files in your working directory and then you are going to take the code into a new area called staging area. This is where git will take a snapshot.

From the staging area, you will be actually doing checkin. Now, content will be saved into repo (local).

Now, repository as well as staging area is hidden area for you. It is infact, not a folder but a virtual space that is available logically. This how it is created internally.


All the commits are stored in repository. The place you will be working is working directory.

First you create a file in working directory -> to modify you move the file to staging area and finally save on repo.

When file is first created, it is untracked file. There is no information about the file in repository thats why it is untacked file.

When file is put into repository, file is tracked file. what that mean? - that mean file is traceable by git.

When there is no difference between a file in Workspace and repo then the file is unmodified file. Once you modified the file, the difference between repository file and what you have been changed. This file is called modified file.

when you try to put this file in staging area, the file is called staged file.
When you try to store, you try to store in repository. Now, content on repo and the workspace is same or unmodified file.

Life cycle of a file is that a very new file, that is not available at repository is untracked file.
When you do checkout all the files are unmodified. When you make changes they are modified. When you put into staging area, they are staged.
From stage, you are going to commit.


what is github?
- it is a site for online storage of git repositories.
- Many opensource project use it including linux kernel
- You get a free repo for personal use or project. You can have private repo with some fee.

==========================================

Git workflow

go to github.com and create an accoutn
-> create a new repository
name: myrepo2020 (name of the project product)
public
create repository

now you will get code

copy the path.

central repo: https://github.com/<myrepo>/myrepo2020.git


download client for your PC

search for git-scm

downlod for windows (git-bash)

install and open it.
you will see it like a linux command prompt.

$ ls
$ which git
$ git --version

Basic configuration
- need username, email
$ mkdir classroom/git; cd classroom/git

$ git config --global user.name "Jay K."
$ git config --global user.email "jay@jayk.com"
$ git config --global push.default "Simple"

With this we will create local copy of workspace. For this you need central repository.

make a copy of
$ git clone <source-central-repo> <dest-local-repo>
$ git clone https://github.com/<myrepo>/myrepo2020.git
it will create myrepo2020 on the current directory.
$ ls
you will see the repo.

or,

$ git clone https://github.com/<myrepo>/myrepo2020.git workspace2030

it will create new folder workspace2030 @current directory.

execute the following command
$ echo "First line"> 1.java
$ git status

you see untracked files

put the file in repo, then you will have tracked file.

now, store the file in repo. You can not put file directory in repo. You have to first put the file in staging area and then store in repo permanently.

you have two commands
add- adding file to staging area, creates a copy of file

$ git add <filename> -> adds single file
$ git add * -> adds all files that are modified.

$ git status

no commit yet, changes are ready to be commited.

that mean files are in staging area and ready to save.

$ git commit -m "First change for testing purpose"
-m - message -> what changes you made.


when you do commit,
it will take user name from global config file.
time stamp from machine and detail what it got from snapshot.



$ git status
now file is unmodified.


Now, lets go ahead and modify the file again,

$ echo "Line 2">> 1.java
$ git status

modified
file is modified.

$ cat 1.java

now first you stage the file

$ git add 1.java
$ git status
file is added to staging area

what if you have to modify multiple files
say

$ echo "first line" > 2.java

we have 2.java file.
$ git status
it shows now, there is 1.java file which is modified and 2.java which is untracked.

untracked file is not available in repository.

$ git add . -> all modified file will be added to staging area,

$ git status

Now, lets commit
$ git commit -m "Second commit with set of changes"

$ git status

as a user you keep modify and add to repo.

$ ls

when you run ls, you will not see repository. As we discussed earlier, its a hidden foler.

How to see the repo?
====================

in the current directory, there is a hidden directory .git.
this is where commits are available. You are not supposed to modify manually.
$ ls -la

$ cd .git' ls -la


$ git log
gives you detail about commit

40 digit commit id.
author
date

every commit, you will get unique commit id.

when you run git log you will see latest commit id on the top

to find latest commit id, you can give the number

$ git log -3 -> displays 3 commits

$ git log --online -> gives small output without id and author

$ git log --online -l

$ git show <commit_ID>

git show c54333> -> give 6 letters from commit id.


we learned we have repo, we downloaded and committed.

$ mkdir test
$ git status
$ touch test/file1
$ git status
$ git add .
$ git commit -m "Testing .."


whatever we made changes locally, it is not available on central repository.
to update or share the changes you made locally, you can push.

$ git push

how to changes are tracked? with commit id.

what git will do when you run git push?
git will try to take the commit ids which are not available on central repo but available on local repo. only those commits are going to be pushed.


transaction details are commit ids.

coming back to our workspace, we can see the commit id by running
$ git log --online

you see three changes, three commit id

What happend when you run git push

- it will take all 3 commit ids and going to push to central repository.

first it will try to connect to central repository, if you have not set up already, will prompt for user name and password.

once you supply username/pw, info is pushed to central repo.

go to your online repo repo, you will see the files

now, come back and push again
$ git push

it will say everything up-to-date.

as of now, your local and repote repo are on sync.


How does it know where to push the change?
inside your .git, you have config file

$ cat .git/config

when you run git push, it will read the config file and push to the repo.


There might be multiple user working. They might also clone...
and modify on local repo.

$ mkdir classroom/lab1; cd classroom/lab1
$ git clone https:/github.com/<user-name>/repo-name.git workspace01

all the modification on local repo is only available locally.

$ echo "First line" > 4.java
$ git stauts
untracked
$ git add .
$ git commit -m "added"

$ git log --oneline

displays commit ids.

now to uplod to central repo
$ git push

now, it will only push the one that are changes.

look at the message

go to repote repo and review the changes.


go back to old workspace
but now central repo is updated but this is not updated.
you need to update first that mean pull from central repo

this mean commit id available from central repo, you will be downloading.
$ git log --oneline
$ git pull
update mean -> update with whatever you have on central repo.

$ git log --oneline


review
Configuration
$ git config --global user.name "Jay K"
$ git config --global user.email "jay@jay.com"
$ git config --global push.default "Simple"

User/developer workflow
----------------------
$ git clone <source-central-repo> <dest-local-repo>
$ git clone https:/github.com/<user-name>/repo-name.git myworkspace
$ git status
$ touch myfile; echo "Hello">myfile
$ git status
$ git add <file> or git add .
$ git status
$ git commit -m "mesage"
$ git log -num -ineline
$ git show <commitid>
$ git push
$ git pull

======================
Branch












 






















No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...