12 git reference
Patrick Haralabidis редактировал(а) эту страницу 2017-05-23 21:50:12 +10:00

git commands and workflow for beginners

Goal: Keep it simple, no fancy stuff - just easiest way to get things done (and to understand them)

Main text

TODO:

  • We need reference table and also basic workflow (1 computer, 2 computers - move work, PR updates / rebase).
  • The workflow will naturally create branches for each fix (new git concept)
  • Picture with remote, origin and current will be worth thousands words ...

Quick reference

The table below describes the Git flow for creating and updating a pull request.

Task Git command Details
Fork repository Fork the repository from the project's github page See Checking out the code repository
Clone repository git clone https://github.com/YOUR-USERNAME/corefx.git See Checking out the code repository
Configure remote repository git remote add upstream https://github.com/dotnet/corefx.git See Checking out the code repository
Sync repository git fetch upstream Retrieve the latest code from the original fork.
Sync repository git checkout master Checkout your fork's master branch.
Sync repository git merge upstream/master Merge the latest code from the original to your fork's master branch.
Create a branch git branch < Name of the branch you want to create > Creates a new branch on which you will add your code.
Checkout a branch git checkout < Name of the branch created above > Sets your newly created branch as your current working branch
Add changes to the queue git add --all Stages all the files you have added and edited for commit
Commit changes with message git commit -m < message > Commits the changes to your local branch and includes the message as a description
Push the changes to your remote branch git push Pushes the changes you committed earlier to your local branch to your remote branch
Raise a pull request See details See Raising a pull request

Ideas / Notes

Very useful git blog posts - Sara Ford's Blog:

Useful starter: Git and GitHub.com

[MS internal] GitHub usage help: http://aka.ms/GitHelp

Logical workflow

Idea: Describe this logical workflow and annotate it with git commands (in a table?)

  • Enlist / clone = get sources on your computer
  • Work on a fix/feature = create branch
    • Tip: What if you forget and realize you have pending files in master, or if you committed to master
  • Update (sync) with latest master branch changes
  • File operations - edit, delete, rename (how)
    • Note: Rename is auto-detected by file content, no 'rename' records in git
  • Publish (commit) changes locally
    • Note: Don't worry about history too much, it can be cleaned up before final checkin
    • ProTip: Don't mix unrelated changes, keep each bug fix / logical work separate - esp. separate larger code cleanup / refactoring / formatting changes (unlikely to introduce regressions) from real functional changes (which may potentially introduce regressions, etc.)
  • Backup changes on GitHub
  • Share changes between 2 computers (e.g. Laptop & main machine)
  • Create PR
    • Cleanup your history prior to submitting PR
    • //Link to Dos & Don't in PRs - TODO: Is this the right place?
  • Update PR to incorporate feedback
    • Note: Do not merge master during PR work! Do rebase instead if history is too messy or if there are conflicting changes in master (which is rare!) - if unsure, do it only if you're asked. If you must merge, then do it only after all feedback is incorporated (to avoid restarting the code review from scratch). Note that rebase can be done automatically (by repo maintainers) upon merging your PR -- that should be the 95% case.

karelz's reference

DISCLAIMER: These are my old newbie zero-experience someone-told-me notes, they need to be validated.

Task Git command TFS command
Enlistment
Enlist [one-time per repo] Create new repo or your repo fork (GitHub UI)git clone <repo_name>
  • Just shortcut for "git clone https://github.com/<your_username>/<repo_name>"
  • Creates subdir repo_name (incl. 'origin' info - run "git remote -v")
Note: The directory is movable between directories, disks, machines
tf workspace /new
Enlistment info git remote -v tf workspace
Modify
Edit file Just edit the file
  • To make it part of your to-be-committed changes: git add <file>
tf edit <file>
Add file git add <file> tf add <file>
Rename file Just rename file on disk + git add <new_file>, git will automatically detect renames tf rename <file>
Delete file Just delete file on disk + git add <old_file>, git will automatically detect deletes tf delete <file>
Undo (local) change git checkout -- <file>
  • Note: "git status" tells you the instructions
tf undo <file>
Sync / checkin / history
List pending & local-only changes git log
  • Lists changes which are not committed locally and which are not pushed to server yet
tf status
Sync git pull -r
  • fetch + rebase
tf get
Resolve conflicts tgit resolve tf resolve
Diff tgit diff tf diff
Checkin
  • Checkin locally (only into local history all edited files): git commit -a
  • Adjust local checkins before publishing them to server (e.g. squash=merge them together): git rebase -i
    • Note: i=interactive mode (pops txt file editor)
    • Read the commands - use p/s/f as needed
  • Push changes to server:
    • git push <repo_name> <branch_name>
    • git push origin my-feature
    • Note: Never push into upstream
    • Review changes: git compare
tf checkin
tf submit
Revert change TODO tf revert
History view git log
UI: gitk
tf history
Branches
Create branch / named set of changes ("shelveset") git checkout -b <branch_name>
  • Note: Branches are super-cheap, more like shelvesets
  • Beginner hint: Create a new branch before you try anything dangerous with git (pull / squash)
tf shelve
Switch between branches ("shelvesets") git checkout <branch_name>
Hint: Use master as branch name
tf unshelve
List active branch git status
  • Starts with active branch "On branch <branch_name>"
Also in: git branch
--
List branches
  1. git branch -a
  2. GitHub UI on your repo clone
tf shelvesets
Merge branches git checkout <target_branch_name>
git merge <source_branch_name>
tf integrate
Delete branch
  1. Locally: git branch -d <branch_name>
  2. Remotely: git push origin --delete <branch_name>
tf shelveset /delete
Other
Create stash / shelveset backup
  • Create: git stash
  • List all: git stash list
  • Restore: git stash apply <stash_name>
  • Delete: git stash drop
--
Rewriting history http://sethrobertson.github.io/GitFixUm/fixup.html (incl. dangerous rewrite published history) --