- Initialize a Git repository:
git init
- Check the repository status:
git status
- Add changes to the staging area:
git add . or git add fileName
- Commit changes with a message:
git commit -m "message"
- View the commit history:
git log
- Compare changes between the working directory and the staging area:
git diff
- Compare changes between the staging area and the previous commit:
git diff --staged
- Directly commit changes to tracked files (skip staging):
git commit -a -m "Direct Commit"
- Remove a file and stage the deletion:
git rm filename
- Rename a file and stage the rename operation:
git mv oldname newname
- Stop tracking a file but keep it in the working directory:
git rm --cached filename
- View the commit log in a one-line format:
git log --pretty=oneline
- Unstage changes for a specific file:
git restore --staged filename
- Discard changes in the working directory for a specific file:
git checkout -- filename
- Forcefully checkout the previous commit (discard local changes):
git checkout -f
- Create a global Git alias (e.g.,
git st
forgit status
):
git config --global alias.st status
- Create a new branch and switch to it:
git checkout -b branchname
- Switch to an existing branch:
git checkout branchname
- List all branches in the repository:
git branch
- List branches that have been merged into the current branch:
git branch --merged
- Delete a branch if it has been merged into the current branch:
git branch -d branchname
- Forcefully delete a branch (without checking for merges):
git branch -D branchname
- List branches that haven't been merged into the current branch:
git branch --no-merged
- Check remote available or not
git remote
- Check from where to push and pull
git remote -v
- Remore remote branch
git push -d origin branchname
Remember to adapt these commands to your specific Git workflow and repository needs. Always use caution when performing operations that involve branch deletion or discarding changes.