Git Guide

Git

linked In share
August 20, 2020
Guide to master GIT!

Basic Commands
Clone remote repository

git clone repo_url

Switch to the branch you want to work and build in your local

git checkout branch_name

Let’s say you created a new branch in your remote repository and need to checkout in local the first use fetch command and then checkout command

git fetch

Add all files to commit

git add .

Add specific files to commit

git add file_name

Commit all files

git commit -m “commit message"

Commit specific files

git commit -i file_name -m “commit message"

Push changes to remote repository

git push

Branch Creation in Local

Option 1: First Create a Branch and then checkout

git branch branch_name
git checkout branch_name

Option 2: Single command to create a new branch and checkout.

git checkout -b branch_name

Option 3: By providing existing branch name, we can base new branch from it

git checkout -b branch_name existing_branch

Branch Deletion

Delete local branch 

git branch -d pre-release

Delete remote branch 

git push origin --delete pre-release

Set Remote Repo
If origin is your forked remote repo, set upstream to original repo

git remote add upstream original_repo_url

Git Logs
See all commits with one line message

git log --oneline

Update commit message

git commit --amend

Cherry Pick
Cherry pick single commit

git cherry-pick #hash_commit

Cherry pick range of commits after commit A and till commit B

git cherry-pick A..B

Cherry pick range of commits including commit A and till commit B

git cherry-pick A^..B

Git Squash

Let's take an example of squashing last two commits

git rebase -i Head~2

It will give list of commits with the tag "pick", we can update one with "reword" and other commits as "squash" and save it by executing :wq

git squash commands

on the next step, update the commit message and comment remaining lines of commit messages and save it by entering :wq
Finally, force push the commit

git push -f

Git Stash

Let’s say you’re working on two different features, and you don’t want to commit them, then use git stash command to save and retrieve it whenever needed.

git stash all files

git stash

git stash with custom message

git stash push -m “commit message“ file-path-1 file-path-2

see the list of stashed commits

git stash list

git apply stashed changes

git stash apply stash@{0}

…to be continued

linked In share

Comments

Loading...

Leave a reply