Balu
Balu Platform Engineer | Devops | Monitoring | CICD | ODD | SQL/NoSQL | ELK | AWS | Jhipster | Microservices | Agile | Scrum

Git - Basics for a Developer

Git - Basics for a Developer

Git basics is a good idea before you start using it, as a programer upto what extent you need is the question? Let's get into it


Configure git

git config helps you set your email and name for your commit, we can't change the committer names after the commit. Every contributor will have their identification on every commit.

We can configure git at two levels, at the repository level, and global

# Global Config
git config --global user.name "Your Name"
git config --global user.email yourmail@domain.tld

# Repository Config - useful when you work with multiple business emails
git config --global user.name "Your Name"
git config --global user.email yourmail@domain.com
Initialize

Initialize new local repository

git init
Clone the repository
git clone [URL_FROM_GIT_SERVER]
# Examples:
git clone git@github.com:JinnaBalu/jinnabalu.github.io.git
git clone https://github.com/JinnaBalu/jinnabalu.github.io.git
Get Latest
git pull
# OR
git pull origin [BRANCH_NAME]
git status

Check the status of the files added, modified or deleted

git status
git diff

Check the lines of code added, modified ot deleted

git diff
Commit
# Commit local changes
git add --all
git commit -am "message"
# Show commits history
git log
# Show history of the file
git log -p [file]
# Who changed what and when
git blame [file]
git remote

Check the origin repository url. Update or set remote url of the local repository

# Listing remote
git remove -v

# Switch remote URL between https - ssh
git remote set-url origin [https://[url]]
git remote set-url origin [git@git://[url]]
Branch Management
# List All branches
git branch -av
# Switch between Branches
git checkout [BRANCH_NAME]
# Create a new branch
git checkout -b [BRANCH_NAME]
# Delete Branch
git branch -d [BRANCH_NAME]
# Forceful Delete
git branch -D [BRANCH_NAME]
# Delete remote branch
git branch -dr [BRANCH_NAME]
Merge changes between branches
# 1. Merging changes from feature_branch to develop

# 1.1 Get latest from develop and merge to feature_branch
git checkout develop
git pull
git checkout feature_branch
git merge --no-ff origin develop

# Note : To be safe from conflict with develop(GOOD PRACTICE), we will resolve in feature_branch

# Merge feature_branch to develop
git checkout develop
git merge --no-ff origin feature_branch
git reset | undo

Discard changes or reset back, or rebase changes

# Discard all local changes
git reset --hard HEAD

# Discard all local changes of file
git checkout HEAD [FILE_PATH]

# Soft Reset
git reset [commit_hash] --soft

# Mixed Reset
git reset [commit_hash] --mixed
git reset [commit_hash]

# Hard Reset
git reset [commit_hash] --hard

comments powered by Disqus