Category: githacks

Starting with git and github

Setting up git repository on github

1. create and initialize a git repository

[root@user git_client]# git init new_repo.git

2. change directory

[root@user new_repo.git]# cd new_repo.git

3. Set your username for git repository

[root@user new_repo.git]# git config --global user.name "Github_UserName"
ex:  # git config --global user.name "xyz"

4. Set your email id

[root@user new_repo.git]# git config --global user.email "your_email@youremail.com"

Check the git configuration

[root@user new_repo.git]# git config -l

5. Do the following

[root@user new_repo.git]# git remote add origin https://github_username@github.com/userName/repo_name.git
ex:
 # git remote add origin https://xyz@github.com/xyz/new_repo.git

6. Check updated url

[root@user new_repo.git]# git remote -v (to check)

7. Append following lines in .git/config

[ branch "master"]
        remote = origin
        merge = refs/heads/master

8. Create new file and make changes

[root@user new_repo.git]# touch new_file

9. Add new file in staged area

[root@user new_repo.git]# git add new_file

10. Commit the changes in newly local git repository just created

[root@user new_repo.git]# git commit -m "commit msg"

11. Now try to push directory to github. You would get following error

[root@user new_repo.git]# git push origin master
Password:
error: The requested URL returned error: 403 while accessing https://kashishbhatia@github.com/kashishbhatia/pqr.git/info/refs
fatal: HTTP request failed

12. Now login to github account and create a empty repo(with same name) in github account

13. Now try to pull the empty repo. It fails

[root@user new_repo.git]# git pull origin master 
Password: 
fatal: Couldn't find remote ref master

14. try again with “git pull”. It passes.

[root@user new_repo.git]# git pull 
Password: Your configuration specifies to merge with the ref 'master' 
from the remote, but no such ref was fetched.

15. Now finally push your changes to github repository

[root@user new_repo.git]# git push origin master 
Password: 
Counting objects: 3, done. 
Writing objects: 100% (3/3), 202 bytes, done. 
Total 3 (delta 0), reused 0 (delta 0) 
To https://github_username@github.com/userName/repo_name.git 
* [new branch] master -> master

Pull the updates done in repository

# git pull

To refresh and pull changes done by other clients on a shared repository

# git pull --rebase origin master

Clone an existing repository from github

 # git clone https://github_username@github.com/github_username/repo_name.git

Sometimes while cloning a repository from github you can get following error

[root@user rep_folder]# git clone https://github_username@github.com/username/new_repo.git
Initialized empty Git repository in /home/kashish/Desktop/git_client/reps/sleep_in_kernel/.git/
error: Could not resolve host: github.com (Domain name not found) while accessing https://github.com/kashishbhatia/sleep_in_kernel.git/info/refs

fatal: HTTP request failed

If you face this kind of problem while cloning the repository, do check if there are any http proxy enabled on your machine. Unset it and try again

[root@user rep_folder]# git config --global --unset https.proxy
[root@user rep_folder]# git clone https://github_username@github.com/username/new_repo.git
Initialized empty Git repository in /home/user/Desktop/reps_folder/new_repo/.git/
Password:
remote: Counting objects: 17, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 17 (delta 2), reused 15 (delta 0), pack-reused 0
Unpacking objects: 100% (17/17), done.

Delete a local repository

[root@user rep_folder]# git branch -D local_branch

Delete a remote repository

[root@user rep_folder]# git push origin :remote_branch

OR

[root@user rep_folder]# git push origin --delete remote_branch