Git Basic
Installing Git
- Windows
- Mac
- Linux
- RPM based
Download from git-scm and Install.
Install with homebrew package manager
brew install git
For debian based like ubuntu
sudo apt install git-all
For RPM based os like fedora or cent-os
sudo dnf install git-all
Create a new repository
create a new folder and then
git init
Clone the repository
https method
git clone [url of repo] ./
ssh method
git clone username@host:user/reponame ./
Add & Commit
Add sepecifc file/s
to change index
git add [filename]
Add all changed files
to change index
git add .
Commit the message
for changes in files
git commit -m "Commit message"
Pushing changes
Push local head changes
to remote master
git push origin master
Add remote server to repo
git remote add origin [repo url]
Branching
Create
new branch from local head (current state of changes)
git checkout -b [new branch name]
Switch
back to master or other branch
git checkout master
Delete
the branch with merged changes (safe delete)
git branch -d [branch name]
the branch with unmerged changes (force delete)
git branch -D [branch name]
Push changes
git push origin [branch name]
Merge and update
Update
the local repo to latest commit from remote
git pull
Merge
other branch into current branch
git merge [branch name]
Check diff
between two branches
git diff [source branch] [target branch]
Logs
All log
git log
Commit log by author
git log --author=[username]
Log for changed files
git log --name-status
Tags
Add tag (locally)
to specific commit
git tag [tag name] [commmit]
Delete tag (locally)
git tag --delete [tag name]
Push specific tag to remote
git push origin [tag name]
Push all local tags
to remote
git push origin --tags
Create empty branch without history (no previous commits)
git checkout --orphan [new branch name]
Reset
Reset/Rollback to specific commit
git reset --hard <commit-hash>
or
git reset --hard HEAD~n
replace n with last n commits
git push -f origin master
Tweaks
Showing colorful output
of command results (works only in local repo.)
git config color.ui true
Auto setup remote when pushing to new branch
git config --global --add --bool push.autoSetupRemote true
Open repository in browser
Prerequisites
Before proceeding with the Git commands and aliases in this guide, ensure you have the following installed:
- GitHub CLI (gh): Required for some advanced features like opening repositories in the browser.
To install GitHub CLI, visit the official website: https://cli.github.com/
Follow the installation instructions for your operating system. After installation, authenticate with your GitHub account by running:
gh auth login
Add a global alias to open the current repository in the browser:
git config --global alias.open '!gh repo view -w'
Usage:
git open
This command will open the current repository's page in your default web browser. Note that this requires the GitHub CLI (gh) to be installed and authenticated.