Saturday, August 23, 2014

Git : Quick Reference

Get Started

To get started with git read the first two chapter in git documentation. I think after the first two chapters it would become useless to  keep reading, instead just keep learning as you go. Use git help <command> to learn more about a command or just google. If you still want to keep reading look at the reference URLs at the end.

To create a local git repository read git-basics. You can use any remote computer as a server very easily with git. Just create a git repository and then clone it from another computer using
git clone <username>@<IP>:<path/to/.git>
Git will use ssh to login and copy the files so no extra setup is needed. The instructions at  git-basics show how to create a git repository without a name, but if you want to give a name to you git repository i.e. instead of just a .git folder you want myrepo.git you can do :
mkdir myrepo.git 
cd myrepo.git 
git init --bare
Add remote repositories with
git remote add [shortname] [url]

Configurations

  • color your diffs
git config --global color.ui auto
  • more configuration options git-scm

Working with git

Working with Remotes

Use the following command to create and track remote branches (ref)
git checkout --track -b <local branch> <remote>/<tracked branch>
OR
git checkout -t <remote>/<tracked branch>
The second command keeps the same name as the remote tracked branch. To avoid the default merging with a remote branch with git pull  use git fetch 
git fetch <remote>OR
git fetch --all
to fetch from all remotes. You can then checkout a branch and fast forward using
git merge --ff-only <remote>/branch

Reverting to old commits

Look at the reset command.
git checkout <branch>~n checkout the commit n commits before the latest commit
git checkout <commit> <file1> <file2>...  checkout files form commit
Note: <commit> can be a branch name or the checksum of any commit.

Looking at the commit logs


Look at diff and status.
git diff <branch old/commit checksum>:[<file>] <branch new/commit checksum>:[<file>]
to see the difference in the files. To see commit log with only the names of the changed files do
git log --name-only
Useful : git log -p -n shows the changes introduced in the last n commits. Use gitk --all to see everything in a gui.


Branching

Show all branches
git branch -a
Create branch on the current state
git branch <name>

Git Stash

Use git stash to quickly save your current state without messing up your commit history
git stash save "<message>"
You can also do multiple layers of stashes. So list all the stashes you have done using

git stash list
stash@{0}: On shoulda: Updating instructions
stash@{1}: On master: started merge but need to fix #104 first
stash@{2}: On feature1: Adding some stuff

Pop the stash from your commit and get back to the state before "git stash save".
git stash pop/apply
A note with this command, it deletes that stash for good, while "git stash apply" does not. You can manually delete stashes with:
git stash drop <id>

and delete stashes with
git stash drop <id>
or delete all of the stored stashes with:
git stash clear
URLs :
a. http://gitready.com/beginner/2009/03/13/smartly-save-stashes.html
b. http://gitready.com/beginner/2009/01/10/stashing-your-changes.html

Renaming branches

Refer to here

Now to rename the master branch you have first change the default head to something else on the remote. You cannot do this from the client. First ssh onto your server and do 

git symbolic-ref HEAD refs/heads/new_master

Now, you can do as you want.

Frequently Used Commands 

  • git add    add files to git tracking
  • git commit   commit the files to the local git
  • git push    update the remote with the current branch
  • git pull   fetch and merge current branch from remote origin
  • git fetch    fetches named heads or tags from one or more other repositories, along with the objects necessary to complete them.
    • git fetch --all   fetch from all remotes
  • git merge  incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch.
    • use "git merge --ff-only <remote>/branch" to fetch and merge from remote 
  • git branch <name>  creates a branch at the current checkout
  • git log  show commit logs for the current branch
  • git checkout <file OR branch OR branch:file> Checkout files and branches

Reference URLs

http://git-scm.com/doc
http://gitready.com

No comments:

Post a Comment