|
#GIT Basic Commands |
|
|
|
#Creating new repo |
|
git init [project-name] |
|
|
|
#Cloning existing repo |
|
git clone [url] |
|
|
|
#List all new/modified files that needs to be committed |
|
git status |
|
|
|
#Shows difference in files that is not yet staged |
|
git diff |
|
|
|
#Add particular file for staging |
|
git add [file] |
|
|
|
#Add all files for staging |
|
git add . |
|
OR |
|
git add -A |
|
|
|
#Show difference between staging and latest local file |
|
git diff --staged |
|
|
|
#Show difference with a commit |
|
git diff HEAD |
|
git diff HEAD^ |
|
git diff HEAD~6 |
|
|
|
#Show difference between commits |
|
git diff HEAD^..HEAD |
|
git diff sha1..sha2 |
|
git diff master featureBranch |
|
git diff --since=2.month.ago --until=1.week.ago |
|
|
|
#Show all changes to a single file by lines with date |
|
git blame index.html --date short |
|
|
|
#Unstage file with preserved content |
|
git reset [file] |
|
|
|
#Committing file |
|
git commit -m "Message" |
|
|
|
#Adding and committing file in one shot |
|
git commit -am "Message" |
|
|
|
|
|
#List all branches |
|
git branch |
|
git branch -r #shows remote branches |
|
git remote show origin #Shows more details about each local and remote branch |
|
git remote prune origin #Cleans up deleted remote branches |
|
|
|
#Create new branch |
|
git branch [branch-name] |
|
|
|
#Checking out/Switching to specific branch |
|
git checkout [branch-name] |
|
|
|
#Creating and Checking out a branch in one step |
|
git checkout -b [branch-name] |
|
|
|
#Creating a branch and checking out from a different branch |
|
git checkout -b myfeature develop |
|
|
|
#Merging specific branch's commits into the current branch |
|
git merge [branch] |
|
|
|
#Adding remote link |
|
git remote add origin ssh://git@stash.ddhive.com:7999/test/test.git |
|
git remote rm origin |
|
git remote rename origin newname |
|
|
|
#List all remotes |
|
git remote -v |
|
|
|
#Publishing branch to remote repo |
|
git push -u origin [branch-name] |
|
|
|
#Pushing tags to remote |
|
git push <remote> --tags |
|
|
|
#Deleting branch locally |
|
git branch -d [branch-name] --> If changes are fully merged |
|
git branch -D [branch-name] --> Force deleting even if changes are not fully merged |
|
|
|
#Deleting branch from remote |
|
git push origin --delete [branch-name] |
|
OR |
|
#git push origin :[branch-name] |
|
|
|
|
|
#Delete file and stage deletion |
|
git rm [filename] |
|
|
|
#Remove from version control and preserve locally |
|
git rm --cached [filename] |
|
|
|
#Move/rename file and prepare for commit |
|
git mv [oldname] [newname] |
|
|
|
#Temporarily stash all modified tracked files |
|
git stash |
|
|
|
#Restores latest stashed files |
|
git stash pop |
|
#OR |
|
git stash apply + git stash drop |
|
|
|
#Lists all stashed changesets |
|
git stash List |
|
|
|
#Delete most recent stashed changeset |
|
git stash drop |
|
|
|
#Putting to stash and applying back |
|
git stash save |
|
or |
|
git stash save "message to stash" |
|
or |
|
git stash save --keep-index #To stash and keep staged files as such |
|
or |
|
git stash save --include-untracked #To stash and keep untracked files also |
|
|
|
git stash apply |
|
or |
|
git stash apply stashName |
|
|
|
git stash branch branchName stashName # to create branch from stash |
|
|
|
git stash show stashName # to get details of stash |
|
|
|
git stash clear # clear all stashes |
|
|
|
#Enabling color |
|
git config --global color.ui true |
|
#Listing all config |
|
git config --list |
|
|
|
#List version history |
|
git log |
|
#One line log |
|
git log --pretty=oneline |
|
#Log with change details |
|
git log --oneline -p |
|
#Log with how many lines inserted/remoted |
|
git log --oneline --stat |
|
#Log with graphical branching |
|
git log --oneline --grph |
|
|
|
#Filtering logs with time range |
|
git log --since=2.month.ago --until=1.week.ago |
|
|
|
#List version history in graphical manner |
|
git log --graph |
|
|
|
#Creating alias for log |
|
git config --global alias.mylog "log --pretty=format:'%h %s [%an]' --graph" |
|
git mylog |
|
#Alias for status |
|
git config --global alias.st status |
|
git st |
|
|
|
#List version history of a particular file |
|
git log --follow [filename] |
|
|
|
git diff [first-branch]...[second-branch] |
|
|
|
#Show changes on a particular commit |
|
git show [commit] |
|
|
|
#Reset head to a particular commit, preserving changes locally |
|
git reset [commit] |
|
|
|
#Resets head and discards all history |
|
git reset --hard [commit] |
|
|
|
#Reset last two commits |
|
git reset --hard HEAD^^ |
|
|
|
#Undo last commit and put the changes back to staging |
|
git reset --soft HEAD^ |
|
|
|
#Amending last commit |
|
git commit -amend -m "New message" |
|
|
|
|
|
|
|
#Downloads all history of a particular branch |
|
git fetch [branch-name] |
|
|
|
#Download history and incorporate to local branch |
|
git pull |
|
|
|
git pull --rebase #does fetch + rebase. It is better. |
|
|
|
git push |
|
|
|
#Resetting head to an old point |
|
git reset --hard 6ae0aa9c5cb678 |
|
#Clearing untracked files after hard reset |
|
git clean -f -d |
|
#Ignoring changes to two files |
|
git checkout -- file1.txt file2.html |
|
|
|
|
|
#Merging feature branch to develop branch |
|
$ git checkout develop |
|
#Switched to branch 'develop' |
|
$ git merge --no-ff myfeature |
|
#Updating ea1b82a..05e9557 |
|
#(Summary of changes) |
|
$ git branch -d myfeature |
|
#Deleted branch myfeature (was 05e9557). |
|
$ git push origin develop |
|
|
|
#Creating release branch from develop |
|
$ git checkout -b release-1.2 develop |
|
#Switched to a new branch "release-1.2" |
|
$ ./bump-version.sh 1.2 |
|
#Files modified successfully, version bumped to 1.2. |
|
$ git commit -a -m "Bumped version number to 1.2" |
|
#[release-1.2 74d9424] Bumped version number to 1.2 |
|
#1 files changed, 1 insertions(+), 1 deletions(-) |
|
|
|
#Merging changes to master and tagging |
|
$ git checkout master |
|
#Switched to branch 'master' |
|
$ git merge --no-ff release-1.2 |
|
#Merge made by recursive. |
|
#(Summary of changes) |
|
$ git tag -a 1.2 |
|
|
|
#Merging same changes to develop |
|
$ git checkout develop |
|
#Switched to branch 'develop' |
|
$ git merge --no-ff release-1.2 |
|
#Merge made by recursive. |
|
#(Summary of changes) |
|
|
|
#Deleting release branch |
|
$ git branch -d release-1.2 |
|
|
|
|
|
#Creating hotfix branch and merging to master and develop branches |
|
$ git checkout -b hotfix-1.2.1 master |
|
#$ ./bump-version.sh 1.2.1 |
|
$ git commit -a -m "Bumped version number to 1.2.1 and applied fixes" |
|
$ git checkout master |
|
$ git merge --no-ff hotfix-1.2.1 |
|
$ git tag -a 1.2.1 |
|
$ git checkout develop |
|
$ git merge --no-ff hotfix-1.2.1 |
|
|
|
#List Tags |
|
git tag |
|
#Checkout particular tag code |
|
git checkout v0.1 |
|
#Creating new tag |
|
git tag -a v0.2 -m "v0.2" |
|
#Pushing tags to remote |
|
git push --tags |
|
|
|
#Rebase applies local commits after remote commits. This avoids a merge commit |
|
#Rebasing master with remote master |
|
git fetch |
|
git rebase origin/master |
|
#If conflict is there fix it, git add, then |
|
git rebase --continue |
|
|
|
#Manipulating last 3 commits using rebase |
|
git rebase -i HEAD~3 |
|
#Then below options |
|
# p, pick = use commit |
|
# r, reword = use commit, but edit the commit message |
|
# e, edit = use commit, but stop for amending |
|
# s, squash = use commit, but meld into previous commit |
|
# f, fixup = like "squash", but discard this commit's log message |
|
# x, exec = run command (the rest of the line) using shell |
|
# d, drop = remove commit |
|
|
|
#Clone entire repo & rewriting history |
|
git clone repoName filterRepo |
|
git filter-branch --tree-filter anyShellCOmmand #[takes each commit, runs command, recommits] |
|
git filter-branch --tree-filter anyShellCOmmand -- --all # to run in all commits and all branches |
|
git filter-branch --tree-filter 'rm -f passwords.txt' |
|
git filter-branch --tree-filter 'find . -name "*.mp4" -exec rm {} \;' |
|
#or run below command to operate in staging are with git command |
|
git filter-branch --index-filter 'git rm --cached --ignore-unmatch passwords.txt' --prune-empty -- --all |
|
|
|
#To remove empty commits |
|
git filter-branch -f --prune-empty -- --all |
|
|
|
#Line endings |
|
Unix /n |
|
git config --global core.autocrlf input |
|
Windows /n/r |
|
git config --global core.autocrlf true |
|
|
|
#Sample .gitAttributes |
|
* text=auto |
|
*.rb text |
|
*.js text |
|
*.bat text eol=crlf |
|
*.sh text eol=lf |
|
*.png binary |
|
|
|
|
|
git cherry-pick commitId |
|
git cherry-pick --edit commitId # to edit commit message |
|
git cherry-pick --no-commit commit1 commit2 # pick multiple and stage |
|
git cherry-pick -x commitId # keep same SHA |
|
git cherry-pick --signoff commitId # adds name of cherry-picker |
|
|
|
|
|
#Submodules |
|
|
|
git submodule add remoteRepoName |
|
#Any new local commits to submodule should be pushed to submodule remote and main repo remote seprately |
|
|
|
#Any new user need to run below after clone |
|
git submodule init |
|
git submodule update # should be be used to refresh the repo anytime |
|
#submodule update brings repo to headless state. To fix run below, |
|
git checkout master |
|
git merge hashShownInHeadlessState |
|
|
|
#To abort push if submodule is not pushed |
|
git push --recurse-submodules=check |
|
#To automatically push submodules |
|
git push --recurse-submodules=on-demand |
|
#Alias it as below |
|
git config --global alias.pushall "push --recurse-submodules=on-demand" |
|
|
|
#Git stores backup in reflogs (Only available locally) |
|
git reflog #-> to see backup. even after hard reset |
|
or |
|
git log --walk-reflogs #-> gives more details |
|
|