Git Commands
- git symbolic-ref HEAD
- git log –oneline –graph –decorate –all
- git –version
- git config –list
- git config –global user.name “Hello World”
- git config –global user.emao “hello@world.com”
- clear
- git config user.email
- git help
- git help commit
Linux Commands
- pwd
- cd ~
- cd ..
- ls
- ls -l
- ls -lrth
- cd folder
Git Basic Commands
- git init
- ls -la
- git status
- git add filename.ext
- git add .
- git commit -m “Take snapshot till this point”
- git commit -am “add and commit at the same time”
- git log
- git log –author=”Hello World”
- git log –oneline
- working copy >> staging area >> repository
- git diff
- git diff filename.ext
- git diff –staged
- git rm filename.ext
Rename file
Way 1
- git add newfilename.ext
- git rm oldfilename.ext
Way 2
- git mv oldfilename.ext newfilename.ext
Undo changes in file (copy repo file to local file)
- git checkout — filename.ext
- git reset HEAD filename.ext
- REVERTING THE CHANGES
git checkout versionnumber(few chars) — filename.ext - Pull Request
- Watch
- Star
- Fork
- Github issues and Labels
- Github Wiki
- Git Organization
Useful git commands for production:
Logs
git log –oneline
To display commit log in one line format
git diff-tree --no-commit-id --name-only -r <commit_id>
git show --pretty="" --name-only <commit_id>
git ls-tree --name-only -r <commit_id> git show --name-only <sha>
git show --stat --oneline HEAD
Branching
git branch
To show all existing branches on local machine
git checkout -b NewBranch
To create new branch from master branch
git checkout Branch
To switch from current branch to specified branch
git branch -d BranchName
To delete specific branch
git push origin :branchname
OR
git push origin –delete :branchname
To delete remote branch
Merging Changes from Another Branch to Current Branch
git checkout target-branch
Switch to target branch e.g. master or dev
git merge source-branch
Merge change from source-branch to current branch e.g. git merge CHG001
Revert Uncommit Changes
git checkout — file
To revert all uncommitted changes for specified file
git checkout — .
To revert all uncommitted changes for all files
Revert Commited Changes
git revert [-n] hashcommitcode
To revert commited changes for hashcode
Reset Pushed Changes
git reset –hard hashcommitid
To reset till specified hashcommitcode
Push after reset
git push -f origin master
To push changes forcefully to git repository
In Git, commands like reset, rebase, revert, and restore serve distinct purposes. Here’s a detailed breakdown:
1. git reset
- Purpose: Moves the HEAD pointer and optionally modifies the working directory and staging area.
- Use Case: Undo commits or unstage changes.
- Types:
- Soft: Moves HEAD, but keeps changes staged.
git reset --soft <commit>
- Mixed (default): Moves HEAD and un-stages changes, but keeps them in the working directory.
git reset <commit>
- Hard: Moves HEAD and deletes all changes in the staging area and working directory.
git reset --hard <commit>
- Soft: Moves HEAD, but keeps changes staged.
2. git rebase
- Purpose: Reapplies commits from one branch onto another in a linear fashion.
- Use Case: Keep commit history clean by avoiding merge commits.
- Types:
- Interactive Rebase:
git rebase -i <base>
Lets you squash, reorder, or edit commits.
- Regular Rebase:
git rebase <base>
Applies commits from the current branch onto
<base>
.
- Interactive Rebase:
- Key Note: Rewriting history with rebase is fine for local branches, but avoid using it on shared branches.
3. git revert
- Purpose: Creates a new commit that undoes the changes made by a previous commit.
- Use Case: Safely undo a commit without rewriting history.
- Example:
git revert <commit>
This creates a new commit that negates the changes introduced by
<commit>
. - Key Note: Unlike reset, it doesn’t alter history and is suitable for public/shared branches.
4. git restore
- Purpose: Restores working directory files or staging area files to their original state.
- Use Case: Discard changes in a file or unstage changes without affecting the commit history.
- Examples:
- Restore a file in the working directory:
git restore <file>
- Unstage changes:
git restore --staged <file>
- Restore a file in the working directory:
- Key Note: A safer alternative to
git reset
for managing uncommitted changes.
Summary Table
Command | Main Action | Scope | Safe for Shared Branches? |
---|---|---|---|
reset | Moves HEAD pointer; optionally modifies working/staging | Undo commits or unstage files | No |
rebase | Reapplies commits to a new base | Clean up commit history | No (rewrites history) |
revert | Creates a new commit that undoes changes | Undo changes safely | Yes |
restore | Resets files in working/staging area | Discard or unstage changes | Yes |
Choose the command based on whether you’re working locally or on a shared branch and whether you want to rewrite history or preserve it.