A Git quiz on version control, Git vs GitHub, commits and hashes, branches, merge conflicts, detached HEAD and git reset --hard, with each idea explained.
Can you answer these 10 Git questions? Let's get started. What is Git used for? What is a commit? What happens if you run git reset --hard? What is a detached HEAD? What are branches in Git? What causes a merge conflict?
What is a commit hash? What’s the difference between Git and GitHub What is the “main” branch usually used for? Can you use Git without GitHub? What is a Git expert? What is A) Git expert? Are you A Git expert? Comment your score.
Git keeps the full history of a project, so you can see what changed, go back to any earlier version and work alongside other people without overwriting each other. A handful of ideas carry most of that: commits, hashes, branches, merges, HEAD and the commands that rewrite your working folder.
Git is version control, GitHub is hosting
Git is a version control system. It records changes to a set of files over time, and it lets several people change those files at once and combine their work later. It has nothing to do with designing websites or running servers, and it is the wrong place to keep passwords: anything you commit stays in the history, even after you delete the file.
Git runs entirely on your own computer. git init turns a folder into a repository, and from then on you can commit, branch and look back through history with no network and no account anywhere.
mkdir notes && cd notesgit initgit add todo.txtgit commit -m "Add todo list"
GitHub is a website that hosts Git repositories. It gives your repository a shared copy on a server, plus the things a team needs around it: pull requests, code review, issues and automation. GitLab, Bitbucket and a plain server you reach over SSH do the same job. Git is the tool that tracks versions; GitHub is one place to put the result.
Because every clone holds the full history, Git is called distributed. Pushing to GitHub copies your commits to the server, and pulling copies other people's commits back. You can use Git for years without ever creating a GitHub account.
Can you answer these 10 Git questions? Let's get started. What is Git used for? What is a commit? What happens if you run git reset --hard? What is a detached HEAD? What are branches in Git? What causes a merge conflict?
What is a commit hash? What’s the difference between Git and GitHub What is the “main” branch usually used for? Can you use Git without GitHub? What is a Git expert? What is A) Git expert? Are you A Git expert? Comment your score.
A commit is a saved snapshot of your project at one moment, with a message saying why it changed, an author, a date and a pointer to the commit before it. Following those pointers backwards gives you the history.
Making one is a two-step process. git add puts changes in the staging area, and git commit saves everything staged as a new snapshot:
git add login.jsgit commit -m "Validate email on sign-in"
Committing uploads nothing: the commit stays in your local repository until you run git push. Commits make it easy to get old work back, but they don't replace a backup, since a repository that lives on one laptop dies with that laptop.
Commit hashes
Every commit gets a unique ID, its hash: by default a 40-character SHA-1 value worked out from the commit's contents, its message, its author and its parent. Most tools show a short prefix of it, such as 3f9a2c1, which is enough to tell commits apart in most repositories.
Unlike a branch name, a hash never moves: it always means the same commit. Because it is calculated from the contents, changing anything about a commit, even a typo in its message, produces a different hash. That is why commands that rewrite history, such as git commit --amend or a rebase, create new commits instead of editing old ones.
Branches are separate lines of development
A branch is a separate line of development: a place to build a feature or fix a bug without touching anyone else's work. Internally, a branch is only a movable label that points at one commit. Each new commit on the branch moves the label forward. That makes branches cheap to create, which is why teams make one for almost every change.
git switch -c add-search# work, then commit as usualgit switch maingit merge add-search
A branch is not a copy of the repository. Copying a whole repository is a clone, or a fork on GitHub. Here is how a feature branch leaves main and comes back:
Drawing the diagram…
Each dot is a commit; the last one on main is the merge.Expand it to fill the screen. Zoom with the buttons or pinch, drag to pan.
What main is for
main is the default branch in a new repository; older repositories often call it master. By convention it holds the code that is ready for production: reviewed and safe to release at any time. New work happens on other branches and joins main through a merge, usually after review in a pull request.
That is a team agreement, not something Git enforces. Hosts like GitHub let you protect main so nobody can push to it directly. How a team arranges its other branches is covered in Branching Strategies.
Merge conflicts
When you merge two branches, Git combines their changes line by line. If one branch changed a line and the other did not, Git takes the change. If both branches changed the same lines in different ways, Git cannot know which version is right, so it stops and asks you. That is a merge conflict.
The top half is your current branch, the bottom half the branch you are merging in. Edit the file to the version you want, delete the markers, then git add the file and commit to finish the merge.
Two people pushing at the same moment do not cause a conflict. Git rejects the second push until that person pulls the new commits, and only then, if both touched the same lines, does a conflict appear. Merging small changes often keeps each conflict small.
Detached HEAD
HEAD is Git's name for 'where you are now'. Normally it points at a branch, and the branch points at a commit. When you commit, the branch moves forward and HEAD goes with it.
A detached HEAD means HEAD points straight at a commit, with no branch in between. You are not on any branch. It happens when you check out a specific commit or a tag:
git checkout 3f9a2c1# You are in 'detached HEAD' state...
The repository is fine and no commits have gone anywhere: this is a normal way to look at old code. Commits you make here belong to no branch, though. Switch away and nothing points at them, so they are easy to lose. To keep them, give them a branch before you leave:
git switch -c keep-experiment
If you have already switched away, git reflog lists where HEAD has been, so you can still find the commit's hash for a while.
What git reset --hard does
git reset --hard moves your current branch to a commit and makes the staging area and your working folder match it exactly. With no commit given, it resets to the last commit, which throws away every uncommitted change to tracked files.
git reset --hard # drop all local editsgit reset --hard a17be04 # move the branch back too
Uncommitted work it discards is gone for good: Git never stored it, so not even git reflog can bring it back. Untracked files are left alone; removing those takes git clean.
Before you reach for it, check whether a gentler command does the job. git stash sets changes aside so you can get them back later. git restore <file> throws away edits to one file. To undo a commit that is already shared, git revert adds a new commit that reverses it, instead of rewriting history other people have.
Common mistakes
Committing a password or API key, then deleting the file and assuming it is gone. It is still in the history.
Treating a local commit as safe off-site. Until you push, it exists only on your machine.
Running git reset --hard with uncommitted work you meant to keep.
Committing in a detached HEAD and switching away without creating a branch.
Letting a branch drift for weeks, then facing one huge merge full of conflicts.
Key takeaways
Git tracks versions on your own machine; GitHub is one place to host the repository.
A commit is a snapshot, and its hash is a unique ID worked out from its contents.
A branch is a separate line of development, and main usually holds production-ready code.
A merge conflict means both sides changed the same lines differently.
A detached HEAD means you are not on a branch, and git reset --hard deletes uncommitted changes.