GIT – Useful Commands
GIT is a code versioning tool which allows teams to work on projects at the same time.
Firstly, open command prompt and locate your project directory. If git is already installed then the following commands can be run.
Status
This will tell you what changes have occurred.
git status
Add
Staging files which can then be committed to your local repo.
// This will add individual files
git add myfile.js
// This will add all changed files
git add -A
Commit
Commit will commit all your locally staged changes to your local repo.
git commit -m 'I made a change to something'
Push
Push is used to push your locally committed changes to the remote repo. You may be prompted for a username and password for the remote repo.
git push origin branch
Branch
Branch is used to display which branch you are currently working on.
// which branch you are ongit branch
// Fetch a new branch
git fetch origin myBranch
// checkout one of the branches available
git checkout myBranch
Undo a remote Commit
// revert the local checked out branch back to a commit.
git reset --hard ID-OF-COMMIT-TO-REVERT-TO
// force push to overwrite the repo branch with your remote one.
git push --force
Only merge certain files from another branch
// checkout the branch you want to pull file changes togit checkout branch1
// checkout JUST the file you wish to merge into branch1
git checkout branch2 src/filename.js
// check status to see the new file that is unstaged
git status
// then add and commit the new file change
git add -A git commit -m 'Added new file to branch1 from branch 2'