Manage Git Repository in Magento 2
In this blog I'll explain you how to create git repository and how to manage branch, how tp Manage Git without username and password and also, give you an list of other git commands in a single post.
Step 1: Create a repository
https://bitbucket.org/
https://github.com/
Step 2: Create a .gitignore file and add the below code.(Ref link)
etc/*
.htaccess
autoload.php
bootstrap.php
functions.php
Step 3: Switch to your directory
cd /path/to/your/repo
Step 4: Connect your existing folder to the git repository
git remote add origin git@bitbucket.org:sample/app.git
git push/pull origin master
Or Clone git repository
Using https:
git clone https://sample@bitbucket.org/sample/app.git
Using SSH:
git clone git@bitbucket.org:sample/app.git
Specific Branch:
git clone git@bitbucket.org:sample/app.git -b branch_nam
Manage Git without username and password
Create SSH file in local/server (Ref link)
ssh-keygen
ls ~/.ssh
eval `ssh-agent`
ssh-add ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub
ssh -T git@bitbucket.org
Add your SSH public key to bitbucket.
Please refer to this link for more details to generate different types of a key like (rsa, dsa, ecdsa, ed25519).
Step 5: Git config
git config --global user.name "[name]"
git config --global user.email "[email address]"
Step 6: Check/Create/Switch Branch
git branch
git branch checkout -b branch_name
git branch checkout branch_name
git branch -d branch_name
Step 7: Update changes on staging branch
git status
git add .
git add --all
git add /file/path
git add /file/path /file/path
git commit -m "Type in the commit message."
git push origin staging_branch_name
Step 8: Get updates on master branch
git pull origin master_branch_name
Best practice to manage the git process in any project.
- Manage 3 environment and create 3 branches for that.
- Development
- Staging
- Master
- Once the development process is complete push all those changes to the development branch.
- Pull the development branch in a staging environment and test it. If everything is working then push on the master branch.
- Pull changes from the master branch.
General Git Commands
- git clone [url]
- git init
- git add
- git commit -m "Type in the commit message."
- git diff
- git diff
- git diff –staged
- git diff /file/path
- git diff [first branch] [second branch]
- git log (Display commit log)
- git remote set-url origin git@bitbucket.org:sample/app.git
- git remote -v
- git –help command
- git help -a
- git help -a | grep credential-
- git reset
- git reset [file path]
- git reset [commit]
- git reset –hard [commit]
- git rm [file]
- git show [commit]
- git checkout
- git merge [branch name]
- git config
- git push origin branch_name
- git push –all origin
- Git pull origin branch_name
Command Reference
https://dzone.com/articles/top-20-git-commands-with-examples
That’s it.
I hope this post helped you to find what you were looking for. Bookmark it for your future reference. Do comment below if you have any other questions on that.
P.S. Do share this note with your team.
Review other articles maybe it'll help you too.