Git basics
Commit, branch, merge and share your work. 36 commands.
- Install Git and set your nameInstall Git and tell it who you are, so your commits are signed with your name.
- Save your changes with a commitPick the files you changed and save them as one step in your project's history, with a short message.
- Create a branch and switch to itStart a side path for a new feature or fix, so the main version stays safe while you work.
- Add and update Git submodulesKeep one Git project inside another, fetch it after cloning and bring it up to date.
- Change where your repo pushes (remote URL)Point your local repo at a new GitHub address, or switch between HTTPS and SSH.
- Clean up branches deleted on GitHubRemove the leftover copies of branches that were already deleted on GitHub, so your branch list only shows branches that still exist.
- Clone a repositoryDownload your own copy of a project from GitHub or GitLab, with its full history, so you can work on it.
- Clone just one branchDownload a single branch of a big repo, without the others.
- Copy one commit to another branch (cherry-pick)Bring a single commit over from another branch, without merging the whole branch.
- Delete a branchRemove a finished branch from your machine and from GitHub.
- Find a commit by its message or by code it changedSearch your Git history by commit message, by a piece of code or by file, then open the commit you need.
- Find the commit that broke something (git bisect)Let Git jump through your history halfway at a time with git bisect until it finds the first commit that broke things.
- Fix "you have divergent branches" when you pullYour copy and GitHub both have new commits. Tell Git to put yours on top of theirs.
- Fix a merge conflictGit found two changes to the same lines and wants you to choose. Here's how to finish the merge.
- Fork a project and clone itMake your own copy of someone else's GitHub project and download it, ready to change.
- Install GitHub CLI and log inThe gh command for pull requests, issues and repos, signed in to your GitHub account.
- Merge a branch into mainBring the finished work from your branch back into the main version.
- Open a pull request from the terminalPush your branch and ask for it to be checked and merged, without leaving the terminal.
- Push and pull your changesSend your saved commits up to GitHub with push and bring everyone else's new work down with pull.
- Put changes aside for later (git stash)Save unfinished work without committing, switch to something else, then bring it back.
- Remove .DS_Store files from a Git repoTake macOS's .DS_Store files out of your repo and stop them coming back.
- Rename a branchGive a branch a new name on your machine and on GitHub.
- See the history as a tidy graphShow your commits and branches as a graph in the terminal with git log, then save it as a short command.
- See the history of one fileEvery change ever made to a file, and who last touched each line.
- See what changed before you commit (git diff)Check exactly what you changed with git diff before you commit, so nothing goes in by mistake.
- Set Git's default editorWrite commit messages in VS Code (or nano) instead of getting stuck in Vim.
- Set up a global .gitignoreStop system junk and secrets like .DS_Store and .env from ever being committed, in every repo.
- Set your Git name and email or fix a commit's authorTell Git who you are, use a different email for one project and fix the last commit's author.
- Stop Git asking for your passwordSave your GitHub login for HTTPS remotes, so push and pull stop prompting every time.
- Stop tracking a file that's now in .gitignoreAdding a file to .gitignore doesn't untrack it if Git already knew it. Remove it from Git but keep it on disk.
- Store big files with Git LFSKeep big files like videos, images and datasets out of your normal Git history with Git LFS, so cloning and pushing stay fast.
- Tag a releaseMark a commit as a version, like v1.0.0, so you can find it and release it later.
- Update GitGet the latest version of Git.
- Update your fork from the original repoPull the latest changes from the original project into your fork, so your copy doesn't fall behind.
- Work on a branch someone pushed to GitHubGet a teammate's branch onto your machine and switch to it.
- Work on two branches at once (git worktree)Open a second branch in its own folder with git worktree, so you can switch tasks without stashing or committing half-done work.