/ Home
Git Commands
Note: Git commands
Basic flow: (basis status check, add, commit, push)
git status
git add .
git commit -m "dc-00: comment"
git push
git diff *_client.html
git checkout -- abc.java
git commit filename
git diff --cached [filename]
git diff HEAD [filename]
Configure user details on Git
git config --global user.email "abc@gmail.com"
git config --global user.name "Your Name"
Git cache / github password cache:
git config credential.helper store
Reset a single file:
git checkout filename
Ref: Revert a single file
Git commit some files
End git diff
type q
Revert last push
git reset --hard <revision_id_of_last_known_good_commit>
git push --force
Add all except a single file
git add .
git reset filename
Git logs:
git log
Git logs with limit
git log -n 2
Git previous logs one line
git log --pretty=oneline
Git stats
git log --stat
git log --stat -n 2
Git log for particular author
git log --author="rajacsp" -n 2
### Git log graph
git log --graph --decorate --oneline
Ref:
Revert all uncommitted changes including new files
Revert changes to modified files
git reset --hard
Remove all untracked files and directories. (-f is force, -d is remove directories)
git clean -fd
Undo a last add before commit
git reset
Revert a single file
git checkout filename
Git add all but ignore one file
git add -u
git reset -- filename.txt
Revert one uncommitted file git
git checkout abc.php
Clone
git clone https://github.com/rajacsp/drivercheck
Ignore class files git
/*.class in gitignore
Short log
git shortlog -s
will get all users’ commit by count
Total commits count in Head, Branch and All
git rev-list --count HEAD
- will get the total commits in head
git rev-list --all --count
- will get the total commits in all
git rev-list --count origin/K-develop
Caching your GitHub password in Git:
Take branch
git checkout -b branch_name
Clone the right branch
git clone abc.git branch abc_branch
Merge branch with Master (puhsing branch’s code into master): verified on Feb 10, 2017
go inside master folder:
git checkout master
git pull origin master
git merge branch_name
git push origin master
If conflict occurs, fix them using method below:
<<<<<<< HEAD - old code start
======= - old code end
>>>>>>> plainreport - new code end
How to fix Git merge conflicts:
[Fix Git Merge](https://www.youtube.com/watch?v=g8BRcB9NLp4)
How to merge a new branch with master?
git checkout master
git pull origin master
git merge branch_name
git push origin master
git pull origin K-develop
git merge origin/K-develop
git push
Gitk
How to install gitk in MacOs?
brew update
brew install git
brew install git-gui
ref:
https://stackoverflow.com/questions/17582685/install-gitk-on-mac
How to install gitk on Ubuntu?
<TBD>
Git Tags
git tag v1.0
git tag -a v1.1 -m "Second version"
git tag
git show v1.0
git tag -l "v1.*"
git push origin v1.0
git push origin --tags
git push --tags
git tag -d v1.0
git push origin -d v.10
git checkout tags/v1.0
git fetch --all
Git and GitHub Beginner Tutorial 7 - Git Tags - what, why, when and how What is git tag, How to create tags & How to checkout git remote tag(s) How To Delete Local and Remote Tags on Git
How to delete a branch locally and remotely
// delete branch locally
git branch -d localBranchName
// delete branch remotely
git push origin --delete remoteBranchName
Find Git Version in cmd
git --version
git version 2.16.2.windows.1
if you get “command not found”, it simply means that you haven’t installed Git Version
Pull Request:
[pull-request](https://help.github.com/articles/about-pull-requests/
Git Cherry pick
git cherry-pick <commit-id>
Rewrite old history in GIT
java -jar c:/bfg/bfg-1.13.0.jar --delete-file *.mp3 pythonsamples.git
java -jar c:/bfg/bfg-1.13.0.jar --strip-blobs-bigger-than 30M pythonsamples.git
cd pythonsamples.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
Rename previous commit
git commit --amend -m "TACT-436: Script alignment made"
Change commit message after git push
git commit --amend -m "Message"
git push --force-with-lease
OR
git rebase -i eb5661a8e4de761d2af79c5ecbdbc875c34481fa
Stash existing changes
git stash (will stash your existing changes)
git pull (pull all of your new changes from central repository)
git stash apply (bring back your old changes on top of new code)
View stash files
git stash show
List stash
git stash list
Save/Apply Stash by Name
git stash save <stash_name>
git status apply stash^{/stash_name}
git stash save abc
git stash apply stash^{/abc}
Delete a branch
git push -d <remote_name> <branch_name>
git branch -d <branch_name>
delete multiple branches:
git push -d <remote_name> <branch_name_1> <branch_name_2>
Git Tutorial: Git Fetch
git cheat sheet: Git Cheat sheet
git commands: Git commands
more: Saving changes
Copy Repository with history: Copying a full git repo
git status -uno
Get all commits
git rev-list --all --pretty=oneline
https://stackoverflow.com/questions/1314950/git-get-all-commits-and-blobs-they-created
How to cherry pick
git checkout p (target branch)
git cherry-pick <commit_id>
Check diff https://stackoverflow.com/questions/436362/how-to-diff-a-commit-with-its-parent/449128#449128
How to find the parent branch?
git parent
https://stackoverflow.com/questions/3161204/find-the-parent-branch-of-a-git-branch
When did I create a branch?
git reflog --date=local
git reflog --date=local feat-feature-endpoint-p
https://stackoverflow.com/questions/2255416/how-to-determine-when-a-git-branch-was-created
Create a branch from another branch
git checkout -b myfeature dev
https://stackoverflow.com/questions/4470523/create-a-branch-in-git-from-another-branch
How to rename a branch name?
https://stackoverflow.com/questions/6591213/how-do-i-rename-a-local-git-branch
Git diff and apply
git diff new-feature..new-feature2 | git apply -
Find the parent branch: (buggy as well)
git for-each-ref --format='%(refname:short)' refs/heads/* | while read b; do if r=$(git config --get branch.$b.remote); then m=$(git config --get branch.$b.merge); echo "$b -> $r/${m##*/}"; fi; done
https://blog.liplex.de/git-show-parent-branch/
Git Rebase
https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase
git bi-sect
https://git-scm.com/docs/git-bisect
Git rebase manual vs Git rebase interactive Ref needed
Git rebase vs git merge https://stackoverflow.com/questions/804115/when-do-you-use-git-rebase-instead-of-git-merge
When should I git pull rebase https://stackoverflow.com/questions/2472254/when-should-i-use-git-pull-rebase
Merge vs pull
git pull origin master
is same as
git fetch origin
git merge origin/master
https://stackoverflow.com/questions/21756614/difference-between-git-merge-origin-master-and-git-pull
Merge another branch changes into mine
https://help.github.com/en/articles/merging-an-upstream-repository-into-your-fork
Show which files have been changed from another branch
git diff --name-status another_branch
git diff --name-status feat-product-apply-permission-K20-1382-latest
git diff --name-status p
https://stackoverflow.com/questions/822811/showing-which-files-have-changed-between-two-revisions
Delete a branch
git branch -D branch_name
https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely
Git stash
git stash list
will list all stashes
https://git-scm.com/docs/git-stash
Show git username
git config user.name
git config --list
https://alvinalexander.com/git/git-show-change-username-email-address
View file in another branch
https://stackoverflow.com/questions/7856416/view-a-file-in-a-different-git-branch-without-changing-branches
Undo pushed commits
https://stackoverflow.com/questions/22682870/git-undo-pushed-commits
How to revoke last pushed commit?
(ref needed)
Get the old check point
git checkout 85d29a6c0853f85303f87091efd8b3ac9ca69766
git add .
git commit -m "Last commit revoked"fa
git push origin HEAD:master -f
How to merge my branch to master in git?
git checkout master
git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git BRANCH_NAME
git push origin master
git push --set-upstream origin qa
git push --set-upstream origin feat-raja-one
Merge branch to qa
git pull
git checkout qa
git pull
git pull https://github.com/rajasgs/merge-test feat-raja-one
git push origin qa
Merging an upstream repository into your fork
How to mirror github repo?
git clone --bare https://github.com/kactLabs/kactKMNETL.git
cd kactKMNETL.git
git push --mirror https://github.com/rajasgs/kactKMNETLDec2019.git
cd ..
rm -rf kactKMNETL.git
How to change the committed message?
git amend
git commit --amend -m "Message"
How to change the remote url?
git remote set-url origin git@github.com:username/reponame.git
ref:
https://chatgpt.com/share/6c4af7db-f6cd-4bcf-956a-1b550b4b882e
How to check your current remote url?
git remote -v
you will something like this
origin git@github.com:kactlabs/reponame.git (fetch)
origin git@github.com:kactlabs/reponame.git (push)
Credits
- [submodules] (https://github.blog/2016-02-01-working-with-submodules/)
Git Advanced
Below is the requested content converted into clean, structured Markdown format.
How to migrate from master branch to main
git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a