Git Commands

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

git clone https://[email protected]/rmagesh9999/sbo.git .

git --version
git config --list -show-origin
git checkout feature/feature1

2019wa15129 Incorrect Mapping of Answer.

-- CONFIGURING SSH KEY Authenticaton with BitBucket using GitBash.


ssh-keygen -t rsa -b 4096
ls -l ~/.ssh
eval ssh-agent
ssh-agent bash
ssh-add -l
ssh-add ~/.ssh/id_rsa
ssh-add -l
clip < ~/.ssh/id_rsa.pub
ssh -T [email protected]
---------------------------------------------------------------
git config --global alias.co checkout
git config --global alias.unstage 'reset HEAD --'
git unstage fileA
git config --global alias.last 'log -1 HEAD'
git last
git config --local color.ui auto ; local is the default repository specific
git config --global pull.rebase "false"
git config --global pull.rebase "true" ;If you want to rebase when pulling:
git config --global user.name "mage9999"
git config --global user.email "[email protected]"
git config --global color.ui auto
git config --global init.defaultBranch main
git config --global alias.aliasname "git command"
git config --list --show-origin
git config user.name ;to list username
git config --show-origin user.name
git add -h ; concise help list
git init d:\GIT\SBO\
git help <verb>
git <verb> --help
git clone <URL> <path of Dir>

git status
git log -5
git log --follow a.txt
git log --stat
git reflog

git push ORIGIN ( push current branch)


git push ORIGIN --all ( push all branch)
git push ORIGIN <branch> ( push branch to remote)
git push -u ORIGIN <branch> ; create an upstream connection and push local branch
to remote.
origin is name of remote repository.
git pull ORIGIN ( fetch the remote copy of the current branch and merge it to
local)
git pull ORIGIN <branch>
git pull origin main --allow-unrelated-histories
git pull --rebase ORIGIN
git remote -v ; information about remote repository.
git remote show origin
git remote rename pb paul; rename remote connection.
git remote ; list the remte connetion.
git remote remove paul
git branch -r ; list the remote branches.
git branch -a ; list all local and remote branches.
git branch --merged ; list all the branches which are merged.
git branch --merged master ; list all branches merged to master.
git branch -d <branch> ; local branch deletion
git push ORIGIN --delete <branch> ; remote branch deletion
git add -A ( all files in working directory including the top level)
git add <file>
git add <directory)
git add .
git diff ( diff of what is changed but not staged vs staged)
git diff --staged ( diff of what is staged to the last commit)
git diff --check ; to identify whitespace errors in commit files.
git log branchB..branchA ; shows the commits on Branch B not in Branch A
git diff BranchB...BranchA ; diff of Branch B vs Branch A
git commit -m "added"
git reset <file>
git reset --hard <commit> ; changes removed from working.
git reset --soft <commit> ; changes preseved in staging.
git reset <commit> ; changes moved from staging to working.
git revert <commit> ( create a new commit which undo all changes on the <commit>
and apply to the current branch)
git rebase <branch> ; change the base of the current branch to that of <branch>
git merge <branch> ( merge the Branch to the current branch)
git merge alias/branch ( merge remote branch to current branch)
git rebase <base> base can be commit ID, branch name.

git branch ( list)


git branch <New branch>
git branch -a ( List of All branches of Local and remote)
git branch --merged ( shows a list of Merged branches).
git checkout -b <new branch>
git checkout <branchname>
git clean -f
git clean -n
------------------------------------------------------------------------------
To Merge a Feature Branch to remote Master and delete the local feature branch post
merge.
git checkout master
git pull origin master
git branch --merged
git merge feature
git push origin master
git branch -d feature
git push origin --delete <remotefeature branch>
-----------------------------------------------------------------------------------
-----
----Working on a New Feature Branch and Merging to
Master.------------------------------
git branch feature ; to create a Feature branch in local.
git checkout feature ; or gir checkout -b feature.
<modify the files>
git status
git add -A
git commit -m "feature1"
git push -u ORIGIN feature ; pushing to remote.
git push -u origin featureB:featureBee ; feature B local to featurebb remote.
git pull origin master
git merge feature
git push origin master
git branch -d feature
git push origin --delete feature
---------------------------------------------------------------------

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

git checkout master


git log
git reset --soft <firstcommitid> ;; soft reset will remove from commit and move
files to staging.
git log ; second commit gone.
git status ; soft reset will move files to staging.
git reset <firstcommitid>; removes files from commit and move to working.
git reset --hard <firstcommitid> ; remove files from commit and also from Working.
; untracked files will exist.

git clean -df ; remove all untracked files and directory.

--to recover the files back from Hard reset.


git reflog ; list of all changes done.
git checkout <Hashid> ; Hashid to be recovered. We are in a Detatched Head state.
git branch bkup ; to recover the changes done on the above hashid.
git log
git diff <commit1> <comit2>

-- 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 checkout -- ; checkout to last commit.


git status pop ; pop will apply the top savepoint of the stash and delete it from
stash.
git stash list
git stash drop stash@[0]
git stash clear ; to clear all savepoint

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 reset HEAD <file>... to unstage


git restore --staged <file>... to unstage; restore similar to reset.
git restore file ; to unmodify the modified file in working to the last commit.
git add <file>... to update what will be committed)
git checkout -- <file>... to discard changes in working directory; replace with
last committed.
; if you want to keep the changes and want to revert then do stash.
git restore <file>... ; to discard changes in working; similar to above command.
git status -s or git status --short
git commit -a -m "mesg" ; to bypass staging area.
git diff --cached ; or git diff --staged. ; compare last commit and staging.
git diff ; compare working and staged
git diff HEAD ; diff between working directory and last commit.
git rm file ; removes file from both staging and working.
git rm -f readme ; forceful removal of modifiedfiles.
git rm --cached a.log ; retains the file from disk; but remove it in staging to
stop tracking.
git rm log/\*.log ; backslash is required so that git expands.
git log -p -2 ; show patch diff
git log --pretty=format:"%h %s" --graph
git log --pretty=oneline --graph
----------------------------------------
git mv README.md README is same as
$ mv README.md README
$ git rm README.md
$ git add README
------------------------------------------
git log --stat
git log --graph
git log -p ; shows the patch introduced in each commit.
git log --since=2.weeks; after 2 weeks
git log --after=2.weeks;after 2 weeks
git log --before=2.weeks;before 2 weeks
git log --until=2.weeks; commits made prior to 2 weeks.
git log -- path/to/file ;
git log --since="2008-10-01"
git log --since="2008-10-01" --before="2008-11-01" ; list changes made in oct 2008.
git log --grep="string" ; commit messages which contains the string.
git log --author="magesh"
$ git commit -m 'Initial commit'
$ git add forgotten_file
$ git commit --amend
pushes the old commit out of the way and puts the new commit in its place.
Effectively, it’s as if the previous commit never happened, and it won’t show up in
your repository history

git rm <file> delete the file and stage for commit


git mv <file1> <file2> move the file and stage for commit

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

-- GIT merge and rebase


git checkout master
git merge --squash feature ; will squash all changes in feature branch and merge to
Master. But not commit.
git commit -m "Merged Feature"

git checkout feature


git rebase master ; change the base of the feature branch.
git checkout master
git rebase feature ; will make feature and master branch identical and HEAD for
both brach will be same
-- do not rebase commits that exist outside your repository.

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

------- RENAME a Local Branch and move to remote -------------------


$ git branch --move badname newname ; rename on Local
git push --set-upstream origin newname
git branch --all
git push origin --delete badname
-------------------------------------------------------------

git checkout -b serverfix origin/serverfix

You might also like