Git & GitHub Useful Commands


<<Git-&-GitHub-Useful-Commands>>

  • git --version
  • git config --global user.name "Abhi Bhagat"
  • git config --global user.email "abc.gmail.com"
  • git config --list
  • git init
  • git clone "http.."
  • git commit -m "descriptive message"
  • git push
  • git status
  • git log --oneline
  • ls -lrt

<git workflow>

  1.  Make changes to your files.
  2.  Check what changed > git status
  3.  git add . 
  4. git commit -m "descriptive message"
  5.  git log --oneline
                  
  • echo "Hello world" > index.html
  • cat index.html

<git stages>

  1. untracked (nothing to commit, working tree clean)
  2. modified  (in red.. modified file_name)
  3. staged    (in green.. modified file_name)
  4. committed ( ..1 file changed, 1 insertion(+)..)
  • git restore --staged <file_name> (to unstage a file)
  • git branch -v
  • git branch <branch_name>
  • git checkout <branch_name>
   or
  • git switch -c <branch_name>
  • git switch master

  • git merge <branch_name>
  • git branch -d <branch_name>
  • git remote -v
  • git remote add origin(or any_word)  <url>
  • git push -u origin master (for first time use)
  • git push

  • git pull 
  • git fetch (pull but not merging)
  • git remote remove origin

<git pull request life cycle>

  • . Go to GitHub > Pull requests > new pull request
 >  select branch from dropdown > create pull request(add descriptions)
------
((on review side)
  1.  go to Pull requests > file changed > select line..start review)
  2.  Review changes "all looks good" > submit
---------
  • Merge pull request > (add description..) Confirm merge  
  • > check on the <>Code..pull request.. (check for closed)

[for Merge]   [For Rebase]
---------------|------------------------
git add .       |  git add .
git commit   |  git rebase --continue

To abort:-
Merge: git merge --abort
Rebase: git rebase --abort

  • git stash                                            (to save work)
  • git stash save "working on login feature"            ("  " with msg)
  • git stash list                                       (to see all stashes)
  • git stash pop                                        (restore latest stash)
  • git stash apply                                      (restore but keep stash)

<git stash workflow>

  • git add .
  • git stash save "half-finished login form"
  • git switch main
  • git switch feature-login
  • git stash pop

  • git stash apply stash@{1}                           (Apply a specific stash)
  • git stash drop stash@{1}                            (Delete a specific stash)


  • git tag v1.0                                          (just a label -lightweight tag)
  • git tag -a v1.0 -m "Release version 1.0               (Annotate tag  include details)
                    -Initial launch"                                      
  • git tag -a v0.9 abc123 -m "Beta release"              (tag a specific commit)

<working with tags>

  • git tag                                               (list all tags)
  • git show v1.0                                         (see tag details)
  • git push origin v1.0                                  (push tags to GitHub)
  • git push origin --tags                                (push all tags)
  • git tag -d v1.0                                       (Delete a tag)

<Semantic versioning for tags>

[Format:- Major.Minor.Patch (eg. v2.3.1)
  • Major- Breaking changes- not backwards compatible
  • Minor- New feature- backward compatible
  • Bug fixes- backwards compatible
Examples:
  • v1.0.0 - Initial release
  • v1.1.0 -  Added new feature
  • v1.1.1 - Fixed a bug
  • v2.0.0 - Major rewrite, breaking changes

<Creating a Release on GitHub>

  1.  Go to GitHub repo
  2. Click "Release"-> "Create a new release" 
  3.  Choose an existing/new tag
  4. Write a release title (eg. "version 1.0 - Initial Launch")
  5. Write release notes (What's new, bug fixes, breaking changes)
  6.  (optional) Attach any file users need 
  7.  Click "Publish release"

  • git branch backup-branch-name        (backup before experimenting)
  • git checkout --file_name.txt          
    or                               (discard changes in one file)
  • git restore file_name.txt
  • git restore .                        (discard ALL uncommitted changes)

  • git commit --amend -m  "Better commit message"  (will not commit, just change the message)

  • git reset    (moves the pointer backward, rewrite history, use for local, unpushed commits)
  • git revert   (create new commit for changes, preserve history)

  • Rule: use  'Reset' for 'Local'   &  'Revert' for 'Public'

Git Reset 3 types:
  1. Soft  (keep changes staged):              git reset --soft HEAD~1
  2. Mixed (keep changes unstaged) -default:   git reset HEAD~1
  3. Hard  (delete everything):                git reset --hard HEAD~1
        (* HEAD~1 means "go back 1 commit")
  • git revert HEAD   (revert the last commit)
  • git revert abc123 (revert specific commit)

<Common Git Error messages>

  1. "Your branch is ahead of origin/main"  -> git push (local commits to upload)
  2. "Your branch is behind origin/main     -> git pull (other pushed, need to download)
  3. "Changes not staged for commit"        -> git add . > git commit (forgot to stage)
  4. "Fatal: not a git repository"          -> cd into the right folder/ run git init

  • git reflog               (looks everything you did)
  • git checkout abc123      (recover a lost commit)

<.gitignore> 

  1.  A special file that tells Git what NOT to track
  2. Lists patterns of file/folders to ignore
  3.  Lives in project's root directory
  4. Create it once, benefits forever (WORA)
  5.  One of the first files to create

<Creating the file>

  • touch .gitignore       (for Linux/Mac)
  • type nul > .gitignore  (for Windows)

<Common files/folders to ignore:>

# Dependencies

  • node_modules/
  • vendor/
  • *.gem

# Environment variables & secrets

  • .env
  • .env.local
  • config/secrets.yml

# Build outputs

  • dist/
  • build/
  • *.exe

# OS files

  • .DS_Store
  • Thumbs.db

# IDE files

  • .vscode/
  • .idea/
  • *.swp
https://github.com/github/gitignore/

<Commit Message Best Practices>

Type: 
Short description (50 chars or less)
Longer Explanation if needed (wrap at 72 chars)
  • What changed
  • Why it changed
  • Any side effects

Common types:

  • feat: New feature
  • fix:  Bug fix
  • docs: Documentation changes
  • refactor: Code restructuring 
  • test: Adding tests

  • Credits: DheerajTechInsight
  • Full video playlist: https://youtube.com/playlist?list=PLz8JBMMd7yjUsgrHpSV1r3oIvkVef-do4&si=LzLZfnE1mP9G_b3A
----------------------------------------------------------------------------------------

Comments