Configure your name as the guy who is checking in code:
git config –global user.name “Your Name”
And your email address:
git config –global user.email youremail@example.com
Alias commands:
git config –global alias.co checkout (This will make co same as checkout)
Start your git repository:
git init
If you don’t want to keep telling git what remote branch are you pulling / pushing from:
git config –global push.default current
git branch –set-upstream [your branch name] origin/[the remote branch you are pushing to constantly]
To ignore files when checking in / out:
Create the file .gitignore and put all the files that are to be ignored inside there, one per line
Git stash:
Save your changes, and restore back to the head revision. You will be able to pull your changes back if you need to later (from what you saved).
Making a branch in git:
$ git checkout -b branch-name origin/branch-name // It will create new-branch-name and check it out
$git branch new-branch-name // It will create a new branch locally
$git push origin new-branch-name // Now everybody can see it and pull it, as you are pushing it to the remote repository
Pull a remote branch, and switch to it locally:
git checkout –track origin/the-name-of-the-remote-branch
Renaming a branch:
git branch -m old_branch new_branch
Deleting a branch:
$ git branch -D topic-branch // and the delete happens!
Delete a branch remotely:
$ git push origin :visitor_pages
Where [visitor_pages] is the name of the branch
Merging a branch:
$git merge [name of the branch to be merged with the currently checkout branch]
Bringing a file back in time to a given sha (revert):
git checkout 3fc91a79a4f1d8ebd84091e60995dc0e5c4e04c3 — [file to be reverted here]
Where 3fc91a79a4f1d8ebd84091e60995dc0e5c4e04c3 is the sha number you want the file to revert to
Check file history:
git log –follow -p [filename]
Will tell you even if the file was moved around…
git reset –hard <some commit from origin here> // Will bring your local back to that commit point (and get rid of all changes you did after that!, so use sparingly…)
git config -e // Show your your git config file. In there you can, among other things, change the default branch that is going to be push / pull from:
[branch “master]
remote = “the_branch_that_will_be_ur_default_here”
if all you want is to add an extra remote server to check code in / out (in addition to your main repository):
git remote add alt alt-machine:/path/to/repo
git remote update
git pull alt master
# see the content of the files that have been deleted before:
git show $(git rev-list –max-count=1 –all — path/to/deleted/file_here)^:path/to/deleted/file_here
Adding tags to your codebase
// to display the current tags:
$ git tag
// to add one to a certain point in time:
$ git tag -a v1.1 -m “whatever message here”
// to show the information on the tag:
$ git show v1.1
// to push to origin so everybody can see it:
$ git push origin v1.1
To get the git hash you are currently at:
git rev-parse HEAD