Getting Started with Git
What is Git and how do I get started using it?
What is Git?
Git is a distributed version control software. Version control is a way to save changes over time without overwriting previous versions. Being distributed means that every developer working with a Git repository has a copy of that entire repository — every commit, every branch, every file.
A few key things to know before you start:
- Branches are lightweight and cheap, so it's OK to have many of them.
- Git stores changes in SHA hashes, which compress text files well. This makes Git a good fit for software code, but not ideal for binary files like images or videos.
- Git repositories can be connected, so you can work locally and sync with a shared remote repository to collaborate with others.
Why use Git?
Without version control you risk losing your work. Git lets you commit (save) as often as you like and go back to any previous commit. Key advantages include:
- Speed — SHA compression makes Git very fast.
- Merge conflicts — Multiple people can work on the same file at the same time.
- Cheap branches — Branches give developers a safe sandbox to make and share changes without breaking the main codebase.
- Easy roll back — Commits are immutable, so mistakes can always be reverted.
The basic Git workflow
- Create a branch — Branch off of
main. Name it after the feature or function you're working on. - Make changes and commit — Edit files in your text editor or IDE, then stage them with
git add [file]and save them withgit commit -m "descriptive commit message". - Push to the remote — Run
git pushto upload your commits. For a brand-new local branch, usegit push -u origin [branch-name]the first time. - Open a pull request — A pull request compares your feature branch to
main(or its parent branch). Open it early — not when work is finished — so teammates have visibility from the start. - Collaborate — Add more commits to the branch as needed; the pull request updates automatically.
- Merge into main — Once the pull request is approved, merge it to integrate your changes into
main, then delete the branch.
Essential commands to know
| Command | What it does |
|---|---|
git clone [url] | Download a repository including all files, branches, and commits |
git status | Show current branch, staged files, and other key info |
git branch | List local branches |
git checkout [branch-name] | Switch to a branch |
git add [file] | Stage a file for the next commit |
git commit -m "descriptive message" | Record staged changes in version history |
git pull | Fetch and merge remote changes into your local branch |
git push | Upload local commits to the remote |
git log | Browse project file history |
git remote -v | Show connected remote repositories |
You can also work through the Introduction to GitHub Learning Lab course to practice all of these steps hands-on.
Source: https://github.com/git-guides, https://github.com/github/training-kit/blob/main/git-guides/git-overview.md