DEV Community

Shaikh Al Amin
Shaikh Al Amin

Posted on

Git daily usage

Initializing a Repository:

git init: Initializes a new Git repository in your current directory.
Enter fullscreen mode Exit fullscreen mode

Cloning a Repository:

git clone <repository-url>: Creates a local copy of a remote repository on your machine.
Enter fullscreen mode Exit fullscreen mode

Working with Branches:

git branch: Lists all branches in the repository.

git branch <branch-name>: Creates a new branch.

git checkout <branch-name>: Switches to the specified branch.

git checkout -b <branch-name>: Creates a new branch and switches to it.
Enter fullscreen mode Exit fullscreen mode

Committing Changes:

git status: Shows the current status of your repository.

git add <file>: Adds a file to the staging area for the next commit.

git add . or git add -A: Adds all modified and new files to the staging area.

git commit -m "Commit message": Commits the staged changes with a descriptive message.
Enter fullscreen mode Exit fullscreen mode

**
Synchronizing with Remote Repository:**

git push: Pushes local commits to a remote repository.

git pull: Fetches changes from a remote repository and merges them into the current branch.

git fetch: Fetches changes from a remote repository but does not merge them.
Enter fullscreen mode Exit fullscreen mode

Inspecting History and Changes:

git log: Shows the commit history.

git diff: Displays changes between the working directory and the staging area.

git diff --staged or git diff --cached: Displays changes between the staging area and the last commit.
Enter fullscreen mode Exit fullscreen mode

Setting Up multiple Git accounts:

cd ~/.ssh

ssh-keygen -t ed25519 -C "alamin.cse15@gmail.com" -f "bitbucket_office_shaikh"
ssh-keygen -t ed25519 -C "alamin.cse15@gmail.com" -f "github_personal_shaikh"

Add SSH keys to your respective repository:
Like Bitbucket and github
Enter fullscreen mode Exit fullscreen mode

For cloning from GitHub you will have to change your GitHub repo URL like the below:

git clone git@github.com-{username}:{organization name}/git-practice.git

git clone git@github.com-shaikhalamin:shaikhalamin/git-practice.git

And from Bitbucket like the following:


git clone git@bitbucket.org-shaikh-alamin:liberatelabs/happy-backend.git

Or update the current repo origin:


git remote set-url origin git@bitbucket.org-{username}:company/repo_name.git
Enter fullscreen mode Exit fullscreen mode

Setting up project local git config for a specific project:


git config user.email "alamin.cse15@gmail.com"  
git config user.name "Shaikh Al Amin"

git config user.email "amin@liberate-labs.com"  
git config user.name "Shaikh Al Amin"
Enter fullscreen mode Exit fullscreen mode

*Create a config file inside the .ssh directory and change it accordingly : *

touch config


#tikweb account
Host bitbucket.org-{username}
HostName bitbucket.org
User git
IdentityFile ~/.ssh/bitbucket_office_shaikh

#personal account
Host github.com-shaikhalamin
HostName github.com
User git
IdentityFile ~/.ssh/github_personal_shaikh
Enter fullscreen mode Exit fullscreen mode

*Git branching style: *

Checkout your current master branch :

Create two other branches:


dev
staging
Enter fullscreen mode Exit fullscreen mode

From the dev branch create a branch using your ticket number like the following for your current assigned task:


git checkout -b WEDB-1299

Git adding and committing style:

git add index.js
git add user-list.js

git commit -m “[WEDB-1299] User list added“

git add db.js
git add models/users.js

git commit -m “[WEDB-1299] user model and migration added“
Enter fullscreen mode Exit fullscreen mode

Pushing the changes to the remote branch :

git push --set-upstream origin WEDB-1299
Enter fullscreen mode Exit fullscreen mode

Prepare commit before pull request:

Merge multiple commits into a single commit before the pull the request. we will use reset master instead of interactive rebase because of reducing the complexity

git config pull.rebase false
git checkout master
git pull origin master

git checkout branch

git pull origin master

git reset $(git merge-base master CURRENT_WROKING_BRANCH)

git add changes …
git commit -am “ticket-name commit message”
git push -f
Enter fullscreen mode Exit fullscreen mode

Create a pull request into the dev branch

Other command details like:


git show <hash>
git diff 
    --> branch 
    --> file
    --> commit

git checkout
  --> branch 
  --> file
  --> hash

git reset 
    git reset --soft HEAD~1

git reflog
    git reset --hard e548764
Enter fullscreen mode Exit fullscreen mode

Resolve conflict using IDE:
1. VSCODE
2. Extension is Gitlense [VS Code]

Top comments (0)