LinuxCommandLibrary
GitHub F-Droid Google Play Store

Git

Getting Started

Tell git who you are first; the name and email are recorded in every commit you make.
$ git config --global user.name "Your Name"
copy
$ git config --global user.email "you@example.com"
copy
Start a new repository in the current directory, or clone an existing one.
$ git init
copy
$ git clone [url]
copy

Everyday Workflow

Changes move in two steps: add puts them in the staging area, commit turns the staged changes into a permanent snapshot. status shows where everything stands.
$ git status
copy
$ git add [file]
copy
$ git add .
copy
$ git commit -m "describe the change"
copy
Stage interactively, hunk by hunk: ideal for committing only part of your changes.
$ git add -p
copy
Exchange commits with the remote repository. pull is fetch (download) plus merge in one step.
$ git push
copy
$ git pull
copy

Branches

Branches are cheap pointers to commits. switch changes branches, -c creates one; checkout is the older command that does both jobs.
$ git branch
copy
$ git branch -a
copy
$ git switch [branchName]
copy
$ git switch -c [newBranch]
copy
$ git checkout -b [newBranch]
copy
Merge another branch into the current one.
$ git merge [branchName]
copy
Delete branches locally and on the remote. -d refuses to delete unmerged work; -D forces it.
$ git branch -d [branchName]
copy
$ git branch -D [branchName]
copy
$ git push origin --delete [branchName]
copy

Inspecting History

$ git log
copy
$ git log --oneline --graph --all
copy
$ git show [commitHash]
copy
$ git blame [fileName]
copy
diff alone shows unstaged changes; --staged shows what the next commit will contain.
$ git diff
copy
$ git diff --staged
copy
$ git diff [branch1]..[branch2]
copy

Undoing Things

Discard uncommitted changes to a file, or unstage it without losing the changes.
$ git restore [fileName]
copy
$ git restore --staged [fileName]
copy
Fix the most recent commit (message or forgotten files), or undo a commit safely by creating a new commit that reverses it.
$ git commit --amend
copy
$ git revert HEAD
copy
reset moves the branch pointer back. --soft keeps your changes staged, --hard throws them away.
$ git reset --soft HEAD~1
copy
$ git reset --hard HEAD~1
copy
revert is safe on shared branches because it only adds a commit. reset --hard and --amend rewrite history: avoid them on commits you have already pushed.Stop tracking files that are now in .gitignore but were committed earlier.
$ git rm -r --cached .
copy
$ git add .
copy
$ git commit -m "remove ignored files"
copy

Stashing

Put uncommitted work aside to get a clean working directory, then bring it back later. -u includes untracked files.
$ git stash -u
copy
$ git stash list
copy
$ git stash pop
copy
$ git stash apply
copy
pop applies the stash and removes it from the list; apply keeps it around, useful when applying the same work to several branches.

Tags

Tags mark specific commits, typically releases. Annotated tags (-a) store author, date, and a message.
$ git tag [tagName]
copy
$ git tag -a v1.0 -m "release 1.0"
copy
$ git tag -d [tagName]
copy
$ git push --tags
copy
Tags are not pushed by default. Push them explicitly with --tags or git push origin [tagName].

Remotes

A remote is a named URL of another copy of the repository; origin is the conventional name for the main one.
$ git remote -v
copy
$ git remote add [remoteName] [remoteURL]
copy
$ git fetch [remoteName]
copy
$ git pull [remoteName] [branchName]
copy
push -u links the local branch to the remote one, so future git push and git pull work without arguments.
$ git push -u [remoteName] [branchName]
copy

Rewriting History

rebase replays your commits on top of another branch for a linear history; cherry-pick copies a single commit onto the current branch.
$ git rebase [branchName]
copy
$ git rebase -i HEAD~3
copy
$ git cherry-pick [commitHash]
copy
Interactive rebase (-i) lets you reorder, squash, and reword commits. As with reset, never rebase commits that others may already have pulled.

Getting Help

$ git help [command]
copy
$ git help -g
copy
Copied to clipboard
Kai

AltStyle によって変換されたページ (->オリジナル) /