Git and GitHub, Explained
If you are reading this, you have probably seen git clone, "open a pull request" or "it is on GitHub" many times, and you want to understand what they mean, use the commands with confidence, and avoid the mistakes that leak passwords or lose work.
By the end you will be able to explain what Git and GitHub are and how they differ, create a repository and record changes, work with branches and merge them, share work through a remote, undo mistakes safely, and build a small project from start to finish. You will also know the main security risks in everyday Git use (secrets in history, weak accounts, forged commit authors, unprotected branches, risky automation) and how to reduce each one. It builds on Linux Fundamentals, because you will type commands in a terminal.
1. What Git and GitHub are
Git is version control software. It records the history of a project's files so you can see what changed, go back, and work on several versions at once. GitHub is a website and service that hosts Git repositories and adds collaboration features. GitHub's own quickstart describes Git as "the version control software that GitHub is built on".
| Git | GitHub | |
|---|---|---|
| What it is | A program on your computer | A hosted service on the web |
| Where it works | Mostly offline, on your own machine | Online |
| What it adds | History, branches, merging | Pull requests, issues, forks, access control, security scanning |
| Alternatives | Other version control systems | Other hosts, such as GitLab |
A repository (repo) is a project folder plus its full history. On GitHub, a repository is "a place where you can store your code, your files, and each file's revision history", and it can be public or private.
How Git thinks
The Pro Git book, which is the free official guide, describes four ideas that explain most of Git's behaviour:
- Snapshots, not differences. Each time you commit, Git "basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot". If a file has not changed, Git stores a link to the previous identical file.
- Nearly every operation is local. You have the whole history on your own disk, so you can browse it and commit offline.
- Git has integrity. "Everything in Git is checksummed before it is stored and is then referred to by that checksum", using a SHA-1 hash, which is a 40-character string of hexadecimal characters. Those strings are the commit IDs you see everywhere.
- Git generally only adds data. After you commit a snapshot, it is "very difficult to lose", especially if you push to another repository. The book adds that you can lose changes you have not yet committed.
The three places your files live
Git has three states for a file, and three matching areas:
| State | Area | Meaning |
|---|---|---|
| Modified | Working tree | You changed the file but have not marked it for the next commit |
| Staged | Staging area (also called the index) | You marked the current version to go into the next commit |
| Committed | Git directory (the .git folder) |
The snapshot is stored safely in your local database |
The basic workflow is: change files, stage the changes you want, then commit them. The staging area lets you build a commit from just some of your changes.
2. Install and set up Git
Check whether Git is already installed
Open a terminal (on Windows, a terminal such as PowerShell or Command Prompt) and run:
git --version
If a version number prints, Git is installed. The Pro Git book still advises that "even if it's already installed, it's probably a good idea to update to the latest version". If you see "command not found" or a similar message, install it using the steps for your system below.
Windows
| Option | How |
|---|---|
| Official installer | Download Git for Windows from the Git website, https://git-scm.com/install/windows. The book calls this "the most official build". |
| winget | winget install --id Git.Git -e --source winget (from Command Prompt or PowerShell) |
Git for Windows is "a project called Git for Windows, which is separate from Git itself". It installs Git Bash, a Bash emulation that behaves like the git command in Linux and Unix environments, as well as Git GUI, right-click shell integration and Git Credential Manager, which provides secure credential storage for GitHub and other hosts. The commands in this writeup are written for a Bash shell, so on Windows use Git Bash to follow them exactly.
macOS
The Git website lists three ways:
| Option | Command |
|---|---|
| Xcode Command Line Tools (Apple ships Git in them) | xcode-select --install |
| Homebrew (install Homebrew first) | brew install git |
| MacPorts (install MacPorts first) | sudo port install git |
The book adds that on a Mac that does not have Git yet, simply running git --version in the Terminal prompts you to install the command line tools. The Git website notes that the macOS options are provided by third parties and "may not be up to date with the latest source release".
Linux
Use your distribution's package manager (see Linux Fundamentals). Git's website lists these commands, which need administrator rights, so put sudo in front:
| Distribution | Command |
|---|---|
| Debian, Ubuntu | sudo apt install git |
| Fedora (22 and later) | sudo dnf install git |
| Arch Linux | sudo pacman -S git |
| openSUSE | sudo zypper install git |
| Alpine | sudo apk add git |
Git's website warns that "RHEL and derivatives typically ship older versions of git". It also lists a personal package archive (PPA) for newer Ubuntu versions; that is a third-party source, so adding one is a decision to trust its maintainers. You can also build Git from source: the book notes that the kernel.org download page "also has release signatures if you want to verify your download".
Install from the right place
Download Git only from the official Git website, from your system's own package manager, or from a package manager you already trust. Do not use a link from an advertisement or message: a fake installer is a standard way to deliver a trojan (see How Malware Works).
Prefer a window to a terminal? GitHub also offers GitHub Desktop, and GitHub CLI for using GitHub from the command line. This writeup uses the plain git command, because every tool is built on it.
After installing, run git --version again to confirm. If the command is still not found, close and reopen the terminal and try once more.
Set up Git
Git reads settings from three places, and each overrides the one before it: a system-wide file, your own ~/.gitconfig (with --global), and the repository's .git/config (the default, or --local). To see every setting and where it comes from:
git config --list --show-origin
Set your identity once. Every commit records it:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
By default git init creates a branch called master. From Git 2.28 you can choose another name, and main is what GitHub uses for new repositories.
Security note: Git does not check who you say you are. The name and email are just text you type. The book warns that Git "is cryptographically secure, but it's not foolproof", and describes signing as the way to verify "that commits are actually from a trusted source". Section 10 covers it.
GitHub lets you use its noreply email address as your commit email instead of a personal one. Your commit email is stored in every commit you push, so think about which address you use.
3. Your first repository
mkdir hello-git
cd hello-git
git init -b main
git init creates a hidden .git directory holding the repository's objects and references. The -b main option (long form --initial-branch) names the first branch.
Create a file and look at the state of the repository:
echo "# Hello Git" > README.md
git status
git status shows README.md as untracked. Stage it and commit:
git add README.md
git commit -m "Add README"
The message after -m describes why the change was made. Now edit the file and look at what changed before you stage anything:
echo "My first Git project." >> README.md
git diff # changes not yet staged
git add README.md
git diff --staged # changes that are staged for the next commit
git commit -m "Describe the project"
git log --oneline # one line per commit
--staged is a synonym of --cached in git diff. git status -s gives a compact summary: ?? means untracked, A means added to the staging area, M means modified.
Habit worth building: run
git statusandgit diff --stagedbefore every commit. The book warns thatgit commit -a, which stages all changed tracked files for you, "is convenient, but be careful; sometimes this flag will cause you to include unwanted changes". Naming the files you add (git add README.md) is safer than adding everything.
Telling Git what to ignore
A .gitignore file lists patterns for files Git should leave alone, such as logs, build output and files holding secrets:
.env
*.log
node_modules/
Two facts matter for security. First, "files already tracked by Git are not affected" by .gitignore; adding a pattern does not remove a file that is already committed. Second, .gitignore keeps files out of future commits, but it does not clean the past.
4. The commands you will use every day
| Task | Command | Notes |
|---|---|---|
| Start a repository | git init -b main |
Creates .git |
| Copy a repository | git clone <url> |
Adds a remote called origin |
| See the state | git status / git status -s |
Tells you the next step too |
| Stage changes | git add <file> |
Name files; avoid adding everything blindly |
| Commit | git commit -m "message" |
Saves the staged snapshot locally |
| See changes | git diff / git diff --staged |
Unstaged / staged |
| View history | git log --oneline --decorate --graph --all |
Shows branches as a graph |
| Search history | git log -S "text" |
Commits that changed how often a string appears |
| History of one file | git log -p -- path/to/file |
Each change to that file |
| Branch | git branch <name> / git switch -c <name> |
The first only creates; the second creates and switches |
| Switch | git switch <branch> |
Updates your files to that branch |
| Merge | git merge <branch> |
Brings that branch into the current one |
| Download only | git fetch <remote> |
Does not change your work |
| Download and merge | git pull |
Fetch plus merge |
| Upload | git push <remote> <branch> |
Shares your commits |
| Unstage | git restore --staged <file> |
Keeps your edits |
| Discard edits | git restore <file> |
Loses uncommitted edits |
| Reverse a commit safely | git revert <commit> |
Records a new commit that undoes it |
| Set aside work | git stash |
Saves your changes and returns to a clean tree |
| Stop tracking a file | git rm --cached <file> |
Keeps the file on disk |
git log -S is nicknamed the "pickaxe". The same filters work with --author, --since=2.weeks and a path after --.
5. Branches and merging
A branch "is simply a lightweight movable pointer to one of these commits". Each commit moves the current branch forward. Git keeps a special pointer called HEAD that points to the local branch you are on. The book stresses that "the 'master' branch in Git is not a special branch"; it is only the default name.
git switch -c add-greeting # create a branch and move onto it
echo "Hello, world" > greeting.txt
git add greeting.txt
git commit -m "Add greeting"
git switch main # go back
git merge add-greeting # bring the work in
git branch -d add-greeting # delete the finished branch
git log --oneline --graph --all
git branch -d refuses to delete a branch that is not fully merged; the capital -D forces it.
Merge conflicts
If you changed the same part of the same file differently on two branches, Git cannot merge automatically. It stops and says "Automatic merge failed; fix conflicts and then commit the result". The file will contain markers:
<<<<<<< HEAD
the version on the branch you are on
=======
the version on the branch you are merging
>>>>>>> add-greeting
Everything above ======= is your current branch; everything below is the other. Edit the file so it contains what you want, with all three marker lines removed, then run git add <file> to mark it resolved, and git commit. A conflict is not a sign that your repository is broken.
6. Remotes, GitHub and pull requests
A remote is a copy of the repository stored somewhere else, such as GitHub. Its default name is origin.
git remote add origin git@github.com:YOUR-USER/hello-git.git
git push -u origin main
-u (--set-upstream) adds a tracking reference, so that a later git pull and git push know where to go. You can only push to an HTTPS URL (https://github.com/user/repo.git) or an SSH URL (git@github.com:user/repo.git).
Fetch, pull and push
git fetch"only downloads the data to your local repository"; it does not merge anything.git pullfetches and then tries to merge into your current branch.git pushonly works if you have write access "and if nobody has pushed in the meantime". If someone else pushed first, your push is rejected: fetch their work, combine it with yours, and push again.
From Git 2.27 git pull warns until you set pull.rebase. git config --global pull.rebase false keeps the default (fast-forward if possible, otherwise a merge commit).
Pull requests and forks
A pull request (PR) is a proposal to merge changes from one branch into another, where people can review and discuss before merging. A fork is a separate repository that starts as a copy of another, called the upstream repository. You can fork any public repository, and a fork has its own branches, issues, pull requests and settings. The usual flow for contributing to someone else's project is: fork, create a branch, commit, push to your fork, open a pull request.
7. Undoing things safely
The book's first warning: "you can't always undo some of these undos". The table below goes from safest to riskiest.
| Situation | What to do | Risk |
|---|---|---|
| Staged the wrong file | git restore --staged <file> |
None: your edits stay |
| Typo in the last commit message, not pushed | git commit --amend |
It replaces the commit; the book says to "only amend commits that are still local" |
| A pushed commit was a mistake | git revert <commit> |
Safe: adds a new commit that reverses it |
| Throw away edits in one file | git restore <file> |
Edits are gone. The book: "Don't ever use this command unless you absolutely know that you don't want those unsaved local changes." |
| Reset everything to a commit | git reset --hard <commit> |
Overwrites files in your working tree, and may overwrite untracked files |
| Rewrite pushed history | force push | Can make the remote lose commits and cause problems for collaborators |
Two reassurances from the book: "anything that is committed in Git can almost always be recovered", even commits on deleted branches or overwritten with --amend; but "anything you lose that was never committed is likely never to be seen again". git reflog shows where your branch tips used to be, which is how you find a lost commit.
If you must force-push your own rewritten branch, git push --force-with-lease is safer than --force: it fails unless the remote branch is still what you expect, whereas a blind --force "will lose their work" if someone else added commits.
8. Keeping secrets out of Git
This is the most common serious Git mistake. A secret is any credential: a password, an API key, an access token, a private key. The CWE entry CWE-798 (use of hard-coded credentials) describes software that "contains hard-coded credentials, such as a password or cryptographic key".
Why committing a secret is a problem
Git's history is permanent by design. Deleting the file in a new commit does not remove it from the earlier commits, and anyone who cloned, forked or fetched has a copy. GitHub's documentation says that even if you rewrite your history and force-push, the commits with sensitive data "may still be accessible elsewhere": in clones or forks, "directly via their SHA-1 hashes in cached views on GitHub", and through pull requests that reference them.
For this reason the first step is not cleaning the history. GitHub's guidance is that if the sensitive data is a secret, "as a first step you need to revoke and/or rotate that secret". Once the secret no longer works, "that may be sufficient to solve your problem". OWASP's Secrets Management Cheat Sheet gives the same order for incident response: revoke exposed keys, rotate (create a new secret), delete the secret from where it was exposed, and log who had access and when.
| Step | What it means |
|---|---|
| 1. Revoke or rotate | Make the leaked secret useless, at the service that issued it |
| 2. Remove from the code | Load secrets from the environment or a secrets manager instead |
| 3. Decide about history | Rewriting is optional, slow and has side effects |
| 4. Look for misuse | Check logs for use of the secret |
Rewriting history, if you must
GitHub's guide uses git filter-repo. Git's own manual says git filter-branch has "a plethora of pitfalls" and that "its use is not recommended". The rewrite changes every affected commit ID, collaborators must clean up their clones, and, in GitHub's words, you cannot remove sensitive data from other people's clones. If you need to remove cached views and pull request references on GitHub, you contact GitHub Support.
Preventing the leak
- Add secret files to
.gitignorebefore the first commit, for example.env. - Avoid hardcoding secrets. GitHub's guide suggests environment variables or a secret management service.
- Check what you stage.
git statusandgit diff --stagedbefore every commit. - Use a pre-commit hook or scanner. GitHub's documentation names
git-secretsandgitleaksas examples of tools to run in a pre-commit hook. - Turn on secret scanning and push protection where available. Secret scanning looks through your history on all branches for known secret types. Push protection "blocks pushes that contain secrets before they reach your repository".
One caution about availability. Per GitHub's documentation, secret scanning runs automatically and for free on public repositories; for private repositories it is offered to organization-owned repositories with GitHub Secret Protection. Push protection for users is enabled by default but "stops you from pushing secrets to public repositories". So on a private personal repository you may not have these features, and your own discipline is the main protection.
9. Protect your GitHub account
An attacker who controls your account controls your code.
- Turn on two-factor authentication (2FA). GitHub began requiring it in 2023, in stages, for users who contribute code on GitHub.com, and strongly recommends it for everyone. Authentication codes come from an authenticator app (TOTP) or text message; you can add a hardware security key or other WebAuthn authenticator.
- Prefer passkeys or security keys. A passkey is a public and private key pair kept by an authenticator you control. It is bound to the website domain, so "the web browser will refuse to authenticate to a lookalike phishing website", which makes passkeys "much harder to attack than SMS or TOTP 2FA, which can be phished". See Phishing.
- Save your recovery methods. GitHub warns that if you cannot use any recovery method, "you have permanently lost access to your account".
- Review your sessions, keys and tokens from time to time and remove what you do not use.
Logging in from the command line
Password-based authentication for Git on GitHub "has been removed". You have two main options:
| Method | How it works | Notes |
|---|---|---|
| SSH key | A private key stays on your machine; the public key goes in your GitHub account | Protect the private key with a passphrase; a hardware key is stronger. GitHub deletes SSH keys not used for a year. |
| HTTPS with a token | A personal access token replaces the password | "Personal access tokens are like passwords"; prefer fine-grained tokens, and add an expiry |
To create an SSH key, GitHub's guide uses:
ssh-keygen -t ed25519 -C "your_email@example.com"
Then add the public key (.pub) to your account and test:
ssh -T git@github.com
The first time, SSH shows GitHub's host key fingerprint and asks you to confirm. Check it against the fingerprint GitHub publishes before typing yes. A success message says "You've successfully authenticated, but GitHub does not provide shell access."
GitHub recommends fine-grained personal access tokens "whenever possible" over classic ones. Fine-grained tokens can be limited to specific repositories and specific permissions. Before creating a token at all, GitHub suggests asking whether the GitHub CLI or Git Credential Manager will do instead, because they manage credentials for you.
Where your credentials end up. Git's credential helper
store"saves the credentials to a plain-text file on disk", with the downside that "your passwords are stored in cleartext in a plain file in your home directory". On macOS theosxkeychainhelper keeps them in the system keychain, and Windows' Git Credential Manager uses the Windows credential store.
10. Signed commits
GitHub explains the problem: "Git allows you to set the author of your changes and the identity of the committer", which "potentially, makes it difficult for other people to be confident that commits and tags you create were actually created by you". In other words, the name and email in a commit are text you typed. Signing uses a cryptographic key to prove who made a commit or tag. GitHub marks a commit Verified when the signature can be checked against a key in the author's account.
GitHub supports GPG, SSH and S/MIME signatures. It says SSH signatures are "the simplest to generate", and that GPG "has features that SSH does not", such as keys that can expire or be revoked. To sign with SSH (Git 2.34 or later):
git config --global gpg.format ssh
git config --global user.signingkey /PATH/TO/.SSH/KEY.PUB
git config --global commit.gpgsign true
You must also add the public key to GitHub as a signing key. GitHub notes that to use the same SSH key for authentication and signing you "need to upload it twice".
Three details to know. A commit without a signature simply has no verification status. A signature on GitHub proves the commit was signed by a key in someone's account; it does not by itself prove that the code is safe. And signing helps others trust your commits; it does not stop you committing a secret.
11. Protecting branches and reviewing changes
Pushing straight to main means any mistake, or any stolen credential, goes straight into your project. Branch protection rules add checks on important branches. By default, a rule disables force pushes and deletion of the matching branches. You can additionally require pull request reviews, passing status checks, signed commits, a linear history and more.
Three limits to know:
- Plan limits. Protected branches are available in public repositories on GitHub Free; for private repositories you need a paid plan such as GitHub Pro or Team. Rulesets, GitHub's newer system, have the same plan restrictions.
- Admins can bypass by default. "By default, the restrictions of a branch protection rule don't apply to people with admin permissions", unless you choose to apply them to administrators.
- Reviews only help if someone reads the code. A CODEOWNERS file can automatically request review from the people who own a file or folder.
12. Dependencies, automation and visibility
Dependencies
Most projects use other people's packages. Dependabot alerts tell you when a package you depend on has a known vulnerability. They are available for organization-owned and user-owned repositories. After you enable them, GitHub "immediately generates the dependency graph and creates alerts" for vulnerable dependencies it finds. Dependabot security updates can open pull requests that move you to a fixed version. See Threats and Vulnerabilities for how known vulnerabilities (CVEs) are tracked.
GitHub Actions
GitHub Actions runs automation, such as tests, when something happens in a repository. A workflow can reach secrets and write to your repository, so it needs care. GitHub's hardening guide recommends:
- Give the built-in
GITHUB_TOKENthe minimum permissions; "it's good security practice to set the default permission for theGITHUB_TOKENto read access only for repository contents". - Remember that "any user with write access to your repository has read access to all secrets configured in your repository", so use least-privilege credentials.
- Pin third-party actions to a full-length commit SHA, "currently the only way to use an action as an immutable release". GitHub explains that pinning to a SHA protects you from a bad actor adding a backdoor to the action's repository, because they "would need to generate a SHA-1 collision for a valid Git object payload".
- Do not put untrusted input directly into scripts. Values such as a pull request title can contain commands. GitHub's preferred pattern is to pass them through an environment variable instead of expanding them in the script.
- If a secret reaches a log, "delete the log and rotate the secret".
Public or private
Changing a repository from private to public means "the code will be visible to everyone who can visit GitHub.com", anyone can fork it, and the Actions history and logs will be visible to everyone. Because history goes with the repository, check the whole history for secrets before you make a repository public. Also remember that forks keep their own copy: GitHub notes that if you delete a fork, "code contributions from that fork can remain accessible to the repository network", and GitHub says that if the commit exists in any forks, "it will continue to be accessible there".
13. Reading other people's repositories
Security work often starts by reading a repository. Look for:
SECURITY.md: the project's security policy, with supported versions and how to report a vulnerability. GitHub's feature of private vulnerability reporting works only where maintainers have turned it on. See Security Research and Writing a Report That Gets Fixed.- History:
git log -p -- path/to/fileshows every change to a file, andgit log -S "text"finds when a string was added or removed. These help you understand how a bug entered the code. - Dependencies and workflows: the package manifests and the files in
.github/workflows.
Only test code you are allowed to test, and if you find a leaked credential in someone else's repository, report it through their policy; do not use it.
14. A project: build it, share it, protect it
Do this in order. Use a throwaway name and fake data. Never use a real password or token in this exercise.
Part A: local repository
mkdir notes-project
cd notes-project
git init -b main
git config user.name "Your Name"
git config user.email "you@example.com"
- Create
README.mdwith a title and one line, and a.gitignorecontaining.envand*.log. Stage both by name and commit:git add README.md .gitignore, thengit commit -m "Add README and ignore rules". - Create
notes.txtwith two lines. Commit it with a clear message. - Run
git log --onelineand confirm you have two commits.
Part B: branches and a conflict
-
Create a branch and add a third line to
notes.txt:git switch -c feature-a echo "line from feature-a" >> notes.txt git commit -am "Add line on feature-a" -
Go back to
main, and add a different third line tonotes.txt; commit it:git switch main echo "line from main" >> notes.txt git commit -am "Add line on main" -
Merge
feature-aintomain:git merge feature-a. You should see a conflict. Opennotes.txt, keep both lines, delete the three marker lines, thengit add notes.txtandgit commit. -
Run
git log --oneline --graph --allto see the shape of the history. Delete the branch withgit branch -d feature-a.
Part C: practise undoing
- Edit
notes.txt, stage it, then unstage it withgit restore --staged notes.txt. - Discard the edit with
git restore notes.txt. Notice that the change is gone. This is why the command is dangerous. - Pick a commit from
git log --onelineand rungit revert <commit>. A new commit appears that reverses it.
Part D: see why secrets are a problem (local only, fake data)
-
Make the mistake on purpose, with a fake value:
echo "API_KEY=fake-not-a-real-key" > config.txt git add config.txt git commit -m "Add config" -
"Fix" it the wrong way:
git rm config.txt, then commit. Now search the history:git log -S "fake-not-a-real-key" --oneline. It lists the commit that added the string and the commit that removed it, andgit log -pshows the secret in the first one. -
Learn the right sequence in your head: revoke the key, move secrets out of the code, add
.envto.gitignore, then decide about rewriting history. In a real incident you would revoke first.
Part E: put it on GitHub
-
On GitHub, create a new repository named
notes-project. Start with Private. Do not add a README there, since you already have one. -
Connect and push (use the URL GitHub shows you):
git remote add origin git@github.com:YOUR-USER/notes-project.git git push -u origin main -
Create a branch, push it with
git push -u origin <branch>, and open a pull request on GitHub. Review the diff, then merge it. Pull the result:git switch main, thengit pull.
Part F: secure it
- Turn on 2FA, and add a passkey or security key if you can.
- Set up an SSH key, add it to GitHub, and run
ssh -T git@github.com. - Set up SSH signing as in Section 10, make a commit, push it, and look for the Verified label.
- In the repository Settings, find Advanced Security and enable Dependabot alerts. Add a
SECURITY.mdfile. - Look at the branch protection options. If your plan allows them on this repository, require a pull request before merging to
main. If your plan does not, note that and rely on the habit of working on branches. - Before you ever make a repository public, review the whole history:
git log -pandgit log -Sfor anything that looks like a secret.
When you can do all of this without looking at the guide, you know the Git you need for the rest of the path and for security work.
Common mistakes
| Mistake | Better approach |
|---|---|
Committing .env or a key |
Ignore secret files first; if it happens, revoke and rotate before anything else |
| Believing a delete commit removes a secret | The old commits still contain it; history rewriting is optional and limited |
git add . or git commit -a without looking |
Check git status and git diff --staged; add files by name |
Using git restore or git reset --hard casually |
Uncommitted work is not recoverable; commit or stash first |
| Amending or force-pushing a shared branch | Amend only local commits; use git revert for pushed ones |
Using git filter-branch |
Git's manual recommends git filter-repo instead |
| Using a classic token with wide access | Use a fine-grained token with an expiry, or the GitHub CLI |
Storing credentials with the plain-text store helper |
Use the system keychain or Git Credential Manager |
| Trusting the author name on a commit | Names are only text; look for verified signatures |
| Pinning a third-party action to a moving tag | Pin to a full-length commit SHA |
| Making a repository public without checking history | Review the entire history first |
Pushing straight to main |
Work on branches and open pull requests |
Closing
Git records history, GitHub adds collaboration, and security comes from habits: keep secrets out, protect the account, verify who made a change, and review before merging.
Keep these points
- Git stores snapshots locally and generally only adds data; committed work can almost always be recovered, uncommitted work often cannot.
- The workflow is change, stage, commit; check what you stage with
git statusandgit diff --staged. - Branches are cheap pointers; merge conflicts are normal and resolved by editing and committing.
fetchdownloads,pulldownloads and merges,pushuploads; a rejected push means fetch and combine first.- A secret in history must be treated as compromised: revoke and rotate first, then clean up.
- Protect your account with 2FA, preferably a passkey or security key, and keep tokens narrow and short-lived.
- The name and email on a commit can be faked; signing proves origin.
- Use branch protection and reviews where your plan allows, pin third-party Actions to a commit SHA, and check history before making a repository public.