Git is VCS, and it stores files as continous snapshots. There are three main states of your files in Git
- modified
- stages
- commited
- you can initialize repository by cloning or git init command.
- git init
- git clone <
url> - git add <
filename> - git status
- git status -s
- git status --short --> for short status
- .gitignore git ignore templates
- git status shows what files changed
- git diff shows what lines you have exactly changed.
- git diff --staged (to see what will go into your next commit)
- git diff shows changes you have not staged if you staged then this will show no output.
- git diff --cached and git diff --staged are the synonyms.
- git commit -m "message of commit"
- git commit this will open your default editor and if you write git commit -v then this will the changes you have made also.
- If you wan to skip staging area then you can pass flag with git commit command -a
- If you want to remove a file, that git no longer track that file then git rm fileName and if you have made chagnes to your file then git will not allow you, but you can pass -f flag i.e git rm fileName -f
- If you want to keep the file in your working directory and want to no tracked by git then pass --cached flag i.e git rm --cached fileName
- git log to see the all commit history of project
- git log -p which shows the difference introduced in each commit.
- git log -p -2 which shows the last 2 commits
- If you want to see the abbreviated stats you can use git log --stat
- git log --pretty=oneline shows each commit in one line with hash value and message. more available options are short, full, fuller
- git log --pretty=format:"%h - %an, %ar : %s" to format as you desires.
- Table specifiers Pretty output specifiers image
- git log --pretty=format:"%h %s" --graph
- since and --untill are very useful.
- git log --since=2.weeks
- Undo all changes made to the file and revert back to previous commit version.
git checkout - - <file_name>
- Unstaging a staged file with Git restore command
git restore --staged fileName
- Undo or discard changes using checkout command
git checkout <fileName>
- Unmodifying a Modified File with git restore
git restore fileName
- Origin is the default name given by git to the repository you have cloned from. To show all remotes use:
git remote -v
- Adding Remote Git Repositories
git remote add name http://github.com/etc
- Git fetch origin branch ( fetch the changes then we merge the branch) git pull fetch and merge both.
- git push origin master
- show origin => git remote show origin
- renaming and removing remotes
git remote rename oldName newName
git remote remove remoteBranchName
Make your own aliases and use them instead of writing long commands each time.
git config --global.co checkout
git config --global.ci commit
git config --global.br branch
- Creating new branch mean create new pointer on the same commit you'r on.
git branch branchName
branching Image 2. HEAD is the pointer which shows current branch.
git checkout branchName
- If you want to commit all changes then run - this will add and commit files (not recommended)
git commit -a -m "made a change"
- You can create and checkout branch at the same time by using following command:
git checkout -b <branchName>
- switch can be used to switch branch
git switch branchName
git switch -c branchName
Origin : the default name Git gives to the server you cloned from Merging
git checkout targetBranch
git merge sourceBranch
Delete the branch
git branch -d branchName
branch details
git branch
git branch (--merged || --no-merged)
Change Branch Name
git branch --move bad-branch-name correct-branch-name
to Push branch corrected branch name push it.
git push --set-upstream origin corrected-branch-name
Pushing when you want to know the branch to the world, you need to push the branch to the repository where you have write access.
git push <remote> <branch>
This mean take my local branch to the server and make it remote branch. tracking branches Setup Local Branch to Remote
git branch -u origin/serverfix
to check which tacking branches you setup
git branch -vv
git pull
- it fetches from remote server and merge it.
While the git fetch command will fetch all the changes on the server that you don’t have yet, it will not modify your working directory at all. It will simply get the data for you and let you merge it yourself. However, there is a command called git pull which is essentially a git fetch immediately followed by a git merge in most cases. If you have a tracking branch set up as demonstrated in the last section, either by explicitly setting it or by having it created for you by the clone or checkout commands, git pull will look up what server and branch your current branch is tracking, fetch
from that server and then try to merge in that remote branch.
Generally it’s better to simply use the fetch and merge commands explicitly as the magic of git pull can often be confusing.