Git Commands
Git Commands
Git Commands
git --version
git config --list -show-origin
git checkout feature/feature1
git status
git log -5
git log --follow a.txt
git log --stat
git reflog
git commit --amend -m "New Message" ; replaces the old Message with New for the
Last commit
; without creating new commit. Hash is changed.
; so it will change git history.git history is not to be changed unless we are the
only user.
git commit --amend ; interactive editor. we can add new files to an existing
commit.
git log
git log --stat
---cherrypick creates a New commit from the original commit; but does not deletes
the existing commit.
git log
git checkout feature
git log
git cherrypick <commitID>
git log
-- stash command. useful when we have made changes which we are not ready to commit
but to save them
for later or you may want to revert temporarily to prev commit and come back later.
git branch add
git checkout add
; modify files.
-- STASH Feature --------------------------------
git stash save "working on Add function" ; revert to last commit.
git status ; shows clean
git stash list ; list of all stashes.
;to apply the changes
git stash apply stash@[0] ; it will apply the stash but will not commit
git stash apply 2 ; will apply the stash 2. will work only when we prev stash is
not opened.
git stash push -m "Added Feature"
git status list ; will list the stash . stash will remain even after apply . it
does not gets deleted.
git stash drop 2 ; drop the stash index 2 in the stash.
git stash clear ; to clear all the stash entry.
git add -A
git add -A mydir/
git add mydir/
git add -u mydir/ ; only modified and delted files will be staged.
git add --no-removal mydir/ ; only modified and new files staged.
git add --no-all ; only modified and new files staged.
git remote add alias URL ( setting up alias for remote repo)
git fetch alias ( fecth all branches from that remote repo)
git remote rm origin ; to remove the connections of the remote rep.
git remote ; to list the connections of the remote rep.
git push
git stash
git stash list
git stash pop
git stash drop
git stash apply
tags
git tag -l ; or --list ; to list all tags.
git tag -a v1.4 -m "my version 1.4" ; create annonated tag.
git show v1.4
git tag v1.4-lw ; create lightweight tag.
git show v1.4-lw
git log --pretty=oneline
git tag -a v1.2 9fceb02
git push origin <tagname>. ; by default git push will not push tags to remote.
git push origin v1.5 ; explicitly push tag to remote.
git push origin --tags ; push all tags to remote at once.
git push pushes both types of tags
git push <remote> --tags will push both lightweight and annotated tags. There is
currently no option to push only lightweight tags, but if you use
git push <remote> --follow-tags ; only annotated tags will be pushed to the remote.
git tag -d v1.4-lw ; delete local tag only
-----------------------------------------------------------------------------------
--
GIT Branches.
git log testingbranch
git log --all
git log --oneline --decorate --all --graph
git switch testing ; switch to testing branch.
git switch -c testing ; also can use --create
git branch -d hotfix ; delete hotfix branch
git branch -v ; to list all commit on each branch.
git branch --merged ; list all merged branches
git branch --no-merged ; list non merged branches.
git branch -D testing; to forceful removal of branch whiah is not yet merged.
git branch --no-merged master ; to find out branches which are not yet merged with
master.
git branch --move bad-branch-name corrected-branch-name ; to rename a branch.
git push --set-upstream origin corrected-branch-name ; to push the new branch name
to remote.
git push origin --delete bad-branch-name ; to delete a remote branch.
git ls-remote <remote>, or
git remote show <remote> for remote branches as well as more information
git clone -o booyah ;then you will have booyah/master(instead of Origin) as your
default remote branch
git push origin serverfix:serverfix2 ;use this format to push a local branch into a
remote branch that is named differently
If you don’t want to type it every single time you push, you can set up a
“credential
cache”. The simplest is just to keep it in memory for a few minutes, which you can
easily set up by running
git config --global credential.helper cache
git fetch origin
git checkout -b serverfix origin/serverfix ; create a local based on remote branch
Checking out a local branch from a remote-tracking branch automatically creates
what is called a
“tracking branch” (and the branch it tracks is called an “upstream branch”).
Tracking branches are
local branches that have a direct relationship to a remote branch. If you’re on a
tracking branch
and type git pull, Git automatically knows which server to fetch from and which
branch to merge
in.
git checkout --track origin/serverfix ; will create a tracking branch in local
serverfix.
git branch -u origin/serverfix ;Branch serverfix set up to track remote branch
serverfix from origin.
git branch -vv ; to view all Tracking Branches.
git push origin --delete serverfix ; to delete remote branch serverfix.
git rebase --onto master server client
This basically says, “Take the client branch, figure out the patches since it
diverged from the server
branch, and replay these patches in the client branch as if it was based directly
off the master
branch instead.
git rebase <basebranch> <topicbranch> — which checks out the topic branch (in this
case, server) for you and replays it onto
the base branch (master) is same as
git checkout topicbranch
git rebase basebranch
---- THREE way REBASE -------------------------------------
git rebase --onto master server client ; client will be rebased to Master
$ git checkout master
$ git merge client ; Fast forward apply
$ git rebase master server ( server branch will be rebased)
$ git checkout master
$ git merge server ; Fast forward apply
$ git branch -d client
$ git branch -d server