Git Daily Workflow
With the repository, working directory, and staging area model in place, this section focuses on the group of commands used most often day to day: recording changes, viewing history, and working with branches.
Initializing and checking status
Section titled “Initializing and checking status”git init # turn the current directory into a Git repositorygit status # check the current state of the working directory and staging areagit status is one of the most frequently run commands — it lists which files have been modified, which are already staged, and which are new files Git isn’t tracking yet. It’s the basic way to confirm the current state before doing anything else.
Recording a change
Section titled “Recording a change”git add <file>: stage the current changes in a specific file;git add .stages every change in the current directory at oncegit diff: view the specific changes in the working directory relative to the staging area (or the staging area relative to the last commit) — a way to confirm the scope of a change before staging itgit commit -m "commit message": write the staged content into the repository, creating a new commit
The commit message isn’t a throwaway comment — it’s part of the permanent history. Tracing when and why a particular change was introduced relies mainly on these messages, so it’s worth writing something concise but specific about what the change actually does, rather than a vague “update” or “fix.”
Viewing history
Section titled “Viewing history”git log # view commit history, newest firstgit log --oneline # collapse each commit to one line, showing just the hash prefix and messagegit log -p # show the specific changes (diff) for each commitBranches: parallel sequences of commits
Section titled “Branches: parallel sequences of commits”A branch is, at its core, just a movable pointer to a commit — not a complete, independent copy of the files. Creating a new branch is nearly free, because it only creates a new pointer without copying any file content — which is why Git’s branching is noticeably lighter-weight than in many other version control tools.
git branch # list all current branchesgit branch new-feature # create a new branch without switching to itgit checkout new-feature # switch to the given branchgit checkout -b new-feature # create and immediately switch to a new branch (combines the two commands above)git switch new-feature # a newer, more purpose-specific command for switching branchesHEAD is a special internal pointer Git uses to mark “where things currently are.” Normally it points at the current branch, and the current branch’s pointer in turn points at that branch’s latest commit. Switching branches is, under the hood, just moving HEAD to point at a different branch.
Merging branches
Section titled “Merging branches”git checkout main # switch back to the target branch first (usually main)git merge new-feature # merge new-feature's commits into the current branchIf two branches modify the same part of the same file differently, git merge can’t automatically decide which change to keep, producing a merge conflict. Git marks each side’s version in the conflicting file with special markers (<<<<<<<, =======, >>>>>>>), and the file needs to be edited by hand to settle on a final version, then re-git added and git commited to complete the merge.