GIT

Installing git in Ubuntu
sudo apt update && sudo apt install -y git

Configure git
git config –global user.email “jegan@tektutor.org”
git config –global user.name “Jeganathan Swaminathan”
git config –global core.editor vim

Checking the git configurations
git config –list

Create an empty git repository locally
git init

Staging files for commit
git add file1.txt
git add *
git add .
git status

Commit files locally
git commit -m “Initial commit.”
git status

Checking Commit history
git log
git log -p -2
git log
git log –stat
git log –pretty=oneline
git log –pretty=format=”%h %s” –graph
git log –pretty=oneline –graph
git log –since=3.weeks

Checking the status of your files
git status
git status -s

Checking diff
git diff
git diff –staged
git diff –cached

Renaming a file in git
git mv current_name.txt new_name.txt
git status

Removing files from git
git rm file1.txt
git rm –cached file1.txt
git status

Unstaging a Staged file
git reset HEAD file1.txt
git status

Discarding changes done to a file
git checkout — file1.txt
git status

Ignoring certain types of files from getting committed accidentally
echo “.log” >> .gitignore echo “.class” >> .gitignore
echo “target/*/” >> .gitignore
echo “*.txt” >> .gitignore

Undoing the most recent commit
git add new_file
git commit –amend
git status

Let’s assume you are currently working on a medium priority bug fix but the fix is in progress, suddenly you get a show-stopper critical issue. In this situation, you can’t commit your current changes done for the medium priority bug fix as it isn’t complete yet, at the same time you can’t discard your changes as you would have put many hours of effort.

This is where git stash helps you park your current code changes in the repository without committing them. Once you have stashed your changes done part of the medium priority issue, your repository will look clean, allowing you to focus on the critical issue. Once you are done with a critical fix, you can resume your work on the medium priority issue. By the way, there can be many stashes and stashes work like a Stack as the most recently stashed changes appear in the top.

Stashing code changes
git add *
git stash save “Bug1234 – In Progress”

Listing stashes in your repository
git stash list

Resuming back to a stashed changes
git apply stash@{0}
But git apply stash@{0} wouldn’t remove the stash from the repository as it would expect you to drop that from the repository manually.

Removing a stash from the repository explicitly
git stash drop stash@{1}

Git Pop will apply the topmost stash and remove it from the repository in a single step
git stash pop

Delete all stashes at one shot
git stash clear

Creating a git branch
git branch your-branch-name

Switching to a branch
git checkout my-branch

Creating a new branch and switching to it at one-shot
git checkout -b my-branch

Merging changes from feature-branch to master branch
git init
echo “VW Polo” >> cars.txt
git add cars.txt
git commit -m “Added Polo”
echo “BMW X3” >> cars.txt
git add cars.txt
git commit -m “Added X3”
git branch new-feature
echo “Audi Q3” >> cars.txt
git add cars.txt
git commit -m “Added Q3”
echo “Skoda Kodiaq” >> cars.txt
git add cars.txt
git commit -m “Added Kodiaq”
git checkout master
git merge new-feature

Deleting a branch
git branch -d my-branch

Clone a remote git repository
git clone https://github.com/tektutor/centos-sshd-passworldless.git

Pushing changes from local to remote repository
git init
echo “Toyota Crysta” >> cars.txt
git add cars.txt
git commit -m “Initial commit.”

Assuming you have created a GitHub public repository

git remote add origin https://github.com/tektutor/cars.git

This pushes the local master branch to remote repository referred as the origin

git push -u origin master

Pulling delta changes from GitHub to Local Repository
git clone https://github.com/tektutor/cars.txt
cd cars
git pull
git clone https://github.com/tektutor/cars.txt
cd cars
git pull