Git

Git Commands

  1. git symbolic-ref HEAD
  2. git log –oneline –graph –decorate –all
  3. git –version
  4. git config –list
  5. git config –global user.name “Hello World”
  6. git config –global user.emao “hello@world.com”

  1. clear
  2. git config user.email
  3. git help
  4. git help commit

Linux Commands

  1. pwd
  2. cd ~
  3. cd ..
  4. ls
  5. ls -l
  6. ls -lrth
  7. cd folder

Git Basic Commands

  1. git init
  2. ls -la
  3. git status
  4. git add filename.ext
  5. git add .
  6. git commit -m “Take snapshot till this point”
  7. git commit -am “add and commit at the same time”
  8. git log
  9. git log –author=”Hello World”
  10. git log –oneline

  • working copy >> staging area >> repository

  1. git diff
  2. git diff filename.ext
  3. git diff –staged

  1. git rm filename.ext

Rename file

Way 1

  1. git add newfilename.ext
  2. git rm oldfilename.ext

Way 2

  1. git mv oldfilename.ext newfilename.ext

Undo changes in file (copy repo file to local file)

  1. git checkout — filename.ext
  2. 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>
      

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>.

  • 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>
      
  • 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.