Introduction to Git: A Simple Guide for New Users

Introduction to Git: A Simple Guide for New Users

ยท

2 min read

Git concepts (why you shouldn't code without version control)

1. Repository (repo)

think of a repo like a folder that tracks changes. but instead of just dumping files, git actually remembers what you've done (which is more than i can say for you).

  • example:

      git init my-project
      cd my-project
      git status
    

    now you have a git repo. congrats, you've done the bare minimum.


2. Commits (saving your screw-ups)

a commit is a snapshot of your code at a certain point in time. every time you commit, git stores what changed so you can undo things when (not if) you mess up.

  • example:

      git add myfile.txt
      git commit -m "fixed a typo (probably added more)"
    

    this saves your changes with a message explaining what you think you did.


3. Branches

branches let you experiment without destroying the main code. work on a new feature in a branch, and if itโ€™s not completely awful, merge it back.

  • example:

      git branch new-feature
      git checkout new-feature
    

    now you're in a separate timeline where you can break things in peace.


4. Merging

once your feature is "done" (half-baked but youโ€™re tired of it ๐Ÿ˜ซ๐Ÿ˜ซ๐Ÿ˜ซ), you merge it back into the main branch.

  • example:

      git checkout main
      git merge new-feature
    

    expect conflicts. lots of them.


5. Remote repositories

Git works locally, but to collaborate, you push code to a remote repo like GitHub, GitLab, or Bitbucket.

  • example:

      git remote add origin https://github.com/your-username/repo.git
      git push -u origin main
    

    Now your bad code is everyoneโ€™s problem. ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚


6. Pull requests ("please review my mess")

before merging, you open a pull request (PR) so others can tell you how bad your code is. once they approve, you merge.

  • example (on GitHub/GitLab):

    1. push your branch:

       git push origin new-feature
      
    2. go to the repo online and click "Create Pull Request."

    3. wait for your team to roast your code in the comments.


7. reverting mistakes (because you will mess up)

  • undo last commit (but keep changes):

      git reset --soft HEAD~1
    
  • undo last commit (like it never happened):

      git reset --hard HEAD~1
    
  • undo a commit properly (so history isnโ€™t destroyed):

      git revert <commit-hash>
    

Summary

  • git init โ€“ start a repo

  • git add โ€“ stage changes

  • git commit โ€“ save changes

  • git branch โ€“ create/switch branches

  • git merge โ€“ merge changes

  • git push โ€“ upload to a remote repo

  • git pull โ€“ get updates

  • git revert โ€“ undo mistakes

ย