It is essential for every developer to understand and use GIT version control, as it becomes a universal requirement and tool to maintain the versioning system.
Git is a widely used tool, so we have some important useful commands that we use every day.
So, here is a list of all handy commands
# to set a user name and email
Set Email : git config –global user.email ‘your_email@gmail.com’
Set Username : git config –global user.name ‘Your Name’
# basic git commands
git init : Create a new local repository
git status : to check files/folders in a git repo
# staging basics in git
git add . : stages new and modified, without delete
git add -A : stages all files
Git add -u : stages modified and deleted, without new
git commit -m “your message” : commit your code, create a snapshot
# push code to a remote folder
git remote add origin http://link-to-your-domain.com/git-folder
It will ask for a password: server’s login password
git push -u origin master
git remote remove origin (to remove remote folder)
git remote –v // to check all remote added to the repo
# To copy repo from a git repository folder :
git clone git_url
git clone git_url . (Add . To not create an extra folder in ur folder)
It will ask for a password
# git branch management
git branch branch_name
git branch
git checkout branch_name (To switch into this branch)
# to merge files from one branch to another
git merge branch_name (All changes from branch_name branch copied to master)
git branch -d login (To delete the branch from local)
git push origin –delete login (To delete branch from the remote server)
# git logs checker
git log (View all commits)
git log -2 (View latest 2 commits)
git log –oneline
git log –grep=”readme” (Find log via comment)
git log -I -G”hello” (Case insensitive scanning)
git log -i -p -G”hello”
# Scanning
Only before (git add .) to check the changes
git diff filename
git reset filename (to unstage a file)
# Git merge conflict solution
git mergetool
# git stash
Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.
For reference: https://git-scm.com/docs/git-stash
git stash save “my_stash”
git stash list
git stash show
git stash apply
git stash apply stash@{0} (select anyone stash of multiple record)
git stash branch [] : // To convert stash to a branch
git stash clear // to remove all stashes at once
# Remove your uncommitted changes
Tracked files
git checkout -f
Untracked files
git clean -fd
# Save your changes for later
Tracked files
git stash
Tracked files and untracked files
git stash -u
Reapply your latest stash after git pull:
git stash pop
##### Can a project have multiple origins? #####
To check all: git remote -v
You can have as many remotes as you want, but you can only have one remote named “origin”. The remote called “origin” is not special in any way, except that it is the default remote created by Git when you clone an existing repository. You can configure a second remote, push to/pull from that remote, and set up some branches to track branches from that remote instead of origin.
Eg.
$ git remote add origin2 URL_to_set_as_remote
# push master to Github
$ git push origin2 master
# Push my-branch to Github and set it to track origin2/my-branch
$ git push -u origin2 my-branch
##### Some common errors and its solution #####
-
error: git upload-pack: git-pack-objects died with an error.
fatal: git upload-pack: aborting due to possible repository corruption on the remote side. Add below code to a config file in .git folder
[pack]
window = 0
-
My .gitignore file is ignored by git and it does not work
git rm -r –cached .
git add .
git commit -m “.gitignore is now working”
-
ssh: connect to host port 22: Operation timed out fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
-
aborting due to possible repository corruption on the remote side.
Add these commands to server’s git via terminal
git config –global pack.windowMemory “100m”
git config –global pack.SizeLimit “100m”
git config –global pack.threads “1” -
The “fatal: refusing to merge unrelated histories” Git error
git pull origin master –allow-unrelated-histories
Follow my other blogs here