my git commands - vidyasekaran/current_learning GitHub Wiki

Install p4merge diff and merge tool that we can use for git

Install p4merge from below url https://www.perforce.com/downloads/visual-merge-tool

Add it in system path and configure p4merge and difftool for git from gitbash

shant@DESKTOP-GC9DKFC MINGW64 /c $ p4merge

shant@DESKTOP-GC9DKFC MINGW64 /c $ git config --global merge.tool p4merge

shant@DESKTOP-GC9DKFC MINGW64 /c $ git config --global mergetool.p4merge "C:/Program Files/Perforce/p4merge.exe"

shant@DESKTOP-GC9DKFC MINGW64 /c $ git config --global mergetool.prompt false

Configure difftool

shant@DESKTOP-GC9DKFC MINGW64 /c $ git config --global diff.tool p4merge

shant@DESKTOP-GC9DKFC MINGW64 /c $ git config -global difftool.p4merge "C:/Program Files/Perforce/p4merge.exe" error: did you mean --global (with two dashes)?

shant@DESKTOP-GC9DKFC MINGW64 /c $ git config --global difftool.p4merge "C:/Program Files/Perforce/p4merge.exe"

shant@DESKTOP-GC9DKFC MINGW64 /c $ git config --global --list user.email=[email protected] user.name=Vidyasekaran merge.tool=p4merge mergetool.p4merge=C:/Program Files/Perforce/p4merge.exe mergetool.prompt=false diff.tool=p4merge difftool.p4merge=C:/Program Files/Perforce/p4merge.exe


1.git version git version 2.28.0.windows.1

2. setup git in local by provide 2 info

shant@DESKTOP-GC9DKFC MINGW64 ~/projects $ git config --global user.name "Vidyasekaran"

shant@DESKTOP-GC9DKFC MINGW64 ~/projects $ git config --global user.email "[email protected]"

3. Now list the config set shant@DESKTOP-GC9DKFC MINGW64 ~/projects $ git config --global --list user.email=[email protected] user.name=Vidyasekaran

4. Clone a git repostory

shant@DESKTOP-GC9DKFC MINGW64 ~/projects $ git clone https://github.com/vidyasekaran/github-demo-udemy-jason.git Cloning into 'github-demo-udemy-jason'... warning: You appear to have cloned an empty repository.

shant@DESKTOP-GC9DKFC MINGW64 ~/projects $ ls github-demo-udemy-jason/

shant@DESKTOP-GC9DKFC MINGW64 ~/projects $ cd github-demo-udemy-jason/

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/github-demo-udemy-jason (master) $ ls shant@DESKTOP-GC9DKFC MINGW64 ~/projects/github-demo-udemy-jason (master)

5. Status of a git repository

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/github-demo-udemy-jason (master) $ git status

6. Add a file to repository

Our git files movement ---> Working Directory -> Staging Area -> Repository git folder -> Remote $ echo "test git quick start deom" >> start.txt

$ git status $ git add start.txt $ git status $ git commit -m "adding start text file" $ git status $ git push origin master - origin refers to github copy of our repository and master refers to our default and only branch in our repository.

Now our files will be pushed from working directory to remote by issuing git push origin master command Working Directory -> Staging Area -> Repository git folder -> Remote

To create a new git repository and push file to git

  1. login git and create a repository (vidyasekaran/currency-conversion-service
  2. in local clone from parent directory example : C:\Users\shant\projects > git clone https://github.com/vidyasekaran/currency-conversion-service.git
  3. above git clone would have created a directory : C:\Users\shant\projects\currency-conversion-service
  4. now copy all src and all contents into C:\Users\shant\projects\currency-conversion-service
  5. now do git add > C:\Users\shant\projects\currency-conversion-service> git add .
  6. do git commit -m "test"
  7. $ git push origin master
  8. now check all files are availble from browser https://github.com/vidyasekaran/currency-conversion-service

Create a fresh project locally and

cd C:\Users\shant\projects

  1. C:\Users\shant\projects> git init fresh-project (creates fresh project)

  2. cd /c/Users/shant/projects/fresh-project

  3. ls -a $ ls -a ./ ../ .git/

  4. cd .git you can see all folder and files that makes git repository $ dir config description HEAD hooks info objects refs

  5. $ git status
    On branch master (default branch git provides us) No commits yet nothing to commit (create/copy files and use "git add" to track)

  6. create a file hipster.txt

  7. $git status (shows untracked files to track it add it to local git first ) $ git status On branch master

No commits yet Untracked files: (use "git add ..." to include in what will be committed) hipster.txt nothing added to commit but untracked files present (use "git add" to track)

  1. shant@DESKTOP-GC9DKFC MINGW64 ~/projects/fresh-project (master) $ git add hipster.txt

  2. now check git status

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/fresh-project (master) $ git status (files are now in git staging area) On branch master No commits yet Changes to be committed: (use "git rm --cached ..." to unstage) new file: hipster.txt

For a project and then make changes

for this project - https://github.com/vidyasekaran/starter-web.git shant@DESKTOP-GC9DKFC MINGW64 ~/projects $ git clone https://github.com/vidyasekaran/starter-web.git

cd starter-web git add hipster git status git commit -m "hipster.txt added" git pull origin master (best practice to be in synch) $ git push origin master

Listing Files tracked By git Meaning without git add file git-lsfiles won’t list anything.

git-lsfiles

Adding and committing a tracked file ( you can do this for a tracked file only)

git commit -am “test commit”

If u have staged multiple files using git add and then do git commit -m “add files” than all tracked files will be committed to local git repo. Git add is used to stage modified file to git so that git status would show.

Recursively staging several files —————————————————————————————————-

Go to parent directory and execute

git add . git status git commit -m “add files”

Back out changes in git —————————————————————————————————-

git add hipster.txt

git status Shows upstaged file

If u want revert from staging to working directory stage Ie unstage

git reset HEAD level1-file.txt

git status wud show this file is in staged.

If u want to revert back changes same file as in git

git checkout —- level1-file.txt

Renaming file that is already committed always rename it before making any changes

git mv level3-file.txt level3.txt

Git status would show

Renamed: level3-file.txt -> level3.txt

git commit -m “renamed”

Creating a new branch

git branch mynewbranch git branch -a ( list both local and remote branches)

Git shows * on master branch, now I want to switch to mynewbranch

git checkout mynewbranch git branch -a ( shows * on mynewbranch now)

git log —oneline —decorate

qwewet (HEAD, origin/master, origin/HEAD, mynewbranch, master)

HEAD always point to last commit,

Rename branch

git checkout master Git branch -m mynewbranch newbranch git branch -a ( shows all local and remote branches)

git branch -d newbranch ( to delete a branch)

Branching and merging

git branch ( show local branch) git branch -a ( show both local and remote branches)

Create a branch and switch it to new branch git checkout -b title-change

Create a file simple.html and add it to title-change

git commit -am “changing title of HTML” ( add to staging and commit in 1 go)

git log —-oneline ( to see last commit)

Now to merge changes in title-change to master do

git checkout master

Check difference between 2 branches

git diff master title-change ( this difference between 2 branches)

To see differences visually

git difftool master title-change ( brings p4 merge and you can see differences visually)

Merge from source branch to destination branch

git checkout master

git merge title-change ( this merges source “title-change” to master) Fast forward means there has been no changes made to master Before we merged to master in master

git log —oneline —graph —decorate

Shows commit number and both master and title-branch point to same commit

asdfgf ( HEAD,title-change,master) commit message

git branch -d title-change

Git branch shows only master

git log —oneline —graph —decorate

Create A branch add-copyright add some files and then merge to master

Git checkout -b copy-right

Git branch now will have 2 branches

Master and copy-right

Create a file simple.html and merge copyright to master eliminating fast forward Git commit -am “adding file”

Git log —oneline —graph —decorate

Integrate changes to master Git checkout master Git merge add-copyright —no—ff option to allow all changes to be merged to master without fast forward message. Seems fast forward won’t merge all changes.

git log —oneline —graph —decorate

To bring up default editor

$ git config --global -e

Rebase

you have master branch and feature branch and assume commits has happened in both. now if u rebase from feature to master your commits are made on top of master (git history etc is effected ) - merge the history seems intact.

Now rebase

1.Check out new branch myfeature from master, my featuer will have all data master has...

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git checkout -b myfeature Switched to a new branch 'myfeature'

  1. Create a humans.txt and commit to myfeature

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (myfeature) $ notepad humans.txt

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (myfeature) $ git commit -am "thanks to all my students" [myfeature d9df6b6] thanks to all my students 1 file changed, 1 insertion(+)

  1. checkout to master

$ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'.

  1. make change to a index.html in master and commit

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ notepad index.html shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git commit -am "adding one lineer to index.html" [master 68a42e0] adding one lineer to i

  1. checkout to myfeature and apply rebase

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git checkout myfeature Switched to branch 'myfeature'

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (myfeature) $ git rebase master Successfully rebased and updated refs/heads/myfeature.

  1. Verify myfeature branch changes are applied to master

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (myfeature) $ git log --oneline --graph -all --decorate error: switch `l' expects a numerical value

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (myfeature) $ git log --oneline --graph --all --decorate

  • b5ebf77 (HEAD -> myfeature) thanks to all my students
  • 68a42e0 (master) adding one lineer to index.html
  • e83c41f (origin/master, origin/HEAD, add-copyright) changing title
  • 4d12e7b hipster.txt added
  • 5c05047 Copying files from initializr project zip file and then creating simple.html as basis for super simple pages
  1. now push myfeature to git

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (myfeature) $ git pull origin master From https://github.com/vidyasekaran/starter-web

  • branch master -> FETCH_HEAD Already up to date.

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (myfeature) $ git push --set-upstream origin myfeature Enumerating objects: 9, done. Counting objects: 100% (9/9), done. Delta compression using up to 4 threads Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 662 bytes | 662.00 KiB/s, done. Total 6 (delta 4), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (4/4), completed with 3 local objects. remote: remote: Create a pull request for 'myfeature' on GitHub by visiting: remote: https://github.com/vidyasekaran/starter-web/pull/new/myfeature remote: To https://github.com/vidyasekaran/starter-web.git

  • [new branch] myfeature -> myfeature Branch 'myfeature' set up to track remote branch 'myfeature' from 'origin'.

Cutting a branch from master and reviewing changes using difftool and then merge (Fast forward)

  1. create a new branch 'title-change' and modify simple.html and commiting it.

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git checkout -b title-change Switched to a new branch 'title-change'

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (title-change) $ notepad simple.html

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (title-change) $ git commit -am "one additional line added" [title-change e29cbd3] one additional line added 1 file changed, 2 insertions(+), 1 deletion(-)

  1. checkout to master modify simple.html and commiting it.

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (title-change) $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'.

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ notepad simple.html

  1. use p4merge difftool to check diffence between 2 branches master and title-change

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git difftool master title-change

  1. Now merge it by checkout to master and git merge title-change

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git merge title-change Updating b5ebf77..e29cbd3 Fast-forward simple.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)

Fast forward means the feature branch commits are put directly in master as if we didnot create any branch (title-change), to keep It wont show the history of branched etc in git log...

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git log --oneline --all --decorate --graph

  • e29cbd3 (HEAD -> master, title-change) one additional line added
  • b5ebf77 (origin/myfeature, origin

Cutting a branch from master and reviewing changes using difftool and then merge (Fast forward)

I have now modified simple.html in "title_change" and commited

git commit -am "new change" (am - adds to staging and commit in one line)

git difftool master title_change (see the difference and we see changes in simple.html in "title_change" alone)

check log

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (title-change) $ git log --oneline --all --graph --decorate

  • 506e3bb (HEAD -> title-change) simple.html changed
  • e29cbd3 (master) one additional line added

Now commit the file without fastforward i.e to keep the history intact --no-ff

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git merge title-change --no-ff Merge made by the 'recursive' strategy. simple.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)

Check the log to confirm git keeps the branching of title-change and commit made from title-change...Refer above example i.e fast-forward where u dont find this change came from title-change.

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git log --oneline --all --graph --decorate

  • 15cbf4e (HEAD -> master) Merge branch 'title-change' into master |
    | * 506e3bb (title-change) simple.html changed |/
  • e29cbd3 one additional line added

Git Stash

https://git-scm.com/docs/git-stash#:~:text=DESCRIPTION,to%20match%20the%20HEAD%20commit.

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

The modifications stashed away by this command can be listed with git stash list, inspected with git stash show, and restored (potentially on top of a different commit) with git stash apply.

You are making a change and you want to store it and start doing something in master fresh or something urgent..

  1. Modifying simple.html in master

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ notepad simple.html

  1. Execute git stash

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash Saved working directory and index state WIP on master: 15cbf4e Merge branch 'title-change' into master

  1. List git stash to find our last changes are present in stash@{0}

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash list stash@{0}: WIP on master: 15cbf4e Merge branch 'title-change' into master shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $

  1. Now i worked urgently on robots.txt

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ notepad robots.txt

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits)

Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) modified: robots.txt

  1. now go back to where u left before

git stash apply

working with multiple stashes

change something in master and stash

  1. Modifying 404.html and hipster.txt in master

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ notepad 404.html

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ notepad hipster.txt

  1. Apply both with comments "Modifying 404.html" and "modifying hipster.txt"

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash save "Modifying 404.html" Saved working directory and index state On master: Modifying 404.html

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash save "modifying hipster.txt" Saved working directory and index state On master: modifying hipster.txt

  1. Listing all stashes

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash list stash@{0}: On master: modifying hipster.txt -- This is the latest stash@{1}: On master: Modifying 404.html -- Penultimate change

  1. Verify whats been changed - git stash show stash@{0}

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash show stash@{0} hipster.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash show stash@{1} 404.html | 2 +- hipster.txt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)

  1. Reapply one of the stash

$git stash apply stash@{1}

Saving the stashes with proper message so its clear

  1. making and saving changes to "404.html" and humans.txt

$ git stash save "404.html changes" Saved working directory and index state On master: 404.html changes

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ notepad humans.txt

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash save "humans.txt change" Saved working directory and index state On master: humans.txt change

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash list stash@{0}: On master: humans.txt change stash@{1}: On master: 404.html changes

  1. show whats changed...

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash show stash@{1} 404.html | 2 +- hipster.txt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)

  1. Apply the changes in stash@{1}

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash apply stash@{1} On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits)

Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) modified: 404.html modified: hipster.txt

Untracked files: (use "git add ..." to include in what will be committed) .txt need it to keep history

no changes added to commit (use "git add" and/or "git commit -a")

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits)

Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) modified: 404.html modified: hipster.txt

Untracked files: (use "git add ..." to include in what will be committed) .txt need it to keep history

no changes added to commit (use "git add" and/or "git commit -a")

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ notepad 404.html

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $

clear all stashes

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash clear

List all stashes

shant@DESKTOP-GC9DKFC MINGW64 ~/projects/starter-web (master) $ git stash list

⚠️ **GitHub.com Fallback** ⚠️