Skip to content

Instantly share code, notes, and snippets.

@alsongarbuja
Last active March 4, 2022 16:51
Show Gist options
  • Select an option

  • Save alsongarbuja/8f022fad032ad0780f25fdd88cac826a to your computer and use it in GitHub Desktop.

Select an option

Save alsongarbuja/8f022fad032ad0780f25fdd88cac826a to your computer and use it in GitHub Desktop.
Github commands

Initailizing/Creating a new git repository

Used to create a new git repository

git init

Adding files or folders to the repo

Used to add new files or folders to the repo

  • To add a file
git add filename.extension
  • To add everything inside a folder
cd foldername
git add .
  • To add everything in the whole project
git add -A

Commiting added changes

Used to commit (`i.e to confirm`) the changes made by add comman

git commit -m "message for the commit"

To add and commit everything at once

git commit -am "message for the commit"

*Note: Use this only if you know all the file changes will have the same commit message.*

To commit mutiple line

Used to add commits with multiple lines

git commit
  • A file will open
  • Write your multiple lines commit
  • Save the file and close the file

*For using VS code as your commit editor first run this command*

git config --global core.editor "code --wait"
  • It will set the VS code editor as the git editor

Pulling repo from remote servers

Used to pull the contents from Remote git repo

git pull

Clone the remote repo

Used to clone all the contents of the remote repo

git clone <url-to-remote-repo>

*Note: < > are not needed in the command it is only to visually specifiy that url-to-remote-repo is a dynamic text*

Pushing to the remote repo

Used to push contents to the remote repo

git push

Mergining Github Repo to a already existing project

Used to connect the empty github repo to a project in your local device

  • Initailize the project as a github repo
git init
  • Add the github repo to the project
git remote add origin <url-to-github-repo>
  • Create a branch called main as newer github repo tends to have main as the main branch not master
git branch -M main
  • Add the contents of the project
git add .
  • Commit the changes
git commit -m "message for your commit"

*Note: adding and commiting doesn't have to be done in this extact way you can add and commit in your own preffered way*

  • Push the commits
git push -u origin main

*Note: git push -u origin main is done only for this time and pushes afterwards can be simply git push*

Handy git commands

  • To stash the current works in the project. i.e reverting all the changes back to the last commit temporarily
git stash

*Note: It works only for uncommited but tracked changes*

  • See the current status of the repo
git status
  • See the difference between the older and newly to be commited changes
git diff
  • See the commit history
git log

Branching in git

It means we dublicate the current state of the repo and branch it off the main branch

  • Create new branch
git branch <branch-name>
  • Changing the working branch
git checkout <branch-name>
  • Creating and chaning the working branch at once
git checkout -b <branch-name>
  • Listing the available branches
git branch

Merging branch

Used to merge two branches together

  • First go to the branch you want the merge to be applied
git checkout <branch-name>
  • Merge the branch
git merge <name-of-branch-to-be-merged>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment