Git
Git

Git

 

Table of Contents



♥️
Pushing to existing repo from other folder
 
  • Commit messages ‣
  • Git Branch naming ‣
 
 

Basic commands


  • GitHub platform for storing , tracking and collaboration ( cloud)
  • git show SHA shows all about that commit
  • git log --oneline shows all the commit logs in on line with short id name
    • git log -n “number” gives that many number of commits
  • git reset filename removes from the staging area
  • git blame shows the history of everything about a file
  • git commit —ammend -m “XYZ”
    • edit the previous commit
  • HEAD is the latest commit thingy
  • Reverting is nothing but putting the HEAD at a certain commit
  • if we do like this then we loose the history like we can go to a particular commit and loose the history
    • git reset --hard sha
  • IF we do reverting then we have a new commit and all the history too
    • git revert id
 
  • stash your local changes before pulling to your local machine

Git diff


git diff --staged
  • shows the difference in the staged file before and after
  • You can also check the difference between two commits
git diff hash1 hash2 -- or git diff hash1..hash2

Branching


  • default branch is main aka (master)
  • git log --oneline --graph --all
    • shows graph of branches and merging thingy.
  • creating
    • git branch 'xyz' 'or' git checkout -b "abc" "OR" git switch -c "XYZ" Creates a new branch and switches to it automatically
  • viewing all branches
    • git branch
  • Moving to desired branch
    • git checkout "xyz"
  • pushing
    • git push --set-upstream "xyz"
       
  • PR
    • Method 1
      • git checkout main git merge origin/xyz git push
    • method 2
      • make a pr in github
      • to → from
      • Merge it
      • checkout main in CLI
      • pull
  • Naming conventions of branches
    • wip Works in progress; stuff I know won't be finished soon feat Feature I'm adding or expanding bug Bug fix or experiment junk Throwaway branch created to experiment

Reflog


 record when the tips of branches and other references were updated in the local repository. 
  • read as ref-log or reference logs
  • Private record of commits HEAD pointer
  • Maintains commit history and also the exact actions are recorded
 

Merge Conflict


  • if there are changes differently in single file with multiple branches and if you try to merge a merge-conflict arises
  • If you just want to abort the merge and stay in your branch
git merge --abort
  • Merging branch
# you're on the "ours" branch, the "theirs" branch is "their_branch" git merge their_branch
 

Rebase and Merge


  • Rebase and merge - both are used to take commits from a feature/fix branch to main branch
  • Rebase - moves the feature branch commits on top of the main branch. ( duplicates the commits from feature branch as master branch commits). Rewrites history.
    • Merge commit free history.
    • Don’t run in the main branch
    • notion image
  • MERGE - merge makes a merge commit after the feature branch is done into the main
notion image
  • Drawbacks
    • We can’t track commits
    • Doesn’t work with pull requests

Squash


  • Combining multiple commits into one commits. To make git history look tidier and easier to read.
  • Suppose i have a feature branch which has too many commits and i can squash it into one commit.
  • It’s a part of interactive rebase feature.
    • moves to interactive mode and allows to squash commits, reword commit messages, add files, drop commits.
notion image
notion image

Git worktree


  • It’s like having multiple instances of a project.
  • To check the active worktrees
    • git worktree list
  • Creating a worktree of the main branch
    • Highly helpful for pushing hotfixes and pulling instead of switching around.
    • recommended to create outside the rootDir of the project.
    • git worktree add ../project-hotfix main
  • removing the worktree
    • # go to the worktree directory you are working in and do git worktree remove .(path)
Â