GIT

GIT Docs

Installation (Windows)

OR

IT team : Download git-process.rar and follow readme.txt

Installation (Linux)

Link(MD)

Configuration

Configure user name

git config --global user.name "abc"

Configure email

git config --global user.email abc@example.com

Configure all

git config --edit --global

Set editor instead command line editing (windows)

git config --global core.editor "notepad++ -multiInst -nosession"

Note : 'notepad++' system environment variable must be set else specify full path

Set beyond compare (bc) as difftool (windows)

//set difftool
git config --global diff.tool bc
//set difftool path
git config --global difftool.bc.path "C:\Program Files (x86)\Beyond Compare 3\BCompare.exe"

Note: beyond compare docs

SSH Setup for Git Server (ASL)

## (1) Contact IT team to provide domain user permission in GIT server
## (2) Generate key pair
ssh-keygen -t rsa -b 4096 -C "office_email@domain.com"

Note :

  • key will be at Ubuntu : "/home/specificUser/.ssh" or Windows : "C:\Users\specificUser.ssh" folder
  • default keys : id_rsa (private key), id_rsa.pub (public key)
## (3) copy the key to git server
ssh-copy-id userWindowsLogin@asl.com@gitServerIP
## (4) test the key
ssh userWindowsLogin@asl.com@gitServerIP

Commands

Basic main commands

Create new repository

//if client
git init
//if server / shared
git init --bare

Clone new repository

git clone PATH
//ignores last foldername of PATH
git clone PATH .

Check status

git status

Stage(select) new or existing files

//current directory
git add .
//particular file
git add file_name
//particular file/ folder path
git add path

Unstaging files

# file green to red
git rm menu.txt --cached
# folder green to red
git rm food_items/ -r --cached
# checkout style for file green/red to original
git checkout HEAD filename.ext
# reset style for folder green/red to original
git reset hash --hard

Delete

git rm.menu.txt //instead do manual windows delete

Rename

git mv menu.txt menu.md //instead do manual windows rename

Commit(local)

git commit -m "my first commit"

Push(remote)

git push //default remote = origin & branch = master
git push someRemote someBranch

Basic other commands

check log/ history

git log
git log -n
git log --graph --decorate --oneline //--all
git log --patch
git log --grep commitMsgSearchKeyword --oneline //content search available too
git log -GfileSearchKeyword --patch //-G is prefix (without --patch only commit details)
git log fromCommitHash..toCommitHash --oneline // git log HEAD~5..HEAD^ --oneline
(hash/branchName/HEAD)

diff

git difftool filename.ext //File Diff
git difftool --dir-diff //Folder Diff
git diff
git diff --cached
git diff branch1 branch2
git diff HEAD HEAD~2

reset (after local commit also)

git reset hash --hard //--soft, --mixed
git reset hash some_file.txt // to un add particular
Note : For particular file hard reset doesnot work, use checkout option
(git checkout HEAD menu.txt)
Better use checkout instead of ant hard reset option

Branches

# to create new branch at particular commit point
git branch newBranchName
# to check all local branches & active branch (with *)
git branch
# to push new local branch to remote
git push -u origin newBranchName
# to check local + remote branches
git branch --all
# to checkout remote branch (first time with tracking)
git checkout --track remotes/origin/some-branch-name
# To update the local list of remote branches
git remote update origin --prune
# to switch between branches (even master is a branch)
git checkout branchName
# delete a local branch
git branch -d localBranchName

More commands

help

git
git command --help
git help command
git command -h //short form for help

Tags

# create local tag
git tag tagName -m "first tag"
# push the local tags to remote branches
git push --tags
# or
git push origin --tags
# delete local tag
git tag -d tagName
# remote tag delete
git push --delete origin tagname

show

git show hash // hash/HEAD/branchName
git show hash^ // ^^/~Number
git show HEAD@{Number} //from git reflog head
eg:if merge exists -> git show hash~2^2 //go back 2 commits & consider it's 2nd parent

stash

git stash --include-untracked
git stash list
git stash apply stashName //stashName = stash@{0}
git stash clear

revert

git revert hash //hash/head

remote

# check remote origin (fetch/push address)
git remote -v
# to change remote origin
git remote set-url origin new_url
# add remote origin to local repo
git remote add origin PATH //origin is customName , git pull customName branchName
# track remote branch
git checkout --track origin/tarun_task

merge

git merge otherBranchName //say active is master, otherBranchName = lisa
Note : 1> resolve conflicts, 'git add' those files to mark them resolved & do 'git commit'
OR
2> resolve conflicts, 'git add' those files to mark them resolved
& do 'git merge --continue'
git merge master --allow-unrelated-histories
//other product Added as remote origin, then checkedOut remote+
//create some branch on thatdetached head then merge them

rebase

git rebase onToNewBaseBranchName //say active is lisa, onToNewBaseBranchName = master
git rebase -i hash //hash/branch/head

Later

//GIT & tortoiseGit install
git commit --amend
git cat-file hash -p //view content
git blame filepath
git reflog
git reflog head
git reflog refs/heads/master
git shortlog --summary --numbered //to get all the users on project & their commit count
------------------
** Move tortoise git SSH setting also here
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
//passphrase is DOB(DDMMYYYY)
//generate & add key in github site
ssh -T git@github.com
reflog //to track
- find (dangling) commits //https://opensolitude.com/2012/02/29/recover-git-branch.html
git fsck --full //https://git-scm.com/docs/git-fsck
//https://git-scm.com/docs/git-lost-foun
git fsck --unreachable --verbose
//check manual prune / garbage collection
git clone --bare PATH custName.git //(server / shared)
------------------------------------
git clone ssh://[user@]server/project.git
git clone ssh://repouser@192.xxx.x.xx/D:/FTP_SHARED_FILES/Deploy_Management/test
https://git.wiki.kernel.org/index.php/InterfacesFrontendsAndTools#gitolite
//access control in git (gitolite)
https://raymii.org/s/tutorials/Shared_Git_repository_via_ssh_for_multiple_users.html
//cloning just single branch
git clone --single-branch --branch <branchname> <remote-repo>
------------------------------------------------------------------------------------------
Advantages: (Think git object database as File pointer system)
--------
- Auto merge feature
- Same file warning (during merge)
- File rename (just pointer)
- Local commit & distributed
- Branching (& switching) between them is easy
- Traversing through tree nodes are easy
- Release management : No backups, reverting builds are easy (tags feature)
Can have equivalent code to particular build, so fixing bugs on old builds is easy.
- New feature branches are goodway to not break current code
till it's completed (squash merges)
- Semver Style - no build breaking code in final repo,
can use interactive rebase for feature development
(LTS & Current)

** connect domain users in git repo server & use access control in that way instead of shh ?

Issues

  • If Filename too long error, run following
git config --system core.longpaths true

Splitting subfolders as GIT repo

source link

move subfolder history & data to new branch

git subtree split -P <name-of-folder> -b <name-of-new-branch>

create blank repo & pull particular branch data

mkdir <new-repo>
git init
git pull </path/to/big-repo> <name-of-new-branch>

Converting client repo to server bare repo

Just have .git folder & it's setting "core.bare = true"

eg:
//moving out .git with new name
cd existing_repo
mv .git ../repo_new
cd ..
//change setting
cd repo_new
git config --bool core.bare true
cd ..
//remove old
rm -fr existing_repo

Reducing git size

  • find git repo size
git count-objects -v
  • Make current commit as the initial commit & delete old history
//new empty branch
git checkout --orphan latest_branch
//Add all the files
git add -A
//Commit the changes
git commit -am 'Initial commit message'
// Deleting master branch
git branch -D master
//renaming current branch as master
git branch -m master
//push the changes
git push -f origin master
//remove the old files
git gc --aggressive --prune=all
  • Note : You should delete all other branches and tags, because it may still contain the old history.

Tortoise GIT Usage

official docs

Regular Commit case

  • (right click) TortoiseGIT -> pull
  • (right click) GIT commit
  • Select (checkbox) files to commit
  • Write commit message
  • press "commit & push" button

GIT Pull Error

Pull Error comes when Somebody has committed the same file on which you are working.

(right click) TortoiseGIT -> pull

Screenshot

Click Ok

Screenshot

Pull will be rejected. Error message will also show the file name which has issue

Screenshot

Take backup of the file

Go to the repo folder->(right click) TortoiseGIT -> Revert

Screenshot

Select (checkbox) the file -> Click Ok

Screenshot

Pull again

Screenshot

GIT Push Error

Push error comes when you try to push without taking pull.

Commit and Push your files

Screenshot

Push rejected

Screenshot

Take backup of your files or whole repo folder.

Go to repo folder -> (right click) TortoiseGIT -> Show Log

Screenshot

origin/master and master will be on different commits as your changes are not pushed to server

Screenshot

right click on origin/master commit -> Reset "master" to this..

Screenshot

Click Ok

Screenshot

Reset will be success

Screenshot

Take pull and then check log by right clicking in repo folder -> TortoiseGIT -> Show Log. You will see someone has committed.

Screenshot

Add your changes to repo folder from backup then again Commit & Push

Screenshot

Revision History

DateVersionDescriptionAuthorReviewed ByReviewed DateRemarks
04-12-20191.0.0Initial ContentPrasan KumarPending
16-12-20191.1.0Added Splitting subfolders as GIT repoPrasan KumarPending
08-01-20201.2.0Added Tortoise GIT link and Tortoise GIT installation screenshotsPrasan KumarPending