Git
Git Basics
Git is a version control system that lets you track who made changes to what when and has options for easily updating a shared or public version of your code on github.com.
```git
git add .
git commit -m "Why I did this change"
git push <remote> <branch> OR git push origin master
Project history:
# See the list of all the commits that have done so far : git log git lg
```
New Features
When you want to start a new feature. You have to create a new branch to work from:
```git
(master) git status (CLEAN)
(master) git checkout -b my-feature
(my-feature) git add .
(my-feature) git commit -m 'XXXX'
(my-feature) git push origin my-feature
(my-feature) git status (CLEAN)
(my-feature) gh repo view --web
# GO CREATE A PULL REQUEST ON GITHUB
(my-feature) git checkout master
(master) git pull origin master
(master) git sweep
(master) git checkout -b my-new-feature
```
Getting latest changes
When you want to get the latest changes from master on your branch:
```git
(my-feature) git add .
(my-feature) git commit -m 'XXXX'
(my-feature) git status (CLEAN)
(my-feature) git checkout master
(master) git pull origin master
(master) git checkout my-feature
(my-feature) git merge master
```
Solving Conflicts
When you can’t merge your Pull Request due to conflicts. GitHub proposes a web interface to resolve conflicts on Pull Requests. First, try to use their feature. It might ease the process. Otherwise, follow this process:
```git
(my-feature) git status (CLEAN)
(my-feature) git checkout master
(master) git pull origin master
(master) git checkout my-feature
(my-feature) git merge master
```
Now it’s time to fix all the conflicts. You have to open your editor and fix them. Locate them with $$ cmd + shift + f $$.
```git
(my-feature) git add .
(my-feature) git commit -m "Fix conflicts"
(my-feature) git push origin my-feature
```
Go on GitHub and merge your Pull Request.
```git
(master) git pull origin master
(master) git sweep
(master) git checkout -b my-new-feature
```
Fix an accidental commit to master
This operation can be done ONLY if you HAVEN’T PUSHED to GitHub yet! When you accidentally committed some changes to master and want to move them to a new branch. You have to create a new branch for your feature. This branch will contain your commit. Finally, you’ll have to remove your commit from master. Here are the steps:
```git
(master) git checkout -b my-feature
(my-feature) git checkout master
(master) git reset HEAD~ --hard
(master) git checkout my-feature
```
Sharing/Synching your Repository
Ultimately we want to share our code, normally by synching it to a central repository. For doing so, we have to add a remote.
```git
git remote add origin git@github.com:username/existing_project.git
You can list your remotes with:
git remote -v
```
The real powerful thing is that you can add multiple different remotes. This is often used in combination with cloud hosting solutions for deploying your code on your server. For instance, you could add a remote named “heroku” which points to your cloud hosting server repository.
GitHub CLI
GitHub Command Line; Interface. It allows you to interact with GitHub through your terminal. Create a public repository in one command:
```git
my-responsive-profile git:(master) gh repo create
? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name my-responsive-profile
? Description
? Visibility Public
✓ Created repository dmilon/my-responsive-profile on GitHub
? Add a remote? Yes
? What should the new remote be called? origin
✓ Added remote git@github.com:dmilon/my-responsive-profile.git
? Would you like to push commits from the current branch to the "origin"? Yes
✓ Pushed commits to git@github.com:dmilon/my-responsive-profile.git
```
View a GitHub repository web page
Change your current working directory to a local GitHub repository and run:
```git
gh browse # opens this repository GitHub page in your web browser
Dealing with Pull Requests
Open a new pull request based on your current branch (other than the master or main branch):
gh pr create -w # opens the pull request creation page in your web browser
Check out a pull request:
gh pr checkout {<number> | <url> | <branch>} # fetches and checks out the branch of the PR from GitHub
View the pull request page of your current branch (other than the master or main branch):
gh pr view -w # opens the pull request page of your current branch in your web browser
Share code snippets through Gist
GitHub Gist is a pastebin service which allows you to store and share code snippets called gists.
```
https://www.ndpsoftware.com/git-cheatsheet.html#loc=workspace;
Enjoy Reading This Article?
Here are some more articles you might like to read next: