Monday, 28 February 2022

Git Stash Save Changes Temporarily


Git Stash sample workflow

1. Modify a file

2. Stage file

3. Stash it

4. View stash list

5. Confirm no pending changes through status

6. Apply with pop

7. View list to confirm changes


# Modify file and add a file

git add .

git stash save "Saving changes"

git stash list

git status

git stash pop

git stash list

git status


Example

#update the branch files if necessary

#make staging area clean and commit changes

git status

git add -A

#add a newfile

git status

git stash

Saved working directory and index state WIP on new_branch: e436353 updated main

git status

On branch stash_branch

nothing to commit, working tree clean

git stash list

stash@{0}: WIP on new_branch: e436353 updated main


#can do many stash, the stash will hide the files

git stash 

git stash show


#can create a new branch

git stash branch stash_branch stash@{0}


#drop

git stash drop stash@{1}

#drop only  latest

git stash drop -q


#recover step by step

git stash pop stash@{0}

git stash apply stash@{0}


git stash apply 

#recover all changes, good to use if not too many stashes available

#recover not specifying stash number


#clean

#show untracked files

git clean -n

#remove  all untracked files

git clean -f


#date range, git stash list –before and –after options

git stash list --before 5.days.ago

#summary of changes for each element

git stash list --stat


#diff of changes for each stash

git stash list -p

#difference between a stash and your local Git working tree

git stash show -p stash@


# difference between stash and HEAD on the main branch

git diff stash@ main

#-p option (patch) to view the full diff of a stash

git stash show -p


#add a description to the stash

git stash save <description>


#push the stash entry created via git stash create to the stash reflog

git stash create "sample stash"

63a711cd3c7f8047662007490723e26ae9d4acf9

git stash store -m "sample stash testing.." "63a711cd3c7f8047662007490723e26ae9d4acf9"

git stash list

stash @{0}: sample stash testing..


Saturday, 26 February 2022

Git:- Creating Tags


Git uses two main types of tags: lightweight and annotated.


Annotated Tags:

$ git tag -a v2.1.0 -m "message"

Lightweight Tags:

$ git tag v2.1.0


git log --oneline

#use SHA-1 while creating a tag, for example

git tag -a v1.1 e436353

git show v1.1

git push --tags


#verify created tags

Github⇒Repo⇒Branch⇒Tags


Add/change some file then commit and create a new tag v1.2 by repeating the above steps

Tags and branches are completely unrelated


tag create a bundle of .zip and tar.gz of branch

It won't be in sync with each branch in the remote.


#delete a tag

git tag --list

git tag -d v1.1

#delete a tag from remote

git push origin --delete v1.1


#copy tag as a branch

git checkout -b new_branch v1.2


git log --pretty

 

Useful specifiers for git log --pretty=format lists some of the more useful specifiers that format

takes.


Specifier    Description of Output

    %H        Commit hash

    %h        Abbreviated commit hash

    %T        Tree hash

    %t         Abbreviated tree hash

    %P         Parent hashes

    %p         Abbreviated parent hashes

    %an        Author name

    %ae        Author email

    %ad        Author date (format respects the --date=option)

    %ar        Author date, relative

    %cn        Committer name

    %ce        Committer email

    %cd        Committer date

    %cr        Committer date, relative

    %s         Subject


Examples

#SHA-1, author, message
git log --pretty=format:"%h - %an, %ar : %s"

git log --oneline --decorate --graph --all

git log --pretty="%h - %s" --author='Name' --since="2022-01-01"   --before="2022-26-02" --no-merges

#SHA-1, Message,Date
git log --pretty=reference

#SHA-1, author, day,month,date,time,year, message
git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
git log --pretty=format:"%h%x09%an%x09%ai%x09%B"

#day,month,date,time,year, author, message
git log --pretty=" %C(reset)%ad %C(Cyan)%an: %C(reset)%s"

#SHA-1, day,month,date,time,year,hours ago, author, message
git log --pretty="%C(Yellow)%h  %C(reset)%ad (%C(Green)%cr%C(reset))%x09 %C(Cyan)%an: %C(reset)%s" -9

#SHA-1, time ago, message, author
git log --pretty=format:"%C(yellow)%h %ar %C(auto)%d %Creset %s , %Cblue%cn" --graph --all

create alias, add this line to your ~/.gitconfig:
[alias]
list = log --pretty=format:\"%C(yellow)%h %ar %C(auto)%d %Creset %s, %Cblue%cn\" --graph --all
git list

#SHA-1, branch & origin
git show-ref --head --dereference

#SHA-1, head Ref, action, message, date
git log -g --abbrev-commit --pretty=oneline
git reflog show

Friday, 25 February 2022

gitignore - Specifies intentionally untracked files to ignore


#create .gitignore file in repository path

.getignore file


*.xml

*.doc

package/*.*


git status

git add -A

git commit -m "git ignore"


create .doc .xml and package folder with index.html


git status

git add -A

git commit -M "commit"


do change in .xml or .doc files and it will be ignored with,

git status


More geeksforgeeks.org    linuxize.com


Tuesday, 22 February 2022

Git reset example for soft mixed hard modes


 --soft: uncommit changes, changes are left staged (index).

 git log --oneline

c6e3374 (HEAD -> main) Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

echo "new file" > new.txt

git add -A

git commit -m new.txt

[main 7ae912c] new.txt

 1 file changed, 1 insertion(+)

 create mode 100644 new.txt

git log --oneline

7ae912c (HEAD -> main) new.txt

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git reset --soft c6e3374

git log --oneline

c6e3374 (HEAD -> main) Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git status

On branch main

Your branch is ahead of 'origin/main' by 3 commits.

  (use "git push" to publish your local commits)

Changes to be committed:

  (use "git restore --staged <file>..." to unstage)

        new file:   new.txt

--mixed (default): uncommit + unstage changes, changes are left in working tree.

git commit -m "new file for --mixed"

[main 16ca1a9] new file for --mixed

 1 file changed, 1 insertion(+)

 create mode 100644 new.txt

emailid@ip-172-3X-21-XX7:~/gitdemo_feb$ git status

On branch main

Your branch is ahead of 'origin/main' by 4 commits.

  (use "git push" to publish your local commits)

nothing to commit, working tree clean

git log --oneline

16ca1a9 (HEAD -> main) new file for --mixed

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git reset c6e3374

    OR

git reset --mixed c6e3374

git log --oneline

c6e3374 (HEAD -> main) Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

emailid@ip-172-3X-21-XX7:~/gitdemo_feb$ git status

On branch main

Your branch is ahead of 'origin/main' by 3 commits.

  (use "git push" to publish your local commits)

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        new.txt

nothing added to commit but untracked files present (use "git add" to track)


--hard: uncommit + unstage + delete changes, nothing left.

git add -A

git commit -m "commit for --hard"

[main 9022f76] commit for --hard

 1 file changed, 1 insertion(+)

 create mode 100644 new.txt

git log --oneline

9022f76 (HEAD -> main) commit for --hard

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

ls

file1  file2  new.txt  

git reset --hard c6e3374

HEAD is now at c6e3374 Revert "ticket 93"

ls

file1  file2

git log --oneline

c6e3374 (HEAD -> main) Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit


Git example for revert show switch reset


#git revert 

git clone <https://url>

git log --oneline

9f58820 (HEAD -> main, origin/main, origin/HEAD) new file

2c76a55 first commit

echo "New demo file" > demo.txt

git add -A demo.txt

git commit -m "ticket 92"

git log --oneline

3ab8b74 (HEAD -> main) ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

echo "adding more line" >> demo.txt

git commit -am "ticket 93"

git log --oneline

0f70f4f (HEAD -> main) ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit


git show

OR

#using head

git show 0f70f4f

commit 0f70f4f177301f314ba83e409228bab6987156b4 (HEAD -> main)

Author: name <emailid@email.com>

Date:   Wed Feb 23 01:25:47 2022 +0000


    ticket 93


diff --git a/demo.txt b/demo.txt

index 3ad4fc4..746e199 100644

--- a/demo.txt

+++ b/demo.txt

@@ -1 +1,2 @@

 New demo file

+adding more line


#git revert (always record a new copy)

git log --oneline

0f70f4f (HEAD -> main) ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git revert 0f70f4f

[main c6e3374] Revert "ticket 93"

 1 file changed, 1 deletion(-)

git status

On branch main

Your branch is ahead of 'origin/main' by 3 commits.

  (use "git push" to publish your local commits)

nothing to commit, working tree clean


#redo the changes

git log --oneline

c6e3374 (HEAD -> main) Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

cat demo.txt

New demo file

git revert c6e3374

[main 7e7621e] Revert "Revert "ticket 93""

 1 file changed, 1 insertion(+)

cat demo.txt

New demo file

adding more line

git log --oneline

7e7621e (HEAD -> main) Revert "Revert "ticket 93""

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit


#switching head using git checkout 

git checkout 3ab8b74

Note: switching to '3ab8b74'.

...

git log --oneline

3ab8b74 (HEAD) ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git checkout main

Previous HEAD position was 3ab8b74 ticket 92

Switched to branch 'main'

Your branch is ahead of 'origin/main' by 3 commits.

  (use "git push" to publish your local commits)

git log --oneline

7e7621e (HEAD -> main) Revert "Revert "ticket 93""

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git log --oneline --graph

#graph view

* 7e7621e (HEAD -> main) Revert "Revert "ticket 93""

* c6e3374 Revert "ticket 93"

* 0f70f4f ticket 93

* 3ab8b74 ticket 92

* 9f58820 (origin/main, origin/HEAD) new file

* 2c76a55 first commit


#git reset

git log --oneline

7e7621e (HEAD -> main) Revert "Revert "ticket 93""

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

echo "This is foo.txt" > foo.txt

git add -A

git commit -m "version 1"

cat foo.txt

This is foo.txt

git log --oneline

b717ad4 (HEAD -> main) version 1

7e7621e Revert "Revert "ticket 93""

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git reset HEAD~1

OR

git reset 7e7621e

#example for mixed

git log --oneline

7e7621e (HEAD -> main) Revert "Revert "ticket 93""

c6e3374 Revert "ticket 93"

0f70f4f ticket 93

3ab8b74 ticket 92

9f58820 (origin/main, origin/HEAD) new file

2c76a55 first commit

git status

On branch main

Your branch is ahead of 'origin/main' by 5 commits.

  (use "git push" to publish your local commits)

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        foo.txt


git reset command In the simplest terms:

--soft: uncommit changes, changes are left staged (index).

--mixed (default): uncommit + un stage changes, changes are left in the working tree.

--hard: uncommit + un stage + delete changes, nothing left.


Friday, 18 February 2022

Merging branches in Git and Push files to branches using SSH Key

 

#at terminal

mkdir demo-merge

cd demo-merge/

ls

echo "hello DevOps" > index.html

git add -A

git init

git add -A

git commit -m "First Commit"

git branch

#change branch name to main

git branch -m main

git branch

git remote add origin <HTTPS URL>

git remote -v

git push -u origin main


#Now do the setting for Push files using SSH key

#Remove HTTPS URL

git remote remove origin

git remote -v


#ssh push and pull

ssh-keygen

#add password if necessary

|/home/ubuntu/.ssh/id_rsa

|/home/ubuntu/.ssh/id_rsa.pub 

#share .pub key with git-hub


Github.com⇒Settings⇒SSH and GPG keys⇒New SSH key

Title

#lab_sys

Key

#Copy /home/ubuntu/.ssh/id_rsa.pub key and paste

Add SSH key

confirm access


git remote add origin <SSH URL>   

git remote -v


git push -u origin main

#yes ask for the first time

#verify the push remote


#merging example

git checkout -b JIRA

echo "adding new feature" > view.java

git add *

git commit -m "done JIRA"

git push -u origin JIRA

git branch

* JIRA

  develop

  main

ls

demo.java  index.html  view.java

git checkout develop

Switched to branch 'develop'

Your branch is up to date with 'origin/develop'.

ls

demo.java  index.html

#to merge JIRA branch to develop switch to develop branch

git merge JIRA

Updating b50ca2a..4a62f5e

Fast-forward

 view.java | 1 +

 1 file changed, 1 insertion(+)

 create mode 100644 view.java

ls

demo.java  index.html  view.java

git push -u origin develop

#verify the push in Github remote, develop branch


Wednesday, 16 February 2022

Git Branch :- create, switching, rename, delete, push, status, merge


#Create a GitHub repository

Go to github.com, and log in to your account.

Click on the New button to create a new repository, enter a repository name and click on Create Repository button.


#Clone the GitHub repository

git clone <https URL>

cd Newbranchrepo

git branch

#creating a new branch

git branch new_branch


#Rename from branch

git branch -m new_branch

#Rename from another branch

git branch -m new_branch rename_branch


#Delete branch locally

git branch -d rename_branch

#Delete branch remotely

git push origin --delete rename_branch

#enter Username and Personal Access Token


#Switch to the new branch

git checkout new_branch

#Switch to the new branch by creating

git checkout -b branch_name

git branch


#Create a file and commit the changes

vim index.html

git add index.html

git commit -m "commit index file"


#push a new branch

git remote -v

git push -u origin new_branc

#enter Username and Personal Access Token


#Check the status of the new branch

git status


#Switch back to the main branch

git checkout main

git branch


More  Git-branch 


#Git merge

git merge branch_name


3-way merge

#Start a new feature

git checkout -b new-feature main

# Edit some files

git add <file>

git commit -m "Start a feature"

# Edit some files

git add <file>

git commit -m "Finish a feature"

# Develop the main branch

git checkout main

# Edit some files

git add <file>

git commit -m "Make some super-stable changes to main"

# Merge in the new-feature branch

git merge new-feature

git branch -d new-feature


Learn Git


Tuesday, 15 February 2022

Push file to GitHub Repository

 

Go to github.com, and log in to your account.

Click on the New button to create a new repository, enter a repository name and click on Create Repository button.


Create a repository on the local machine

 mkdir createnewproject

 cd createnewproject

 echo "# create new file for my project" >> README.md

 git init

 git add README.md

 git commit -m "first commit"

 git branch -M main


Push the changes in the local repository to GitHub

 git remote add origin <Your HTTPS_URL>

 git push -u origin main

 git status

Go to github.com, and check the remote repository


Monday, 14 February 2022

Create a Pull Request in GitHub


Create a Fork

Login to GitHub and from the same browser navigate to the following repository(owner) 

https://github.com/SharedRepo

In the top-right corner of the page, click the Fork button to enable Fork


Clone your Fork in terminal

mkdir Git-PR

cd Git-PR

On GitHub, click on the code button on your repository

Copy the URL from under Clone with HTTPS option

git clone [the copied HTTPS URL]

#clone command creates a local git repository from your remote fork on GitHub.


Sync fork with the original repository

cd SharedRepo

git remote -v


|git config --global --list

|#add user details if required

|git config --global user.name "Your Name"

|git config --global user.email "yourgitemailid@gmail.com


#commit

echo "my first files" > foo.txt

git status

git add foo.txt

git commit -m "first file"


|#see log

|git log #q to quit

|git log --oneline


Push changes

git remote -v

git push -u origin main 

#Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.

Github⇒Settings⇒Developer Settings⇒Personal access tokens⇒Generate new token

give userid and personal access token using keys shift+insert(*Enter)

#verify the commit in your GitHub account repository


Create a Pull Request

Your GitHub forked repository⇒Pull requests⇒New pull request⇒Create pull request

#Choose two branches to see what’s changed or to start a new pull request.

#check "base repository" and "head repository (your GitHub) is correct.

#share this URL or owner can see in their GitHub Pull requests tab.

#now the owner can see the pull request and from where it comes

The owner can select the Pull request⇒Merge pull request⇒confirm merge⇒comment(close with).


Friday, 11 February 2022

Add JDK Maven and Git Plugins with Jenkins

Add JDK Maven and Git Plugins with Jenkins in local

Login Jenkins⇒Manage Jenkins⇒Manage Plugins⇒available(search maven)⇒Maven Integration(Install without restart)

http://localhost:8080/updateCenter/    #to view


#in terminal

sudo apt install maven

mvn -version

Apache Maven 3.6.3

Maven home: /usr/share/maven

Java version: 11.0.13, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64

Default locale: en_US, platform encoding: UTF-8

OS name: "linux", version: "5.13.0-28-generic", arch: "amd64", family: "unix"


Add JDK

Login Jenkins⇒Manage Jenkins⇒Global Tool Configuration⇒add JDK

#Name

local_java

#JAVA_HOME

/usr/lib/jvm/java-11-openjdk-amd64

#uncheck Install automatically

save


Add maven

Login Jenkins⇒Manage Jenkins⇒Global Tool Configuration⇒add maven

#Name

local_maven

#MAVEN_HOME

/usr/share/maven

#uncheck Install automatically

save


Add Git

git --version

git version 2.25.1

Login Jenkins⇒Manage Jenkins⇒Global Tool Configuration⇒add maven

#Name

localGit

#Path to Git executable

/bin/git

#uncheck Install automatically

save



Install Jenkins on Ubuntu 20.04

 To Install Jenkins on Ubuntu 20.04


#java

java --version

openjdk 11.0.13 2021-10-19


#add jenkins public key

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

OK


#add jenkins repository 

sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'


sudo apt update

sudo apt install jenkins


#service status

sudo service jenkins status

Ctrl+C #exit

sudo systemctl start jenkins

sudo service jenkins status


#In the browser

http://localhost:8080/


#to get Administrator password

sudo cat /var/lib/jenkins/secrets/initialAdminPassword


#Follow the pages to complete

Install suggested plugins 

Create Frist Admin User

Instance Configuration

Jenkins is Ready!


Jenkins⇒Manage Jenkins⇒Jenkins CLI⇒download "jenkins-cli.jar"

#or go to http://localhost:8080/cli/ 


sudo java -jar jenkins-cli.jar -s http://localhost:8080/ -version

Version: 2.319.3


Add JDK Maven and Git Plugins with Jenkins


Ref:- how-to-install-jenkins-on-ubuntu-20-04