Difference between revisions of "Git"

From PHASTA Wiki
Jump to: navigation, search
(Learning Git: Add commit message etiquette section)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Introduction ==
 
  
Git is a distributed revision control system (VCS), designed to work well with highly non-linear workflows. It was initially conceived to support development of the Linux kernel, and has since become the most widely-used VCS for software development.
+
'''Git''' is a distributed version control system (VCS), designed to work well with highly non-linear workflows. It was initially conceived to support development of the Linux kernel, and has since become the most widely-used VCS for software development.
  
This page is essentially a cheat-sheet for git commands frequently used by our group.
+
Git is used to maintain most of the software used by the group, including [[PHASTA]], [[Chef]], [[VTKpytools|vtkpytools]], and more. Learning git will allow you to make changes to these codes and share them with the group, in addition to allowing you to test proof-of-concept codes without fear of accidentally deleting code.
 +
 
 +
== Learning Git ==
 +
 
 +
If you're not familiar with version control systems, see the [https://git-scm.com/video/what-is-version-control What is Version Control?] video for some motivation on why VCS is used.
 +
 
 +
Next, if you're completely new to <code>git</code>, the [https://git-scm.com/video/what-is-git What is git?] video from the maintainers of <code>git</code> is a great primer. If you prefer a written medium rather than a video, the [https://git-scm.com/book/en/v2 Pro Git book] would be a good place to start (specifically, sections [https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control 1.1 About Version Control] and [https://git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F 1.3 What is Git?]).
 +
 
 +
Once you've gotten a general idea on VCS and the core concepts of git, try out [https://learngitbranching.js.org/ Learn Git Branching]. It is a great interactive, visual training program that shows the basics. It'll help you learn the actual command line functions of git while the visuals help to give an understanding of what ''exactly'' those commands do. Even if you're not completely new to git, it may help you to understand some concepts you didn't before.
 +
 
 +
=== Refreshers ===
 +
 
 +
If you need a quick refresher on how to do certain actions, or just a quick cheat sheet [https://ndpsoftware.com/git-cheatsheet.html#loc=stash; this interactive cheatsheet] is quite nice for figuring out what commands you need to move changes from one "stage" to another.
 +
 
 +
=== Commit Message Etiquette ===
 +
 
 +
When writing a commit, it's good practice to follow the basic guidance found in the [https://git-scm.com/docs/git-commit#_discussion git documentation] for writing a commit message:
 +
 
 +
''<blockquote>Though not required, it’s a good idea to begin the commit message with a single short (less than 50 character) line summarizing the change, followed by a blank line and then a more thorough description. The text up to the first blank line in a commit message is treated as the commit title, and that title is used throughout Git.</blockquote>
 +
''
 +
So something like:
 +
Summarize changes in around 50 characters or less
 +
 +
More detailed explanatory text, if necessary. Could also include
 +
some bullet points:
 +
  - Item 1
 +
  - Item 2
 +
 
 +
Other general good ideas it to have the first word of the title be a verb (ie. "Fix...", "Add...", "Refactor...") and have them be present tense (mostly because it's generally less characters: Fix vs Fixed, Add vs Added, etc.). [https://chris.beams.io/posts/git-commit/ This blog post] offers some other good advice on writing "good" commit messages.
  
 
== Basic Git ==
 
== Basic Git ==
Line 9: Line 36:
 
Basic information to get started with Git can be found at http://rogerdudler.github.io/git-guide/.
 
Basic information to get started with Git can be found at http://rogerdudler.github.io/git-guide/.
  
; Git repository (repo)
+
===Git repository (repo)===
 
: Is a directory that contains files of the current branch and a hidden <code>.git</code> directory
 
: Is a directory that contains files of the current branch and a hidden <code>.git</code> directory
 
: Contains an '''Index''' in <code>.git</code> that acts as a staging area for all your branches
 
: Contains an '''Index''' in <code>.git</code> that acts as a staging area for all your branches
Line 15: Line 42:
 
: Your PHASTA repository should always be at <code>~/git-phasta/phasta</code>
 
: Your PHASTA repository should always be at <code>~/git-phasta/phasta</code>
  
; Create a new repository
+
===Create a new repository===
 
: <code>git init</code>
 
: <code>git init</code>
  
; Checkout a repository
+
===Checkout a repository===
 
: <code>git clone /path/to/repo</code>
 
: <code>git clone /path/to/repo</code>
 
: <code>git clone user@host:/path/to/repo</code>
 
: <code>git clone user@host:/path/to/repo</code>
  
; Add files to be tracked in the git index
+
===Add files to be tracked in the git index===
 
: <code>git add <filename1> <filename2> ...</code>
 
: <code>git add <filename1> <filename2> ...</code>
 
: <code>git add *</code>
 
: <code>git add *</code>
 
: <code>git add -u</code> (all files updated since last commit)
 
: <code>git add -u</code> (all files updated since last commit)
  
; Commit changes to HEAD
+
===Commit changes to HEAD===
 
: <code>git commit -m "my_username: my comments about this commit"</code>
 
: <code>git commit -m "my_username: my comments about this commit"</code>
  
; Push changes to remote repo
+
===Push changes to remote repo===
 
: <code>git push origin master</code> (pushes the <code>master</code> branch to the original/main repo)
 
: <code>git push origin master</code> (pushes the <code>master</code> branch to the original/main repo)
  
; Connect to a remote server
+
===Connect to a remote server===
 
: <code>git remote add origin <server></code> (connects <code><server></code> to your main, or <code>origin</code> repository)
 
: <code>git remote add origin <server></code> (connects <code><server></code> to your main, or <code>origin</code> repository)
 
: <code>git remote add myrepo <server></code> (connects <code><server></code> to a named branch called <code>mybranch</code>)
 
: <code>git remote add myrepo <server></code> (connects <code><server></code> to a named branch called <code>mybranch</code>)
 
: Note: <code><server></code> could be <code>user@host:/path/to/repo</code>
 
: Note: <code><server></code> could be <code>user@host:/path/to/repo</code>
  
; Create and delete branch
+
===Create and delete branch===
 
: <code>git checkout -b branch_name</code> (creates and checkout branch)
 
: <code>git checkout -b branch_name</code> (creates and checkout branch)
 
: <code>git checkout master</code> (checkout master branch)
 
: <code>git checkout master</code> (checkout master branch)
 
: <code>git branch -d branch_name</code> (delete branch we just created)
 
: <code>git branch -d branch_name</code> (delete branch we just created)
  
; Push branch to a remote
+
===Push branch to a remote===
 
: <code>git push origin branch</code> (pushes from your main <code>origin</code> repo)
 
: <code>git push origin branch</code> (pushes from your main <code>origin</code> repo)
  
; Update and merge
+
===Update and merge===
 
: <code>git pull</code> (so all branches are up-to-date for the merge)
 
: <code>git pull</code> (so all branches are up-to-date for the merge)
 
: <code>git merge origin/master</code> (merges current branch with <code>origin</code>'s <code>master</code> branch)
 
: <code>git merge origin/master</code> (merges current branch with <code>origin</code>'s <code>master</code> branch)
 
: If a merge has conflicts, you need to edit the conflict files provided by Git, and then mark them as merged with <code>git add</code>
 
: If a merge has conflicts, you need to edit the conflict files provided by Git, and then mark them as merged with <code>git add</code>
  
; Compare code in different branches
+
===Compare code in different branches===
 
: <code>git diff <branch_a> <branch_b></code>
 
: <code>git diff <branch_a> <branch_b></code>
  
; View history of commits
+
===View history of commits===
 
: <code>git log</code>
 
: <code>git log</code>
 
: <code>git log --author=foo</code>
 
: <code>git log --author=foo</code>
Line 61: Line 88:
 
: <code>git log --name-status</code> (show which files changed in each commit)
 
: <code>git log --name-status</code> (show which files changed in each commit)
 
: <code>git log --help</code> (more information)
 
: <code>git log --help</code> (more information)
 
== Advanced Git ==
 
  
 
== Git Prompt Statement (PS1) ==
 
== Git Prompt Statement (PS1) ==
Line 69: Line 94:
 
# Edit your <code>~/.bashrc</code> to say <code>source ~/.git-prompt.sh</code> before the lines that set your <code>PS1</code>
 
# Edit your <code>~/.bashrc</code> to say <code>source ~/.git-prompt.sh</code> before the lines that set your <code>PS1</code>
 
# Modify your <code>PS1</code> string to include <code>$(__git_ps1 "(%s)")</code> where you want the current branch to appear in your prompt
 
# Modify your <code>PS1</code> string to include <code>$(__git_ps1 "(%s)")</code> where you want the current branch to appear in your prompt
 +
 +
[[Category:Software Engineering]]

Latest revision as of 14:55, 4 November 2021

Git is a distributed version control system (VCS), designed to work well with highly non-linear workflows. It was initially conceived to support development of the Linux kernel, and has since become the most widely-used VCS for software development.

Git is used to maintain most of the software used by the group, including PHASTA, Chef, vtkpytools, and more. Learning git will allow you to make changes to these codes and share them with the group, in addition to allowing you to test proof-of-concept codes without fear of accidentally deleting code.

Learning Git

If you're not familiar with version control systems, see the What is Version Control? video for some motivation on why VCS is used.

Next, if you're completely new to git, the What is git? video from the maintainers of git is a great primer. If you prefer a written medium rather than a video, the Pro Git book would be a good place to start (specifically, sections 1.1 About Version Control and 1.3 What is Git?).

Once you've gotten a general idea on VCS and the core concepts of git, try out Learn Git Branching. It is a great interactive, visual training program that shows the basics. It'll help you learn the actual command line functions of git while the visuals help to give an understanding of what exactly those commands do. Even if you're not completely new to git, it may help you to understand some concepts you didn't before.

Refreshers

If you need a quick refresher on how to do certain actions, or just a quick cheat sheet this interactive cheatsheet is quite nice for figuring out what commands you need to move changes from one "stage" to another.

Commit Message Etiquette

When writing a commit, it's good practice to follow the basic guidance found in the git documentation for writing a commit message:

Though not required, it’s a good idea to begin the commit message with a single short (less than 50 character) line summarizing the change, followed by a blank line and then a more thorough description. The text up to the first blank line in a commit message is treated as the commit title, and that title is used throughout Git.

So something like:

Summarize changes in around 50 characters or less

More detailed explanatory text, if necessary. Could also include
some bullet points:
 - Item 1
 - Item 2

Other general good ideas it to have the first word of the title be a verb (ie. "Fix...", "Add...", "Refactor...") and have them be present tense (mostly because it's generally less characters: Fix vs Fixed, Add vs Added, etc.). This blog post offers some other good advice on writing "good" commit messages.

Basic Git

Basic information to get started with Git can be found at http://rogerdudler.github.io/git-guide/.

Git repository (repo)

Is a directory that contains files of the current branch and a hidden .git directory
Contains an Index in .git that acts as a staging area for all your branches
Contains a HEAD in .git that points to the last commit you made
Your PHASTA repository should always be at ~/git-phasta/phasta

Create a new repository

git init

Checkout a repository

git clone /path/to/repo
git clone user@host:/path/to/repo

Add files to be tracked in the git index

git add <filename1> <filename2> ...
git add *
git add -u (all files updated since last commit)

Commit changes to HEAD

git commit -m "my_username: my comments about this commit"

Push changes to remote repo

git push origin master (pushes the master branch to the original/main repo)

Connect to a remote server

git remote add origin <server> (connects <server> to your main, or origin repository)
git remote add myrepo <server> (connects <server> to a named branch called mybranch)
Note: <server> could be user@host:/path/to/repo

Create and delete branch

git checkout -b branch_name (creates and checkout branch)
git checkout master (checkout master branch)
git branch -d branch_name (delete branch we just created)

Push branch to a remote

git push origin branch (pushes from your main origin repo)

Update and merge

git pull (so all branches are up-to-date for the merge)
git merge origin/master (merges current branch with origin's master branch)
If a merge has conflicts, you need to edit the conflict files provided by Git, and then mark them as merged with git add

Compare code in different branches

git diff <branch_a> <branch_b>

View history of commits

git log
git log --author=foo
git log --pretty=oneline (show each commit as one line)
git log --graph --oneline --decorate --all (ASCII tree of branches, commits, merges)
git log --name-status (show which files changed in each commit)
git log --help (more information)

Git Prompt Statement (PS1)

  1. Acquire git-prompt.sh from https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh, and save it somewhere like ~/.git-prompt.sh
  2. Edit your ~/.bashrc to say source ~/.git-prompt.sh before the lines that set your PS1
  3. Modify your PS1 string to include $(__git_ps1 "(%s)") where you want the current branch to appear in your prompt