[Programming Hints] Useful git command summary (Still updating)

2017-05-18

How to push an existing folder to the GitHub

switch to your folder (E.g cd ~/testFolder)

1
2
3
4
5
git init
git remote add origin git@github.com:xxxx/xxxx.git
git add .
git commit -m "First commit"
git push -u origin master

Note: You are erase the git files of the old repository completely by using
rm -rf .git before git init
And you may have a problem while you push it to the master, and if the error is like the following:

1
2
3
4
5
6
7
8
To https://github.com/xxx/xxx.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/xxx/xxx.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

You can fix this problem by pushing it to the git forcedly using command
git push -u origin master -f
If you are pushing a folder to the repository which contains some files already, you need to merge the local branch with the remote branch first. The command should be:
git pull --rebase origin master

How to clone an existing repository from GitHub

1
2
3
mkdir myFolder
cd myFolder
git clone git@github.com:xxxx/xxxx.git

You may need to list the remote branch, and you can do that by using:

1
2
git branch -a (shows all local and remote branches)
git branch -r (shows only remote branches)

Then you can download the repository from GitHub using
git checkout branchName && git pull
(Switch to the targeted branch and pull the code)

This post is still updating through my programming experience. 🙂 Feel free to leave a comment and tell me which problem you are facing with Git. It will be good to configure that out together. 😀


Comments: