Giter Site home page Giter Site logo

git-notes's Introduction

Notes for git/github

This is a note/cheatsheet written as a reference for how to create a git repo and link it with a remote repo, and how to create a branch, and merge it with the main branch.

Html version here: https://kai-lim.github.io/Git-notes/

Step 1: Create a directory and initiate git version control in it

mkdir test #create a new directory
cd test #change directory, enter the "test" folder
git init #initiate a new git repo in the directory

Step 2: Go to Github and create a new repo

Avoid creating a README file at this stage to avoid potential issues when linking the local repo to this remote repo.

Step 3: link the local repo with the remote repo

In this example, the link is https://github.com/kai-lim/test.git

If there are already some files in the repo, add them and commit

git add .
git commit -m "first commit"

Now create the main branch, add the remote repo, and link the main branch to the remote repo

git branch -M main
git remote add origin https://github.com/kai-lim/test.git
git push -u origin main

Use git status to check if everything is okay, output should be something like below:

(base) MacBook-Pro:test kai$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

Step 4: Creating branch

To create a branch, use git checkout. However, before doing anything new on the local repo, it's always a good practice to do git pull

git pull

Output:

Already up to date.

Now let's use git checkout to create a new branch called twigs. We use -b to checkout to a new branch named twigs, which is branching from the main branch.

git checkout -b twigs main

Output:

Switched to a new branch 'twigs'

Use git branch to check which branch you are at. Right now I am at twigs:

  main
* twigs

Now, within the branch you can create a new file, say a txt file called "hello" that contains the text "Hello". In practice, this can be you working on RStudio, creating or changing scripts.

echo "Hello" >> hello.txt

Check on your folder if this file has been created. :)

Step 5: Committing changes in branch

Now we will commit this hello.txt file.

git add hello.txt
git commit -m "commit branch"

Step 6: Switching branch

Now let magic happen! We are going to switch back to the main branch and see if the "hello.txt" file will still exist on the main branch.

git checkout main 
ls

We are at the main branch now, and when we type ls or check on the GUI, the hello.txt file is gone! Fret not, it is safely in the twigs branch. Don't believe it? Let's switch to the twigs branch.

git checkout twigs
ls

Tips: Remember you can always use git branch to check which branch you are at!

PS: If you are bored, you can open the terminal and your finder side-by-side, and type git checkout main and git checkout twigs alternatively to see how the file appears and disappears! Magic!!

Step 7: Linking branch to remote repo

This is not the end yet, as our local branch is still not "connected" to our remote repo. To do this:

git push -u origin twigs

Output:

To https://github.com/kai-lim/test.git
 * [new branch]      twigs -> twigs
Branch 'twigs' set up to track remote branch 'twigs' from 'origin'.

On your Github repo, you should be able to see this notification:

Option 1: Merge on Github

Click on "Compare & Pull Request", then "Create pull request". This will create a pull request (often known as PR). If you click on "Merge pull request", the twig branch will be merged to the main branch.

Option 2: Merge locally on command line

This is another possible option but personally I prefer option 1.

First, go back to the main branch, then use git merge to merge the twigs branch to the main branch:

git checkout main
git merge twigs

You should get something like:

Updating 31e014e..68ffcbc
Fast-forward
 hello.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt

Now if you git checkout to main, you should be able to see hello.txt in the main branch.

git checkout main
Already on 'main'
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)
ls
hello.txt

Step 8: Git push merged branch to remote repo

Now you can use git push to push the merged branch to your remote repo.

Step 9: Delete merged branch

To delete a branch locally, do git branch -d <branch>.

To delete a branch remotely, do git push <remote> --delete <branch> or git push <remote> :<branch>

# delete locally:
git branch -d twigs

# delete remotely:
git push origin :twigs #spacing is important here, note that there is a space after "origin" and no space before "twigs"

Output when local branch deleted:

Deleted branch twigs (was 68ffcbc).

Output when remote branch deleted:

To https://github.com/kai-lim/test.git
 - [deleted]         twigs

Bonus: Doing this on RStudio

I have found a few links/resources that can help us doing this more seamlessly on Rstudio:

  • Create a new branch on Github
  • Git pull on RStudio
  • Click on "main" or "master" dropdown menu at the Git panel, the new branch should be visible
  • Clicking on the new branch will help you switch to it!

Links that may be useful:

  1. Happy Git with R

  2. Git cheatsheet from Atlassian

  3. Git cheatsheet from freecodecamp

  4. Git cheatsheet from Github Education <- this is my fav!!

git-notes's People

Contributors

kai-lim avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.