Difference between revisions of "Git How To"

From Dreamwidth Notes
Jump to: navigation, search
(bit on fixup)
(+ basic commands; rephrasing for clarity)
Line 12: Line 12:
 
<syntaxhighlight lang="bash">man git-branch</syntaxhighlight>
 
<syntaxhighlight lang="bash">man git-branch</syntaxhighlight>
 
<syntaxhighlight lang="bash">man git-push</syntaxhighlight>
 
<syntaxhighlight lang="bash">man git-push</syntaxhighlight>
 +
 +
 +
= Basics =
 +
 +
* To create a branch
 +
 +
<syntaxhighlight lang="bash">git branch BRANCH_NAME</syntaxhighlight>
 +
 +
* To switch to a branch
 +
 +
<syntaxhighlight lang="bash">git checkout BRANCH_NAME</syntaxhighlight>
 +
 +
* To create and switch to a branch at once:
 +
 +
<syntaxhighlight lang="bash">git checkout -b BRANCH_NAME</syntaxhighlight>
 +
 +
* To move changes to the staging area:
 +
 +
<syntaxhighlight lang="bash">git add</syntaxhighlight>
 +
 +
* To commit changes:
 +
 +
<syntaxhighlight lang="bash">git commit</syntaxhighlight>
 +
 +
* To push changes:
 +
 +
<syntaxhighlight lang="bash">git push origin BRANCH_NAME</syntaxhighlight>
  
  
Line 22: Line 49:
 
<syntaxhighlight lang="bash">git branch</syntaxhighlight>
 
<syntaxhighlight lang="bash">git branch</syntaxhighlight>
  
* To see local and remote branches:
+
* To see local and remote branches (including deleted ones):
  
 
<syntaxhighlight lang="bash">git branch -a</syntaxhighlight>
 
<syntaxhighlight lang="bash">git branch -a</syntaxhighlight>
  
* To see the code changes you've made on the branch before you move them to the staging area:
+
* To see unstaged code changes:
  
 
<syntaxhighlight lang="bash">git diff</syntaxhighlight>
 
<syntaxhighlight lang="bash">git diff</syntaxhighlight>
  
* To see the code changes you've made on the branch before you commit them:
+
* To see uncommitted code changes:
  
 
<syntaxhighlight lang="bash">git diff --cached</syntaxhighlight>
 
<syntaxhighlight lang="bash">git diff --cached</syntaxhighlight>
  
* To see the list of files you've added, removed and modified on the branch before you commit them:
+
* To see uncommitted file changes:
  
 
<syntaxhighlight lang="bash">git status -s</syntaxhighlight>
 
<syntaxhighlight lang="bash">git status -s</syntaxhighlight>
  
* To see your last commit on a branch (before it's been merged):
+
* To see committed code changes (last commit only):
  
 
<syntaxhighlight lang="bash">git show</syntaxhighlight>
 
<syntaxhighlight lang="bash">git show</syntaxhighlight>
  
* To see commit logs:
+
* To see all commits (one at a time):
 
+
<syntaxhighlight lang="bash">git log</syntaxhighlight>
+
 
+
You can see them one at a time using:
+
  
 
<syntaxhighlight lang="bash">git log -1</syntaxhighlight>
 
<syntaxhighlight lang="bash">git log -1</syntaxhighlight>
Line 53: Line 76:
 
and so on.
 
and so on.
  
* To see only the commits that changed a certain file:
+
* To see commits for a certain file:
  
 
<syntaxhighlight lang="bash">git whatchanged FILE</syntaxhighlight>
 
<syntaxhighlight lang="bash">git whatchanged FILE</syntaxhighlight>
Line 66: Line 89:
 
* Make sure you're on the right branch using the <code>checkout</code> command.
 
* Make sure you're on the right branch using the <code>checkout</code> command.
  
* Put away your work using the <code>stash</code> command:
+
* To put it away:
  
 
<syntaxhighlight lang="bash">git stash</syntaxhighlight>
 
<syntaxhighlight lang="bash">git stash</syntaxhighlight>
  
* And bring it back with:
+
* To bring it back:
  
 
<syntaxhighlight lang="bash">git stash pop</syntaxhighlight>
 
<syntaxhighlight lang="bash">git stash pop</syntaxhighlight>
  
* To see the work you've stashed, you can use:
+
* To see what's stashed on a branch, you can use:
  
 
<syntaxhighlight lang="bash">git stash show</syntaxhighlight>
 
<syntaxhighlight lang="bash">git stash show</syntaxhighlight>

Revision as of 13:13, 20 April 2013

Note: Feel free to correct, expand, do anything which could make this better and clearer. ^_^

Getting help with git commands

The general man page on git lists all the git subcommands:

man git

...but it might not be immediately obvious that git's subcommands all have their own man pages, named using the pattern git-subcommand. So for detailed help on git commit, git branch, and git push, respectively:

man git-commit
man git-branch
man git-push


Basics

  • To create a branch
git branch BRANCH_NAME
  • To switch to a branch
git checkout BRANCH_NAME
  • To create and switch to a branch at once:
git checkout -b BRANCH_NAME
  • To move changes to the staging area:
git add
  • To commit changes:
git commit
  • To push changes:
git push origin BRANCH_NAME


How to check things

Note: to exit code views, hit the 'q' key.

  • To check which local branch you're on (noted with an asterisk):
git branch
  • To see local and remote branches (including deleted ones):
git branch -a
  • To see unstaged code changes:
git diff
  • To see uncommitted code changes:
git diff --cached
  • To see uncommitted file changes:
git status -s
  • To see committed code changes (last commit only):
git show
  • To see all commits (one at a time):
git log -1
git log -2

and so on.

  • To see commits for a certain file:
git whatchanged FILE
  • On github.com, you can see a graph of the branches you've pushed and the commits you've made by going to your profile page, clicking dw-free or dw-nonfree then on Network. Hit Shift and the right arrow to go to the most current part of the graph.


How to stash your changes

If you have work you haven't committed and don't want to yet you need to stash your work first. It's generally good practice to do this every time you have work you haven't committed.

  • Make sure you're on the right branch using the checkout command.
  • To put it away:
git stash
  • To bring it back:
git stash pop
  • To see what's stashed on a branch, you can use:
git stash show


How to undo changes

  • If you added a file using git add but finally want to unstage it (i.e. remove it from the staging area before you do git commit), you can use the reset command:
git reset HEAD FILENAME
Note that the file is still modified; it's just no longer part of your staging area and won't be part of your commit.
  • If you want to undo the changes you've made to a file before you commit it, you need to use checkout:
git checkout -- FILENAME
  • If you made a mistake in your last commit, you can alter it as long as it hasn't been pushed anywhere else. Make the code changes you want to make, add them like normal, then run
git commit --amend
  • You can also reset everything to the last commit using --hard:
git reset --hard
  • If you've already committed your work and want to go back to a clean slate, you can go one commit back using:
git reset --hard HEAD~1

How to move and delete files

  • To move files:
git mv OLDPATH NEWPATH
  • To delete files:
git rm FILEPATH


How to rename and delete branches

  • To rename a local branch:
git branch -m OLDNAME NEWNAME
N.B. If you had already pushed some changes to GitHub, this will create a new identical branch there. You will need to delete the old one using the method described below.
  • To delete a local branch you created by mistake or a branch which has been merged into develop (you must update your code for the system to detect the merge), use:
git branch -d BRANCHNAME
  • To delete an unmerged branch, use this instead:
git branch -D BRANCHNAME
  • To delete the remote branch on GitHub:
git push origin :BRANCHNAME
You can also do this directly on GitHub one your pull request has been merged into develop. Just click on the pull request (from the Activity list on your profile for example), scroll down and it'll ask if you want to delete the branch.


How to update your branch

  • If, for some reason, you'd like to update your branch so it has all the new code which has been added to develop since then, you can do so with rebase. However, this will only work smoothly if you have nothing on your branch of if what you have has been committed and won't conflict with the new additions. If that's the case then run:
git checkout BRANCHNAME
git rebase develop

Ta-da! All new!

  • If you had already pushed some changes to GitHub, you will need to force a push to get it updated:
git push -f origin BRANCHNAME


How to squash several commits into one

If, for some reason, you'd like several commits to be just one, you can squash them. This is not reversible so proceed with caution.

  1. Make sure everything is up-to-date.
  2. Make sure you're on the correct bug branch using git checkout.
  3. Use git log or git log -n to check the ID of the commits you want to squash. It might come in handy later.
  4. Load the interactive rebase interface using
    git rebase -i develop
  5. The interface will show you all the successive commits on this branch, from the oldest at the top to the most recent at the bottom.
  6. Edit the work 'pick' to the word 'squash' for the ones you want to squash into the previous commit. This is where the commit IDs can be useful as the commit message is shortened and it may not be easy to say which is what. Each line shows the first seven characters of the commit ID.
  7. When you're done hit Ctrl+X then type Y and Enter.
  8. You'll be then shown the commit messages if you want to edit or remove them. When you're done hit Ctrl+X, Y and Enter again.
  9. Ta-da!
  10. Note that if you had already pushed some changes to GitHub, you will need to force a push to get it updated:
git push -f origin BRANCHNAME
  1. Finally, you can skip some of these steps by using 'fixup' instead of 'squash' if you're happy with the commit message of the commit you want to squash others into.

Also, git ready has a nice guide to squashing commits using the interactive rebaser (git rebase -i).

FWIW, the interactive rebaser can also do many other nifty and powerful things. You can change commit messages, edit past commits, reorder commits, or discard individual commits entirely.

How to add/commit only part of a file

There are times when you want to commit only some of the changes you've made to a file. Maybe you've fixed one thing, and are in the midst of fixing a second thing somewhere else in that file when you decide you want to commit the first change.

You don't have to back out the work you've done on the second fix in order to commit the first change by itself. Use the interactive option of git add:

# consider all changed files for addition 
git add -i 
# consider only the files specified 
git add -i FILE [FILE2 FILE3 ...]

You'll be shown the current staging status of the file(s), and how many lines in each of them are staged (added) or unstaged (not added yet).

   twilight:~/temp rick$ git add -i
              staged     unstaged path
     1:    unchanged        +9/-8 bar.pl
     2:    unchanged        +2/-1 foo.pl
     3:    unchanged        +2/-0 index.html
   
   *** Commands ***
     1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked
     5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elp
   What now> 

There are a lot of commands, but you really only need very few of them. Too, there's help available in each menu -- you can enter ? at any prompt for help, in addition to any visible help options.

  • 1: shows you the current staging status, just like when git add -i started.
  • 2: stage (add) whole files
  • 3: unstage (un-add) whole files
  • 4: add a currently-untracked file
  • 5: stage individual chunks in a file bit-by-bit
  • 6: show the diff between what's been staged so far & the previous commit

Number 5 (patch) is the biggest winner of all these, and usually why you're running the command at all. Select 5 and you'll be presented with a menu like this one:

              staged     unstaged path
     1:    unchanged        +9/-8 [b]ar.pl
     2:    unchanged        +2/-1 [f]oo.pl
     3:    unchanged        +2/-0 [i]ndex.html
   Patch update>> 

Pick the file or files you want to part-stage by entering their number(s), then hitting enter. When you're done picking files, hit enter once more at the empty Patch Update>> prompt. The program will show you every change in the files you've selected, one at a time, and ask you if you want to add it, eg:

   diff --git a/index.html b/index.html
   index 32870ee..416e0bd 100644
   --- a/index.html
   +++ b/index.html
   @@ -8,6 +8,7 @@
    
    
        <link rel="stylesheet" type="text/css" href="./css/base.css" />
   +    <link rel="stylesheet" type="text/css" href="./css/frontpage.css" />
        
        <title>Welcome!</title>
      </head>
   Stage this hunk [y/n/a/d/e/?]? 

Hit y to add this change, n to not add it for now. There are many more options, like splitting a change in two or adding every change left in the file -- you can hit ? to see those.

When you're done, you're taken back to the main menu. If you're finished, hit 7 to quit. If you want, you can then review everything you've added by giving the command

git diff --cached


How to create custom keywords for your most-used commands

  • Tired of (mis)typing the same things over and over? You can create keywords for them. Open config in ~/dw/.git. Add this at the bottom and edit as desired:
[alias]
	KEYWORD1= COMMAND1
	KEYWORD2= COMMAND2
Warning! Make sure the keyword you're using isn't already a git command.
  • You can then use your keyword instead of typing the full command (e.g. git c instead of git checkout -b).