Tuesday, 28 October 2014

Basic Git Commands


1. Create a new repo in GitHub
On your GitHub profile click “new repo” screen shot 2013-06-01 at 12 38 50 pm give it a name (example: project), brief description, choose the “public” repo option, and click “create repository”.
In the command line–make sure you cd into your project folder, and type:
git init
This initializes a git repository in your project
Next check if a file called READEME.rdoc exists in your railsgirl directory:
ls README.rdoc
If the file doesn’t exist, create it by typing:touch README.rdocNext type:git remote add origin https://github.com/username/project.git

This creates a remote, or connection, named “origin” pointing at the GitHub repository you just created. You can copy and paste the link from your GitHub repository page by clicking the clipboard icon next to the URL.
2. Make changes and push them to GitHub:

git status
This will list out all the files in your working directory.
Then type:
git add .
or git rm --cached filename //remove files
This adds in all of your files & changes so far to a staging area.
Then type:
git commit -m "first commit"
Then type:
git push -u origin master
This sends your commits in your “master” branch to GitHubThis commits all of your files adding the message “first commit”.

3. Push larger than 1M file error:
error: RPC failed; result=22, HTTP code = 411
This is an error happens for https connection.
If we use ssh url, no such issue.

4. Push a new branch to an empty master Error

$ git checkout -b <branch>

When we want to commit this branch to master as initial commit,
it shows below error.

$ git push origin master
error: src refspec master does not match any.
error: failed to push some refs to 'https://project.git'

Solution:
$ git show-ref 
shows local branches.

$ git push origin HEAD:master

5. Remove a local branch
$ git branch -d <localbranch>

6. Checkout from another branch

$ git fetch //fetch all of the remote branches to local
$ git checkout old-branch
$ git checkout -b new-branch //create and switch to the new branch based on old branch
$ git push origin new-branch //publish the new branch to remote

7. Discard uncommitted changes

$ git reset --hard HEAD
Which removes all the local uncommitted changes.
Or
$ git reset --hard HEAD~3
Move HEAD back by 3 commits

8. Delete a branch
To delete a local branch
COPY
git branch -d the_local_branch
To remove a remote branch (if you know what you are doing!)
COPY
git push origin :the_remote_branch


9. Tag a branch

Checkout the branch you want to tag
$ git tag v1.0.0
$ git tag
By default, the git push command doesn’t transfer tags to remote servers. You will have to explicitly push tags to a shared server after you have created them
$ git push origin v1.0.0
Delete a local and remote tag is similar like a branch
$ git tag -d v1.0.1
$ git push origin :refs/tags/v1.0.0
Reference:
http://stackoverflow.com/questions/2702731/git-fails-when-pushing-commit-to-github/3605544#3605544
http://guides.railsgirls.com/github/
https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
http://stackoverflow.com/questions/4181861/src-refspec-master-does-not-match-any-when-pushing-commits-in-git
https://git-scm.com/book/en/v2/Git-Basics-Tagging

No comments:

Post a Comment