Here are some basic Git commands to ease your life as a developer.
Git Basics
- View Git configurations
git config --list
- Set user name
git config --global user.name "example name"
- Set user email
git config --global user.email "example email"
- Initialize an empty git repository
git init
- Get the status of changes in files.
git status
- Display the entire commit history
git log
- Get the difference between the working directory and the staging area
git diff
- Show the difference between the working directory and the last committed change
git diff HEAD
Handling file changes
- Add a specific file to the staging area
git add <file-name>
- Add all changed files to the staging area
git add .
- Commit the staged changes with a commit message
git commit -m "Commit Message"
- Remove a specific file from the staging area
git rm <file-name>
- Revert a commit with a new commit
git revert <commit-hash>
- Stash current changes
git stash
- Apply stashed changes
git stash apply
- Reset your last commit and keep the changes
git reset --soft HEAD~1
Handling branches
- Create a new branch from the current branch
git branch <branch-name>
- Switch to a different branch
git checkout <branch-name>
- Create a new branch and switch to it
git checkout -b <branch-name>
- List down all branches
git branch
- Merge the branch with the current branch
git merge <branch-name>
Working with Remote
- Clone an existing git repository
git clone <repo-url>
- Connect your local git repository to a remote repository and assign it a name
git remote add origin <url>
- Pull changes from a remote branch
git pull origin <branch-name>
- Fetch changes from a remote branch
git fetch origin <branch-name>
- Push committed changes to a remote branch
git push origin <branch-name>
- Push all the local branches to remote
git push origin --all
- Pull a remote branch and create a local branch for it
git checkout -b <local-branch-name> origin/<remote-branch-name>
- Rebase the current branch with a specific branch
git rebase <branch-name>
- Interactively Rebase the current branch with a specific branch
git rebase <branch-name>
- Apply changes from a certain commit in your branch
git cherry-pick <commit-hash>
Top comments (0)