Git remote repository
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed how to rewrite git history.
https://cloudaffaire.com/git-rewrite-history/
In this blog post, we will discuss git remote repository. We will use GitHub as our remote repository in this demo.
Git remote repository:
Demo:
Create a GitHub account and login to your GitHub account. Create a repository in your GitHub account.
Copy your remote repository https link
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#login to your local instance and clone the remote repository (will ask for your GitHub credentials) git clone https://github.com/CloudAffaire/mygit.git && cd mygit #check remote repository git remote -v #add your user name and email to git config git config --local user.name "Debjeet" && \ git config --local user.email "debjeet@cloudaffaire.com" #create a file and commit in local repository echo "hello from local_master" > local_master git add local_master && git commit -m "added in local repo master branch" #check git log git log --oneline #push the change in remote repository (will ask for your github credentials) git push #check git log (HEAD -> master, origin/master) git log --oneline |
Create a file and commit in remote repository
file name: remote_master
file content: “hello from remote_master”
commit message: “added in remote repo master branch”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#pull the changes in local repo git pull #check git log ((HEAD -> master, origin/master) git log #check local repo working tree (file remote_master has been pulled) ls #create a branch in local repo git branch local_slave #switch to slave branch git checkout local_slave #create a file in local local_slave branch and commit echo "hello from local_slave" > local_slave git add local_slave && git commit -m "added in local repo slave branch" #push the branch in remote repository (will ask for your github credentials) git push --set-upstream origin local_slave |
In remote repository, one branch named local_slave has been created due to above push.
In remote repo create another branch remote_slave
Create a file in remote remote_slave branch and commit
file name: remote_slave
file content: “hello from remote_slave”
commit message: “added in remote repo slave branch”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#switch to local master branch git checkout master #pull remote_slave branch from remote repo (will ask for your github credentials) git pull origin remote_slave #check files (remote_slave file pulled from remote repo) ls #merge local slave with master git merge local_slave #delete the local slave branch git branch -d local_slave #check files (local_slave file added under master branch) ls #push the changes to remote repo (remote_slave file pulled from remote repo) git push #check remote repo master branch(all 4 files exist) #delete remote branches (will ask for your github credentials) git push origin --delete remote_slave git push origin --delete local_slave #check logs git log --oneline #check remote repo (local_slave and remote_slave branch deleted) #cleanup (clear commits in remote repo) git checkout --orphan clean_branch git add -A # Add all files and commit them git commit -m "Cleanup" git branch -D master # Deletes the master branch git branch -m master # Rename the current branch to master git push -f origin master # Force push master branch to github git gc --aggressive --prune=all # remove the old files #cleanup (clear files and commits remote repo) cd .. && rm -rf mygit git init mygit && cd mygit echo "Demo git repository" > README.md git add README.md && git commit -m "README.md added" git remote add origin https://github.com/CloudAffaire/mygit.git git push -u --force origin master #cleanup (remove remote repository reference from local repository) git remote rm destination git remote -v #add remote origin: git remote add origin https://github.com/CloudAffaire/mygit.git #fetch remote repo: git fetch origin remote_slave #merge the update from remote repo to local repo: git merge origin/master #instead of fetch and merge we can also pull from remote repo: git pull Hope you have enjoyed this article. |
To get more details on git, please refer below git documentation