As we seen in our last two posts “How to create git repository in Github” and “Starting your first Git Repository” we created a local git repository with helloworld.c as source inside that repository, and we also created an empty git repository on github website.
In this post we will push our already existing local git to remote server github.
$ cd git-helloworld/
$ cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true editor = vim [user] name = Developer Name email = [email protected]
Now, we will add the remote repository we created at github to our local git as
$ git remote add origin https://github.com/lynxbee/git-helloworld.git
$ cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true editor = vim [user] name = Developer Name email = [email protected] [remote "origin"] url = https://github.com/lynxbee/git-helloworld.git fetch = +refs/heads/*:refs/remotes/origin/*
As, we can see above, the remote github repository got added to config file with remote name “origin” [ origin is a standard name if cloned remote git, you can use any name if you are adding ]
Now, lets try to push our code to github using below command,
$ git pull origin master --allow-unrelated-histories From https://github.com/lynxbee/git-helloworld * branch master -> FETCH_HEAD Merge made by the 'recursive' strategy. README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md
Note: Refer “Solved : fatal: refusing to merge unrelated histories” to understand why we used –allow-unrelated-histories flag, which otherwise is not required to be used with git pull command.
As we can see above, “git pull” command downloaded remote git contents and merged those to local git, which we can see using “git log”. Below, first and third is the local git commits where as second is from remote server git.
$ git log commit 4d775269ad1a7906cb51ed84382ad5ce1e6daf3d (HEAD -> master, origin/master) Merge: 76eb2de f96a4d7 Author: Developer Name
Date: Thu Jun 20 19:33:08 2019 +0530 Merge branch 'master' of https://github.com/lynxbee/git-helloworld commit f96a4d72ebcbbf99ed3c0b9495c87ab025dcebdf Author: Linux Developer Date: Thu Jun 20 08:29:12 2019 +0530 Initial commit commit 76eb2dec9f4749f550ebca7302d12a84f5b972db Author: Developer Name Date: Tue Jun 18 09:22:49 2019 +0530 This is first commit with helloworld.c
Now, we can push our local git to remote server as,
$ git push origin master Username for 'https://github.com': YOUR_GITHUB_USERNAME Password for 'https://[email protected]': Counting objects: 5, done. Delta compression using up to 8 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (5/5), 664 bytes | 664.00 KiB/s, done. Total 5 (delta 0), reused 0 (delta 0) To https://github.com/lynxbee/git-helloworld.git f96a4d7..4d77526 master -> master
We can now see at https://github.com/lynxbee/git-helloworld that we have successfully pushed local code to remote server.