Git Cheat sheet
Git commands which software development teams uses/come across frequently.
- Create new remote repository and add it to local repo:
- First create a repository on GitHub and copy it’s URL.
- Initialize a local folder/directory as Git repo .
$ git init
- Add new remote repo to local directory.
$ git remote add origin https://github.com/Akankshap38/React-Projects.git
2. Get status of all changes made on local repository:
$ git status .
3. Add all the changes to remote repository:
$ git add .
4. Committing and pushing to remote repository:
$ git commit -m "A short message for list of changes made"
$ git push
$ git push -f #to forcefully push regardless of warning
5. New branch creation: git branch <branch-name> creates the branch and git checkout <branch-name> changes the current branch to newly created branch.
$ git branch first-react-project
$ git checkout first-react-project#Shortcut way...just need one command
$ git checkout -b first-react-project
6. Pushing new local branch to remote
git push --set-upstream origin <local-branch-name>
7. Copying the content from one branch to another:
- Copying content of another branch into current branch locally.
- Committing the changes
- Pushing the modifications
$ git checkout master . #copies content present in master to present branch
$ git commit -m "Copied project from master branch"
$ git push -u origin first-react-project
8. Delete a branch: Ensure you are currently out of the branch which is to be deleted.
$ git branch -d first-project #deletes locally
$ git push origin --delete first-project #deletes remote branch
9. List branches for the repo:
$ git branch
10. Overwrite content of master with content of another branch: Suppose I want to overwrite all content of master with that of ‘learner-react-projects’ branch.
$ git checkout learner-react-projects
$ git merge -s ours master
$ git checkout master
$ git merge learner-react-projects