1. What does the git init command do?
The git init command creates a new Git repository. It adds a hidden .git folder to your project. Git then tracks changes in that folder.
git init
Full lesson: Git init
45 questions about Git with short answers and code. Each answer links to a full lesson on this site. Free, with no signup and no ads.
The git init command creates a new Git repository. It adds a hidden .git folder to your project. Git then tracks changes in that folder.
git init
Full lesson: Git init
The git add command puts file changes into the staging area. Git includes these changes in the next commit. You can add one file or all files.
git add [file1] [file2]
Full lesson: Git add
The git commit command moves the staging area content to the local repository. The -m option adds a short message. The message describes the change.
git commit -m "fix login bug"
Full lesson: Git commit
The git log command shows the commit history. It lists all commit information from newest to oldest. You can filter the output with options.
git log
Full lesson: Git commit history
The git checkout -b command creates a branch and switches to it. A branch is an independent line of development. Work on the branch does not affect the main line.
git checkout -b <branchname>
Full lesson: Git branch
The git diff command shows changes that are not yet staged. It compares the working directory with the staging area. Run it before you run git add.
git diff
Full lesson: Git diff
The git diff --stat option shows a summary instead of the full diff. It lists changed files and line counts. This output is short and easy to read.
git diff --stat
Full lesson: Git diff
The git clone command copies a remote repository to your computer. It downloads the files and the full history. Git also sets the remote name to origin.
git clone <repository-url>
Full lesson: Git clone
The git fetch command downloads new commits from the remote. It does not change your working files. You can review the changes before you merge.
git fetch origin
Full lesson: Git fetch
The git pull command downloads remote changes and merges them. It combines git fetch and git merge in one step. Your branch then has the latest commits.
git pull origin main
Full lesson: Git pull
The git mv command renames or moves a file. Git records the change in one step. The -f option replaces an existing target file.
git mv README README.md
Full lesson: Git mv
The git rm command deletes a file from the staging area and the working directory. Use the -f option if the file has staged changes. A plain delete leaves the change unstaged.
git rm example.txt
Full lesson: Git rm
The git range-diff command compares two commit ranges. It is useful for code review across branches or versions. You give an old range and a new range.
git range-diff <old-range> <new-range>
Full lesson: Git range diff
The commit skips your new changes. The staging area is still empty for those files. Run git add first, then run git commit.
git add .
git commit -m "add files"
Full lesson: Git basic operations
Git shows the delete as an unstaged change. The commit does not include the delete. Use git rm to stage the delete in one step.
git rm example.txt
git commit -m "remove file"
Full lesson: Git rm
Use the git push command. It uploads your local branch to the remote and merges it there.
git push origin master
Full lesson: Git push
Use git tag with the -a option to make an annotated tag. Annotated tags record the tagger and date.
git tag -a v1.0
Full lesson: Git tag
Use git show with the commit hash. It displays the commit message and the changes.
git show a1b2c3d
Full lesson: Git show
Use git mv with the old and new file names. Git moves the file and stages the rename.
git mv README README.md
Full lesson: Git mv
Use git rm with the -f option to force deletion. The -f option is necessary if the file was modified and staged.
git rm -f example.txt
Full lesson: Git rm
Git Flow is a branching model for teams. It uses master, develop, feature, release, and hotfix branches.
Full lesson: Git flow
Use the apt-get package manager. The command installs the git-flow package.
sudo apt-get install git-flow
Full lesson: Git flow
First run git fetch to download the remote changes. Then run git merge to merge them into your current branch.
git fetch origin
git merge origin/master
Full lesson: Git fetch
Use git diff with the two commit hashes. It shows the differences between those commits.
git diff a1b2c3d e4f5g6h
Full lesson: Git diff
Use git difftool instead of git diff. It opens the configured difftool for each file.
git difftool
Full lesson: Git difftool
Use git submodule add with the repository URL. It adds the submodule and records it in .gitmodules.
git submodule add https://github.com/example/lib.git
Full lesson: Git submodule
Force push can overwrite remote commits that you do not have. This can cause lost work for other team members.
git push --force origin master
Full lesson: Git push
Git refuses to delete the file and shows an error. You must use -f to force deletion.
git rm example.txt
Full lesson: Git rm
Git stops and reports that the target exists. Use the -f option to overwrite the existing file.
git mv -f old.txt new.txt
Full lesson: Git mv
Git can refuse to merge and warn about local changes. Commit or stash your changes before you pull.
git pull origin master
Full lesson: Git pull
The git clone command copies a remote repository to your computer. It copies all code and history. You get a local copy that matches the remote.
git clone https://github.com/tianqixin/example-git-test
Full lesson: Git clone
Use git log with the file path. Git shows only commits that changed that file. Add --follow to track renames.
git log --follow -- README.md
Full lesson: Git commit history
Run git diff with the file path. Git shows the unstaged changes for that file only. This helps you check your edits.
git diff app.js
Full lesson: Git diff
Run git branch with no arguments. Git prints the local branches. The current branch has a star.
git branch
Full lesson: Git branch
Use git branch -d with the branch name. Git deletes the branch only if it is merged. This protects unmerged work.
git branch -d old-feature
Full lesson: Git branch
Run git show with the tag name. Git prints the tag message and the commit it points to. This helps you check a release.
git show v1.0.0
Full lesson: Git tag
Use git config with the --global flag. Set user.name and user.email. Git uses these values in every commit.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Full lesson: Git install setup
Run git submodule status. Git prints each submodule and its current commit. A minus sign means the submodule is not initialized.
git submodule status
Full lesson: Git submodule
Run git submodule update. Git checks out the commit that the parent repository records. Add --init on the first run.
git submodule update --init
Full lesson: Git submodule
The git range-diff command compares two versions of a commit series. It shows how the patches changed between the two ranges. This helps after you rebase a branch.
git range-diff main..old main..new
Full lesson: Git range diff
Git stops and reports that the target directory is not empty. The clone does not start. Choose a new folder name or empty the folder first.
git clone https://github.com/tianqixin/example-git-test my-copy
Full lesson: Git clone
Your branch does not change. The new commits stay in the remote-tracking branch. Run git merge to bring them into your branch.
git fetch origin
git merge origin/main
Full lesson: Git fetch
Git refuses to delete the branch. It warns that the branch is not fully merged. Merge the branch first or use -D to force the delete.
git branch -d feature-x
Full lesson: Git branch
The submodule folders stay empty. Git does not fetch the submodule content. Run the command again with --init.
git submodule update --init
Full lesson: Git submodule
The output shows unrelated patches and confuses you. The two ranges must describe the same logical series. Check the branch names and the base commit first.
git range-diff main..feature-v1 main..feature-v2
Full lesson: Git range diff