a free and open source distributed version control system
Git is an Open Source Distributed Version Control System.
There are two types of VCS:
Each layer hold the Complete snapshot reference of code at that given moment
Working Tree
The Currnet working folder
Staging Area
Place where we select files which can be add to commit
Local Branch
Local Repo where our changes are saved
Remote Repo
Git server Repo where our changes are stored
Stashing Area
A Place to park our current change
Stash Area
Gitk is a graphical history viewer
Rebasing and merging are both designed to integrate changes from one branch into another branch but in different ways.
(Rebasing is better to streamline a complex history,)
Result of unnecessary local merges
To know more link
To avoid his use rebase while pulling you Change in local
-> git pull origin <branchName> --rebase
Squashing these commits can make the log more readable and understandable, both for ourselves and others.
git merge --squash FeatureBranc
Squash
git cherry-pick is a powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD.
git cherry-pick <commit-hash>
Cherry-Pick
Baisc commands
-> git init or git clone
-> git add sample.txt
-> git commit -m "Jira No:XYZ Added new File"
-> git remote add origin "git@git.bnymellon.net:XBBNNMH/git-demo.git" // Optional for git clone
-> git push origin master
Get Update From Remote
-> git pull origin <branch> or
-> git pull origin master --rebase
Amend Last Commit
-> git commit --amend
-> git commit --amend -m "an updated commit message"
->git commit --amend --no-edit
Safely Revert Pushed commits
-> git revert <Commit-id>
Park Current Changes
-> git stash save "Message"
-> git checkout master && git pull --rebase
...
-> git checkout featureBranch
-> git stash pop
Git Destructive cmd are rewrite the git history or that will altered the file changes in old commits pushed to remote
Permanently Remove Pushed commits
-> git reset <Commit-id> --hard
Amend Pushed commits
-> git rebase HEAD~5 -i
Force Push
-> git push origin branchName --force
if you commit sensitive data, such as a password or SSH key into a Git repository, you can remove it from the history, we can use either the git filter-branch command or the BFG Repo-Cleaner open source tool.
1. Loop through each commit and remove file
-> git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch ./password.properties"
2. Add .gitignore File to avoide pushing secure files in repo
3. Force push all branch and tags
-> git push origin --force --all
1 -> git push origin --force --tags
1. add a new remote in cmd
-> git remote add target <New git repo url>
2. Force push all branch and tags
-> git push target --force --all
-> git push target --force --tags
3. Push old Commits by appending new jira in all previous commits
1. Loop through each commit and update the matched commit
-> git filter-branch -f --msg-filter 'sed "s/OldText/NewText /g"' HEAD...HEAD~1 -- --all
2. Loop through each commit and update the matched commit
-> git filter-branch --msg-filter 'printf "NewJiraNumber: " && cat' master.. -- --all
3. Force push all branch and tags
-> git push target --force --all
-> git push target --force --tags
4. SVN to Git Migration
Steps
-> svn.exe log --quiet | ? { $_ -notlike '-*' } | % { "{0} = {0} <{0}>" -f ($_ -split ' \| ')[1] } | Select-Object -Unique
2. Clone svn repo
-> git svn clone ["SVN repo URL"] --prefix=svn/ --no-metadata --authors-file "authors-transform.txt" --stdlayout c:\mytempdir
3. Add .gitignore File
4. Add git remote and push it to git remote server
-> git remote add origin "git repo url"
-> git push
Steps
git clone <git repository A url>
cd <git repository A directory>
git remote rm origin
git filter-branch --subdirectory-filter <directory 1> -- --all
mkdir <directory 1>
mv * <directory 1>
git add .
git commit
2. Merge files into new repository:
git clone <git repository B url>
cd <git repository B directory>
git remote add repo-A-branch <git repository A directory>
git pull repo-A-branch master --allow-unrelated-histories
git remote rm repo-A-branch
-> git config --global core.editor
-> git log -p --follow [file-path] -> git checkout -