git config --global -e
git config --global core.editor "code --wait"
git config --global core.autocrlf true
git config --global core.autocrlf input
git config --help
git config --h
git init
- You can use
.for all the files in the directory - You can use
*.txtall the.txtfiles in the directory - You can use
file1.txtforfile1.txtin the directory
git add . || git add *.txt || git add file1.txt
git status
git commit -m "First Commit"
git ls-files
- Use the point 11 to see the files
git rm --cached -r accidentlyAddedFile.txt
git diff --staged
--- a/file1.jswill be the old file with -+++ a/file1.jsrepresent the new file with +@@ -1,3 +1,5 @@represent the header in the file and the left side represent the line number starting and ending and same for the right side but after the adding the old Snapshot to staging areahello \n worldrepresent the old file information and with+and green it represent new file
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
- Check the
.gitconfigfile for $REMOTE and $LOCAL should be added. Use point 1 to open.gitconfig.
git difftool
Display all the logs of the commits
git log
git log --onelinedisplay log in one linegit log --oneline --reversedisplay log in one line in descending ordergit log --oneline --statdisplay log in one line in show the changes also
Display all the code changes in the commits are done.
git show
git show 06ayis the commit id06ayused from point 17.git show HEAD~1show the changes start from HEAD and move 1 step ahead. 1 can be changed depending upon your commits.
Reverse the changes to older version locally (local machine). If you have modified a file and want to go back to older version locally
git restore file1.txt
- You can use
.for all the files in the directory - You can use
*.txtall the.txtfiles in the directory - You can use
file1.txtforfile1.txtin the directory
Reverse the changes from the staging area to the working directory (local machine).
git restore --staged file1.txt
- You can use
.for all the files in the directory - You can use
*.txtall the.txtfiles in the directory - You can use
file1.txtforfile1.txtin the directory
Restore the from previous commit to Working directory if you have accidently or intentionally deleted it.
git restore --source=HEAD~1 file1.txt
git show HEAD~1show the restore the file starting from HEAD and move 1 step ahead. 1 can be changed depending upon your commits.
Remove the untracked files
git clean
There are some tags used with this command.
- You can use
-hfor help.
git log --oneline -3
- Change -3 to how many numbers of logs you want.
git log --oneline --patch
git log --oneline --author="Saad"
git log --oneline --after="2020-12-01" ||
git log --oneline --before="2020-12-01" ||
git log --oneline --after="yesterday" ||
git log --oneline --after="one week ago" ||
git log --oneline --after="one month ago" ||
git log --oneline --after="one year ago"
git log --oneline --grep= "first commit by saad"
git log --oneline -S"add()"
git log --oneline -S"add()" --patch
git log --oneline 098aac..as1333
git log --oneline file1.txt
if error then use -> git log --oneline -- file1.txt
git show HEAD~2
git show HEAD~2 --name-only
git diff HEAD~2 HEAD
git diff HEAD~2 HEAD --name-only
git checkout 01480 #01480 is commit id
Here the HEAD will move to the commit and leave the master node.
git checkout master
git bisect start
git bisect bad
git bisect good c9821 #c9821 is commit id (git log --oneline)
git bisect reset
See all the options from git shortlog -h
git shortlog
See all the options from git blame -h
git blame file1.txt
git blame -e L 1,3 file1.txt
-e represent email and L represent Lines
See all the options from git tag -h
git tag v1.0.1 5ac56q #5ac56q is commit id(git log --oneline)
git branch newBranch
git branch
git switch newBranch
git branch -m newBranch newNameofBranch
git branch -d newNameofBranch
vocabulary
- use
-Dto forcefully delete the branch even if you haven't commit it.
git log master..newNameofBranch
git diff master..newNameofBranch
git diff --name-only master..newNameofBranch
or
git diff --name-status master..newNameofBranch
It means putting the changes into the save place without committing it to the repo.
git stash push -m "My first Stash"
git stash push -am "Add all Stash" # -a mean all and m mean message.
git stash list
git stash show @stash{1} # @stash{1} is id from point 51
or
git stash show 1 # 1 is sequence of stashes
git stash apply @stash{1} # @stash{1} is id from point 51
or
git stash apply 1 # 1 is sequence of stashes
git stash drop @stash{1} # @stash{1} is id from point 51
or
git stash drop 1 # 1 is sequence of stashes
git stash clear
Merging mean combining the branch changes with the master branch. Three type of merge
- Fast Forward
- 3 ways Merge
- Squash Merge
Fast Forward is a merge when there is commits in a linear way as shown in the figure.
Fast Forward merge is as follows:
git log --oneline -a --graph
git merge newNameofBranch
git merge --no-ff newNameofBranch
3 Way Merge is a merge when there is commits in a master branch and in another branch and it need to merge both the master and branch.
3 Way Merge is as follows:
Add commit to master
Add commit to the branch
then
switch to master
git merge commitofBranch
git branch --merged
git branch -d newMergedBranchName
git merge --abort
It means move the HEAD n Master pointer to the last commit using HEAD~1. It is not a good approach when there is a team in which you are working.
git reset --hard HEAD~1
It means move the HEAD n Master pointer to the last commit using -m pointing to the parent
git revert -m 1 HEAD
Squash is a merge when there are commits in a another branch and also in master branch n you want to add another branch commits and merge into master without using 3 way merge as shown in the figure.
Squash Merge is as follows:
git merge --squash bugfixBranch
Rebase is a merge when there are commits in a another branch and also in master branch n you want to you want to change the base of the another branchand make it master as shown in the figure.
Rebase is as follows:
Switch the branch.
git rebase master
Cherry Pick is feature you want from some commit and want to add into the master branch as shown in the figure.
Rebase is as follows:
git cherry-pick 05ab28 #05ab28 is commit id
git restore source=featureBranch --routes.js
The fetch command is use to clone/ fetch the remote branch commits into the master branch.
if you are using the same git repo, other team member make changes in the repo and push it into the central repo (GITHUB) and you want to merge those changes into your workspace than you will use fetch..
git fetch origin Branch Name ||
git fetch origin ||
git fetch
After fetching you have merge it git merge origin/master, there may be any conflict you have to resolve the conflict.
Git pull is advance version of the fetch.
This will make a 3 way merge using a simple pull command.
git pull
This will make linear merge using a pull --rebase command.
git pull --rebase
Git push did not sent tags to remote repo. You have to send it manually.
git tag v.11 # create Tag
git push origin v.11
git push origin --delete v.11
It will remove the tags from the repo
git -d v.11
It will add the branch to the remote origin (GITHUB)
git push -u origin add/PushBranchToOrigin
It will delete the branch from the remote origin (GITHUB)
git push -d origin add/PushBranchToOrigin
It will delete the branch from the remote origin (GITHUB)
git remote prune origin









