Git How To

From Dreamwidth Notes
Revision as of 12:37, 30 June 2013 by Ninetydegrees (Talk | contribs)

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

How to get help with git commands

  • To get a list of most used commands:
git help
  • To open the Git manual:
man git
  • To see a summary of options for a specific command:
git COMMAND -h
  • To open the manual page for a specific command:
git help COMMAND
or
git COMMAND --help
or
man git-COMMAND


How to use auto-completion

Hit the Tab key as you type to get suggestions. This works with commands and options.

N.B. New Dreamhacks have this set up automatically: see the Pro Git book on how to set it up otherwise.


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 create and switch to a branch stemming from another branch:
git checkout -b BRANCH_NAME OTHER_BRANCH
  • To move changes to the staging area:
git add
  • To commit changes:
git commit
  • To add and commit changes at once:
git commit -a
  • 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 latest commits on local branches:
git branch -v
  • To see unstaged code changes:
git diff
  • To see staged but uncommitted code changes:
git diff --cached
or
git diff --staged
  • To see staged but uncommitted file changes:
git status -s
  • To see committed code changes (latest commit only):
git show
  • To see past commits one at a time:
git log -1
git log -2

and so on.

  • To see commits for a certain file:
git whatchanged FILE
  • To see what you've done locally in the last 30 days:
git reflog
  • To see branches on a graph: go to your profile page on github.com, click 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:
git stash show


How to undo uncommitted changes

  • To unstage a file:
git reset HEAD FILE_PATH
Note that the file is still modified. It's just no longer part of your current staging area.
  • To undo uncommitted changes:
git checkout -- FILE_PATH


How to amend your last commit

Note: you can do this as long as nothing has been pushed.

  • To update a commit with some new changes:
git commit --amend
  • To do so and reuse your last commit message:
git commit --amend -C HEAD


How to undo committed changes

  • To reset everything to the latest commit:
git reset --hard
  • To go back one commit:
git reset --hard HEAD~1
Make sure there's a commit to go back to before you do this or you'll end up in detached HEAD state, which is as bad as it sounds.


How to move and delete files

  • To move files:
git mv OLD_PATH NEW_PATH
  • To delete files:
git rm FILE_PATH


How to rename and delete branches

  • To rename a local branch:
git branch -m OLD_NAME NEW_NAME
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 or a merged branch (i.e. pushed to github.com and committed to Dreamwidth):
git branch -d BRANCH_NAME
The merge will only be detected if you've updated your code. Otherwise, you'll get an error saying the branch isn't fully merged.
  • To delete an unmerged branch (i.e. pushed to github.com but not committed to Dreamwidth or not fully so):
git branch -D BRANCH_NAME
  • To delete a branch on GitHub:
git push origin :BRANCH_NAME
You can also do this directly on GitHub once your branch 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 push commits to the release branch

  • First create a copy of the release branch:
git checkout -b release-X.X dreamwidth/release-X.X
X-X is the number given to the release on the GitHub repo branch. Make sure it matches.
  • Then follow the usual steps (edit, add, commit, push) and when you request your changes to be pulled on GitHub make sure to select the release branch as both your base branch and your head branch.


How to update your branch

  • To update your branch so it has all the new code which has been added to develop since you created it:
git checkout BRANCH_NAME
git pull --rebase --ff-only dreamwidth develop
This will only work if your branch is empty or what you have has been committed and won't conflict with the new additions.
  • You can also do this with other branches such as the current release branch by checking out the release branch then pulling from release-X.X instead of develop.
  • If you had already pushed your branch to GitHub, you will need to force push your changes:
git push -f origin BRANCH_NAME


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. Load the interactive rebase interface using
    git rebase -i develop
  4. 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.

From there you have several options:

  • If you want to be able to edit all commit messages or merge them in some fashion, edit the work 'pick' to the word 'squash' for the ones you want to squash into the previous commit. You'll be then shown all commit messages and you'll be able to edit them, comment the lines you want to hide, etc.
  • If you want to edit the commit message of your master commit but discard all the other ones, use 'reword' for your master commit then 'fixup' for the other ones.
  • If you want to keep your master message as-is and discard the rest, use 'pick' and 'fixup' instead.


  • 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

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

  • 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 .gitconfig in your root folder. 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).

How to create a default commit message

Easy peasy! In your root folder, create a file called .gitmessage.txt with whatever default message you wanna use then edit .gitconfig to use it:

git config --global commit.template $HOME/.gitmessage.txt


How to automatically insert the bug number into your commit message

Important! This will only work if 'bug XXX' where XXX is the bug number is in your branch name. This is meant as a way not to have to type it or paste it *again* in your commit messages.

Go to ~/dw/.git/hooks/ and create a filed called prepare-commit-msg. Paste this gist made by the brilliant [info]fu and save. Finally make it executable by running:

chmod u+x ~/dw/.git/hooks/prepare-commit-msg
N.B. This can be used in conjunction with the default commit message mentioned in the previous section.


How to configure git

See this article for some of the most useful settings.