Difference between revisions of "How-To on Git for Newbies"

From Dreamwidth Notes
Redirect page
Jump to: navigation, search
(Created page with "{{Note|text=Feel free to correct, expand, do anything which could make this better and clearer. ^_^}} = How to check things = Note: to exit code views, hit the 'q' key. * T...")
 
(Redirected page to Newbie Guide: How To in Git)
 
Line 1: Line 1:
{{Note|text=Feel free to correct, expand, do anything which could make this better and clearer. ^_^}}
+
#REDIRECT [[Newbie_Guide:_How_To_in_Git]]
 
+
= 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):
+
 
+
<syntaxhighlight lang="bash">git branch</syntaxhighlight>
+
 
+
* To see local and remote branches:
+
 
+
<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:
+
 
+
<syntaxhighlight lang="bash">git diff</syntaxhighlight>
+
 
+
* To see the code changes you've made on the branch before you commit them:
+
 
+
<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:
+
 
+
<syntaxhighlight lang="bash">git status -s</syntaxhighlight>
+
 
+
* To see your last commit on a branch (before it's been merged):
+
 
+
<syntaxhighlight lang="bash">git show</syntaxhighlight>
+
 
+
* To see commit logs:
+
 
+
<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 -2</syntaxhighlight>
+
 
+
and so on.
+
 
+
* 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 yet and you want to work on another bug or update your 'master' and 'develop' branches 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 <code>checkout</code> command.
+
 
+
* Put away your work using the <code>stash</code> command:
+
 
+
<syntaxhighlight lang="bash">git stash</syntaxhighlight>
+
 
+
* And bring it back with:
+
 
+
<syntaxhighlight lang="bash">git stash pop</syntaxhighlight>
+
 
+
* To see the work you've stashed, you can use:
+
 
+
<syntaxhighlight lang="bash">git stash show</syntaxhighlight>
+
 
+
 
+
= How to undo changes =
+
 
+
* If you added a file using <code>git add</code> but finally want to unstage it (i.e. remove it from the staging area before you do <code>git commit</code>), you can use the <code>reset</code> command:
+
 
+
<syntaxhighlight lang="bash">git reset HEAD FILENAME</syntaxhighlight>
+
 
+
: 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 <code>checkout</code>:
+
 
+
<syntaxhighlight lang="bash">git checkout -- FILENAME</syntaxhighlight>
+
 
+
* You can also reset everything to the last commit using <code>--hard</code>:
+
 
+
<syntaxhighlight lang="bash">git reset --hard</syntaxhighlight>
+
 
+
* If you've already committed your work and want to go back to a clean slate, you can go one commit back using:
+
 
+
<syntaxhighlight lang="bash">git reset --hard HEAD~1</syntaxhighlight>
+
 
+
 
+
= How to move and delete files =
+
 
+
* To move files:
+
 
+
<syntaxhighlight lang="bash">git mv OLDPATH NEWPATH</syntaxhighlight>
+
 
+
* To delete files:
+
 
+
<syntaxhighlight lang="bash">git rm FILEPATH</syntaxhighlight>
+
 
+
 
+
= How to rename and delete branches =
+
 
+
* To rename a local branch:
+
 
+
<syntaxhighlight lang="bash">git branch -m OLDNAME NEWNAME</syntaxhighlight>
+
 
+
: 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:
+
 
+
<syntaxhighlight lang="bash">git branch -d BRANCHNAME</syntaxhighlight>
+
 
+
* To delete an unmerged branch, use this instead:
+
 
+
<syntaxhighlight lang="bash">git branch -D BRANCHNAME</syntaxhighlight>
+
 
+
* To delete the remote branch on GitHub:
+
 
+
<syntaxhighlight lang="bash">git push origin :BRANCHNAME</syntaxhighlight>
+
 
+
: 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 <code>rebase</code>. 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:
+
 
+
<syntaxhighlight lang="bash">git checkout BRANCHNAME
+
git rebase develop</syntaxhighlight>
+
 
+
Ta-da! All new!
+
 
+
* If you had already pushed some changes to GitHub, you will need to force a push to get it updated:
+
 
+
<syntaxhighlight lang="bash">git push -f origin BRANCHNAME</syntaxhighlight>
+
 
+
 
+
= 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.
+
 
+
# Make sure everything is up-to-date.
+
# Make sure you're on the correct bug branch using <code>git checkout</code>.
+
# Use <code>git log</code> or <code>git log -n</code> to check the ID of the commits you want to squash. It might come in handy later.
+
# Load the interactive rebase interface using <syntaxhighlight lang="bash">git rebase -i develop</syntaxhighlight>
+
# 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.
+
# 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.
+
# When you're done hit Ctrl+X then type Y and Enter.
+
# 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.
+
# Ta-da!
+
# Note that if you had already pushed some changes to GitHub, you will need to force a push to get it updated:
+
 
+
<syntaxhighlight lang="bash">git push -f origin BRANCHNAME</syntaxhighlight>
+
 
+
 
+
= 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. At the bottom, add this then then your file:
+
 
+
<syntaxhighlight lang="bash">[alias]
+
KEYWORD1= COMMAND1
+
KEYWORD2= COMMAND2</syntaxhighlight>
+
 
+
: 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. <code>git c</code> instead of <code>git checkout -b</code>).
+
 
+
 
+
[[Category:Development]]
+
[[Category:Getting Started]]
+
[[Category:Git]]
+

Latest revision as of 16:06, 13 March 2013