GIT- ARCHITECTURE
GIT - Practice
------------------------
Install git package:
----------------------------
# sudo su -
# apt-get update
# apt-get install git-core -y
# git --version
git version 2.17.1
# git init --> to create an Empty git repo
Create a Global repo:
-------------------------------
visit ---> www.github.com --> create a New Account
and do create a New Git Repo. and do Git clone
https://github.com/sathyadevops/dev830am
# git clone https://github.com/sathyadevops/dev830am.git
Cloning into 'dev830am'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
Method-1: Generate Token:
---------------------------------------
https://github.com/sathyadevops/ --> Settings --> Developer settings
select "Personal access tokens (classic)" --> create a New token
TOKEN: ghp_lgduMvXFoO9BAvNkaG6ixxx....
# cd dev830am
# vi demo.java
class Demo
{
public static void main(...)
{
s.o.print(" Hello world ");
}
}
# git add ---> to move code to stage area
# git commit -m "java code" ---> to save code in local repo
# git push ---> to move code to Global repo
Username for 'https://github.com': sathyadevops
Password for 'https://sathyadevops@github.com':
Counting objects: 3, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 344 bytes | 344.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/sathyadevops/dev830am.git
3a4f7ed..11f86ff main -> main
Method-2: using SSH-Keys:
----------------------------------------
# ssh-keygen (to generate a Key-pair)
# cd ~/.ssh/
# ls
authorized_keys id_rsa id_rsa.pub known_hosts
# cat id_rsa.pub (copy public key)
ssh-rsa AAAAB3NzaC1xxxx... root@ip-172-31-40-23
visit --> https://github.com/sathyadevops/dev830am
settings --> Deploy keys --> add key -->
Title: mykey
Key: paste key --> Allow write access --> add key
# git remote set-url origin git@github.com:sathyadevops/dev830am
# vi sample.c
void main()
{
.... code ....
}
# git add sample.c
# git commit -m "c-code"
# git push
Counting objects: 3, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 339 bytes | 339.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:sathyadevops/dev830am
11f86ff..2faec6f main -> main
Git move :
---------------
# mkdir mydir
# git mv sample.c mydir/sample.c
# git commit -am "file moved"
# git push
Git recover a deleted file :
--------------------------------------
# git rm index.html
# git status
# git reset HEAD index.html
# git status
# git checkout index.html
Git remove :
-----------------
# git rm index.html
# git commit -am "file removed"
# git push
Git Logs:
--------------
# git log
# git log -p
# git reflog
# git log --oneline
# git log --since=15-nov-2022 --until=16-nov-2022
Git Branches:
---------------------
# git branch br1
# git branch
# git checkout br1
# git branch
# vi index.html
........ BRANCH CODE ......
# git add index.html
# git commit -m "branch code"
# git push origin br1
# git checkout main
# git merge br1
# git status
# git push
# git branch -d br1 ---> to delete a local branch
# git push origin --delete br1 ---> to delete a Global branch
Git Merge Conflict :
----------------------------
# git branch
# git branch dev
# git checkout dev
# git branch
# vi demo.java
........... BRANCH-CODE ........
# git add demo.java
# git commit -m "branch code"
# git push origin dev
# git checkout main
# vi demo.java
........... MAIN-CODE ........
# git add demo.java
# git commit -m "main code"
# git merge dev ----> it leads to code conflict
# vi demo.java
........... accept the code changes .........
# git commit -am "code merged"
# git push
# git branch -d dev ---> to delete a local branch
# git push origin --delete dev --> to delete a Global branch
# git branch prod
# git checkout prod
(or)
# git checkout -b prod ---> to create and switch to branch
# git branch
# git branch -D prod ---> to delete branch forcefully
Git Stash:
---------------
# vi sample.c
........... CODE-A .......
# git stash save "Add(), sub() code"
# git stash list
# git stash apply stash@{0}
# vi sample.c
........... CODE-B .......
# git stash save "mul(), div() code"
# git stash list
# git stash pop stash@{0}
# git stash list
# git stash drop stash@{0}
# git stash clear
Git Rebase:
--------------------
# vi bank.java
class Bank
{
s.o.print("Bank code")
s.o.print("Bank code")
}
# git add bank.java
# git commit -m "bank code"
# git push
# git checkout -b devops
# vi bank.java
void Depoiste()
{
bal = bal + amount;
}
# git commit -am "Deposite() code"
# vi bank.java
void Withdraw()
{
bal = bal - amount;
}
# git commit -am "Withdraw() code"
# git log --oneline
# git checkout main
# git log --oneline
# git rebase devops
# git log --oneline
5cc893d (HEAD -> devops) Withdraw() code
753efd0 Deposite() code
9766285 (origin/main, origin/HEAD, main) bank code
b3936b3 add() sub() code
57a99a7 c-cdoe
7846347 code merged
26ad6a1 Main code
2faec6f c-code
11f86ff java code
3a4f7ed Initial commit
# cat bank.java
# git status
# git push
Git HEAD concepts
###https://www.cduan.com/technical/git/git-1.shtml
No comments:
Post a Comment