Git
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.
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 commit -m "describe the change" copy
Stage interactively, hunk by hunk: ideal for committing only part of your changes. Exchange commits with the remote repository. pull is fetch (download) plus merge in one step.
Branches are cheap pointers to commits. switch changes branches, -c creates one; checkout is the older command that does both jobs. $ 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
$ 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 [branch1]..[branch2] copy
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. 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 commit -m "remove ignored files" copy
Put uncommitted work aside to get a clean working directory, then bring it back later. -u includes untracked files. pop applies the stash and removes it from the list; apply keeps it around, useful when applying the same work to several branches.
Tags mark specific commits, typically releases. Annotated tags (-a ) store author, date, and a message. $ git tag -a v1.0 -m "release 1.0" copy
$ git tag -d [tagName] copy
Tags are not pushed by default. Push them explicitly with --tags or git push origin [tagName] .
A remote is a named URL of another copy of the repository; origin is the conventional name for the main one. $ 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
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.
Copied to clipboard