Git basics to advanced

 1. Repository (Repo)

  • Concept: A repository is like a project folder where all your files and the history of changes are stored.
  • Command:
    • git init (Create a new Git repository)
    • git clone <repository-url> (Copy an existing repository to your computer)

2. Commit

  • Concept: A commit is like a save point in a game. It records a snapshot of your project.
  • Command:
    • git add <file> (Add a file to the staging area)
    • git commit -m "message" (Save changes with a message)

3. Branch

  • Concept: A branch is like a separate path where you can make changes without affecting the main project.
  • Command:
    • git branch <branch-name> (Create a new branch)
    • git checkout <branch-name> (Switch to the branch)
    • git checkout -b <branch-name> (Create and switch to a new branch)

4. Merge

  • Concept: Merging combines changes from one branch into another.
  • Command:
    • git merge <branch-name> (Merge the specified branch into the current branch)

5. Remote

  • Concept: A remote is an online version of your repository, usually hosted on a platform like GitHub.
  • Command:
    • git remote add origin <repository-url> (Link your local repository to a remote one)
    • git remote -v (View linked remotes)

6. Push

  • Concept: Pushing sends your committed changes from your local repository to the remote repository.
  • Command:
    • git push origin <branch-name> (Push changes to the remote branch)

7. Pull

  • Concept: Pulling fetches and merges changes from the remote repository to your local repository.
  • Command:
    • git pull origin <branch-name> (Pull changes from the remote branch)

8. Stage (Staging Area)

  • Concept: The staging area is like a holding area where you prepare files for a commit.
  • Command:
    • git add <file> (Add a file to the staging area)
    • git add . (Add all changes to the staging area)

9. Checkout

  • Concept: Checking out means switching to a different branch or commit.
  • Command:
    • git checkout <branch-name> (Switch to a branch)
    • git checkout <commit-id> (Switch to a specific commit)

10. HEAD

  • Concept: HEAD is a pointer to your current branch or commit.
  • Command:
    • git log (View commit history; HEAD shows the current commit)
    • git checkout HEAD^ (Move to the previous commit)

11. Conflict

  • Concept: A conflict occurs when Git can't automatically merge changes. You have to resolve it manually.
  • Command:
    • git merge <branch-name> (Try to merge)
    • Resolve conflicts manually in files, then:
    • git add <file> (Mark the conflict as resolved)
    • git commit (Commit the merge)

12. Revert

  • Concept: Reverting undoes a commit by creating a new commit that reverses the changes.
  • Command:
    • git revert <commit-id> (Undo a specific commit by making a new commit)

13. Reset

  • Concept: Resetting moves the HEAD and branch pointer to a previous commit, effectively undoing changes.
  • Command:
    • git reset --soft <commit-id> (Undo commits but keep changes staged)
    • git reset --hard <commit-id> (Undo commits and discard all changes)

14. Fork

  • Concept: Forking creates a copy of someone else's repository under your account on platforms like GitHub.
  • Command: This is usually done on GitHub, not through Git commands.

15. Pull Request (PR)

  • Concept: A pull request is a way to suggest changes to someone else's repository. It’s used for code review and collaboration.
  • Command: This is typically done on platforms like GitHub.

16. Tag

  • Concept: Tags mark specific points in the commit history, like a version release.
  • Command:
    • git tag <tag-name> (Create a new tag)
    • git tag (List all tags)
    • git push origin <tag-name> (Push a tag to the remote)

17. .gitignore

  • Concept: The .gitignore file tells Git to ignore certain files or directories.
  • Command:
    • Create a .gitignore file in your project and list the files/directories to ignore.

18. Blame

  • Concept: Blame shows who last modified each line in a file, useful for tracking changes.
  • Command:
    • git blame <file> (Show who made changes to each line in the file)

19. Cherry-pick

  • Concept: Cherry-picking lets you apply a specific commit from one branch to another.
  • Command:
    • git cherry-pick <commit-id> (Apply a specific commit to the current branch)

20. Stash

  • Concept: Stashing temporarily shelves your changes without committing them, allowing you to work on something else.
  • Command:
    • git stash (Stash changes)
    • git stash pop (Apply stashed changes)
    • git stash list (List stashes)

21. Rebase

  • Concept: Rebasing moves or combines a sequence of commits to a new base commit.
  • Command:
    • git rebase <branch-name> (Reapply commits from the current branch onto another branch)
    • git rebase -i <commit-id> (Interactive rebase, to edit commits)

22. Squash

  • Concept: Squashing combines multiple commits into one. It’s useful to clean up commit history.
  • Command:
    • git rebase -i <commit-id> (Use the interactive rebase to squash commits)

23. Submodule

  • Concept: A submodule is a repository inside another repository. It allows you to include and manage dependencies.
  • Command:
    • git submodule add <repository-url> (Add a submodule)
    • git submodule update --init (Initialize and update submodules)

24. Bisect

  • Concept: Bisect helps find the commit that introduced a bug by performing a binary search through the commit history.
  • Command:
    • git bisect start (Start the bisect process)
    • git bisect good <commit-id> (Mark a commit as good)
    • git bisect bad <commit-id> (Mark a commit as bad)
    • Follow the prompts until Git finds the problematic commit.

25. Reflog

  • Concept: Reflog keeps track of all your movements in the repository, even those not in the commit history. It's useful for recovering lost commits.
  • Command:
    • git reflog (Show a history of all actions and changes in the repository)

 

Comments