Below are few useful git commands that can help and ease our git experiences.

Git Initialization

git init
Initializes Git in current directory.
git config --list
Lists all config values of git.
git config user.name "username"
Sets user name in git.
git config core.editor vim
Sets vim as default git editor.
git config core.autocrlf true

All files from the repo (which should have LF line endings in this scenario) are converted to CRLF line endings on checkout to a Windows PC. All files are converted back to LF line endings on commit from a Windows PC.

There can be issues if we have binary files. Then better option will be to set .gitattributes for specific files

git remote add origin /path/to/central/repo/repo.git
Sets remote repository link/address to git with short name origin(default shortname of remote).

Git Remote

git remote add shortname /path/to/central/repo/repo.git
Sets remote repository link/address to git with short name shortname (default shortname of remote is origin).
git remote -v
Shows the URLs that Git has stored for the shortname to be used when reading and writing to that remote.
git remote show origin
Shows information about specified remote.
git remote rename pb paul
Renames a remote's shortname.
git remote remove paul
Removes the remote with shortname paul

Git Branch

git branch
Lists local branches
git branch -a
Lists local and remote branches
git branch -avv
Lists local and remote branches with few more detailed information
git branch -d branchname
Deletes the branch branchname.
git branch newbranchname
Creates a new branch newbranchname off current branch.
git checkout featurebranch
Switches working directory to point to featurebranch.
git checkout -b newbranchname
Creates a new branch newbranchname off current branch and switches working directory to point to branch newbranchname.
git checkout -

Switches back to previously used branch.

git merge feature-branch

Merge feature-branch to the current branch and creates a merged-branch.

We have to fire git commit -m "feature and master merged" to commit merged changes

git rebase master

Brings latest change in master to current branch

Current branch now bases on the latest changes in master branch

git add .
Stages all changed files in all directories for commit.
git add -A
Stages all changed files wheather in current directory or inside children directories for commit.
git add --renormalize
Normalize the line endings in a repository. Files which had been committed with CRLF will be committed with LF.
git diff
Show changes between commits, commit and working tree, etc.
git status
Shows working tree status the state of working directory and staging area.
git status --porcelain
Gives the output in an easy-to-parse format.
git push origin master
Pushes the changes in the current branch into remote master branch.
git push
Pushes the changes in the current branch into corresponding branch in remote.
git push --set-upstream origin new-branch-name
Create the new branch on remote and pushes the current changes in to the new branch.

We can use below in short as well.

git push -u origin new-branch-name
git push -u origin HEAD
HEAD is a reference to the top of the current branch, so it's an easy way to push to a branch of the same name on the remote. This saves you from having to type out the exact name of the branch
git pull --verbose
Pulls the changes into the current branch from corresponding branch in remote.
git pull origin master
Pulls the changes in the master branch in remote into the current branch.
git log
Shows commit logs. Output is harder to read as project grows.
git log --oneline
Shows commit logs in one line per commit. This is easier to read.
git log --graph --oneline --decorate
Shows commit logs in graphical decoration. This is easier to read.
git log --oneline -S "search text"
Find commits where search text appears in the change.
git log --pretty="%h, %an, %ad, %d %s" -5

Get user, time of commit, and other details in log output.

0fc8fca Author name Sat Sep 30 07:33:04 2023 +0530 Commit Message

%h = hash (commit hash)
%an = author name
%ad = author date
%d = ref names
%s = subject
-5 = show only last 5 lines

git log --pretty='%C(yellow)%h %C(cyan)%cd %C(Green)%cr %C(blue)%aN%C(auto)%d %C(auto)%s' -5

Get user, time of commit, and other details with colour in log output.

6c3e1a2 Sat Sep 30 07:33:04 2023 +0530 Author name lsof is a dependency now.

%h = hash (commit hash)
%cd = committer date (format respects --date= option)
%aN = author name (respects .mailmap)
%d = ref names
%s = subject
-5 = show only last 5 lines

git reflog
Shows reference logs, the records when the tips of branches and other references were updated in local repository.
git commit -m "commit message"
Commits changes with the specified message.
git commit -amend -m "Correct commit message"
Changes commit message of last commit.
Provided we have not pushed the code to remote yet.
git commit -am "commit message"
Adds and Commits the file changes with the specified message.
Add missed files to last commit.
git add .
git commit --amend --no-edit
Provided we have not pushed the code to remote yet.
git revert HEAD
Reverts the latest commit and appends a new commit with resulting reverted content at the tip of the branch.
Provided we have not pushed the code to remote yet.
git revert 6dfee0a
Reverts the commit with commit ref 6dfee0a and appends a new commit with resulting reverted content at the tip of the branch.
Provided we have not pushed the code to remote yet.

Git Work In Progress

git stash
Removes work in progress changes from your current directory and saves them for later use without committing to repo.
git stash pop
Adds latest stashed changes back to working directory.
git stash save workInProgress1
Saves the stash with a neme workInProgress1
git stash list
Lists name of saved stashes
git stash appliy 0
Applies the saved stash with the specified index number of the saved stashes list. Refer git stash list command output to get the index number.
git fetch
Fetches commits, files and refs from remote repo into your local repo theryby letting you know what everyone else has been working on and how central repo has progressed. It does not merge the changes in to your local repo.
git rebase master

Brings latest changes in master into current branch.

Current branch now bases on the latest changes in master branch.