Giter Club home page Giter Club logo

mygitnotes's Introduction

Git and Github

About

Cover

Description

This is just a list of git commands to help me to remember and learn them Those could be a nice help to work together with others in bootcamps like codeio, 4geeks, and so on, or as a professional developer

Menu

PreIntroductory Commands

Check Git Version

  • Useful to see the current git version of if it is not installed at all
git --version

Getting Help with Git

General Help for all the commands in git

git help
man git

Help for a specific command

git help commit

Setting git user

Setting the name

git config --global user.name "Alejandro Salazar"

Setting the email

git config --global user.email "[email protected]"

Setting main branch

git config --global init.defaultBranch main

To see the changes in the git user

  • You can even modify all the current config from there instead of writing the commands again
git config --global -e

Creating Aliases in Git

Creating the alias

  • git config –global alias.newAliasNameInventedByTheUser {command that comes after git}

Simple Alias

Example:

git config --global alias.s "status --short"

Complex Alias

git config --global alias.lg "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all"

Editing the alias

git config --global -e

GIT

Initialize

  • Turn the current folder you are working on into a git repository
  • This command will create the hidden folder .git
  • Create the local repository
git init

Git Status

  • This command gives information about the current branch you are working on
  • It also notifies which files in the current repository are untracked by git
  • It also notifies about the files which are ready to be commited (the staged files)
  • It shows all the files within the repository that have been modified including staged and untracked files

Long Version (A longer description of the current state of the repository)

git status

A shorter Description

  • The “??” symbols means that those files or folders have not been added to stage where is being tracked by git
  • The symbol “A” means the file/folder is already being tracked by git because of it being in the stage area
  • The symbol “M” in color red means the stagged file/folder was modified, and those changes have not being reported to the stage area by the user
  • The symbol “M” in color green means the stagged file/folder was modified, and those changes were already reported to the stage area by the user
  • The symbol “R” means the file or folder was rename
  • The symbol “D” means the file or folder was deleted
git status --short
git status -s

IMPORTANT NOTES!!!!

GitKeep

  • git ignore folders that are completely empty
  • Some projects could need an empty folder to save data generated by the program or user
  • In order to avoid broken the project as a result of the default behavior de git, The user must create a folder with an special file called .gitkeep

Example:

mkdir uploads
touch uploads/.gitkeep

GitIgnore

  • There is going to be times, when you would want git to ignore some files that were created by third parties libraries you used in your project
  • In order to achieve that, we need to create a special git file named gitignore in the repository root
  • Some example of undesired files to keep a track of them are the node_modules, dist and a log of your local machine

Example:

  1. You create the file
touch .gitignore
  1. Inside the file you write which folders or files to ignore
dist/
node_modules/
*.log

Add elements in the staged area

  • Command use to stage the untracked files to be ready for the commit
  • The add command send the element to the stage area. Therefore, the next time you write the previous command, instead of the symbols “??”, it will appear and “A” symbol

Single Element

git add index.html

Add all the untracked files of one type

git add *.html
git add folderName/*.js

Add all the untracked files (Everything)

git add .

Add all the directories within a specific folder

  • The css file contains other directories like bootstrap
git add css/

Remove elements from the staged area

  • Any of the two commands unstaged the desired file
git reset fileName
git rm --cached fileName

Creating the first instant copy of the proyect or staged elements

  • Take a picture of the current repository which saves the current staged files
  • Create a historical register of the repository with all the changes made in the staged files
git commit -m "my new message to identify this commit"

Creating a commit and pushing the elements to the stage area with only one command

git commit -am "Description of the changes for this commit"

Fixing the description message of last commit

git commit --amend
git commit --amend -m "new description"

Reset

Soft

Include the new changes in the last commit instead of creating a new commit

  • Beware! Reset hard delete the changes but the soft one doesn’t
  • you can add a number after the symbol ^ to notify how many commits you want to go back
  • In other words, we preserved all the changes but return back to the desired commit in order to make a new commit which is going to include all those changes instead of creating new commits for the new additions
git reset --soft HEAD^

Mixed

  • This like the –soft state, it is not destructive.
  • It preserve the changes in the repository but unstaged all the changes made after the selected commit
  • you can select the desired commit to return by using the hash/id/identification
git reset --mixed 01cdac6

Hard

Return the project to an earlier stage or copy and Delete the unwanted future!!

  • The alphanumeric thing at the end is just an example about a possible visible value from git log in order to return to the expected git copy
  • This command delete all the copies that comes after the selected commit or copy. You return in the time, as if all those commits have never existed before
git reset --hard ce82803

Reflog

  • This command is used to preserved a log about all the changes made in the repository, including those that where deleted by reset –hard
git reflog

Linux commands with git?

  • The changes in the files made through git are kept registered.
  • Because of it being registered, you can easily recover them by using commands like git reset –hard
  • If you use the typical linux commands to do the same, git will not interpret the changes like rename as what it is but as a creation of a new file
  • That new interpretation is not desired because a rename file is losing all the registered changes it suffered since its creation if you rename the file by other means different than git mv unless you add them in the stage

Rename a file with git (Excellent to keep it in the log of changes)

git mv oldName newName

Delete a file with git

git rm fileName

See all the copies made by git for the project

git log --oneline

Return the project to an earlier stage

Everything

  • Rebuild or return the project exactly as it was in the last commit
  • It restore everything except the untracked filed
git checkout -- .

Just one specific file (last commit)

git checkout -- fileName

Just one specific file to and specific hash (commit)

git checkout 762ac65 fileName

Tags

Upload Tags

Creating

Creating a Tag for Github

  • This first command is not recommended due to lack of information
git tag super-release
git tag 29-10-22v1 -m "First Project Version"
git tag -a v1.0.0 -m "Version 1.0.0 lista"

Creating a Tag for an Old Commit

git tag -a v0.1.0 d2ac819

Show

Show the existing Tags

git tag

Display more information of an specific tag

git show tagName
git show v0.1.0

Deleting Tag

git tag -d nameOfMyTag

Branches

Creating a Branch

git branch nameOfMyBranch

See the existing branches

  • Both commands show the existing branches in the project, although it is more clear in git branch
  • Git branch list all the branches and mark the exact branch you are currently working on
git branch
git log --oneline
git status # only shows the current branch

Rename a branch

git branch -m currentName newName

Change the current branch you are working on

git checkout nameOfMyBranch

Create and immediately change into the new branch

git checkout -b nameOfMyBranch

Merge a branch

  • Return to the main branch and then use the command merge
git merge nameOfMyBranch

Delete a branch

  • The last two commands are used to force the deletion of the branch
git branch -d nameOfMyBranch
git branch -d nameOfMyBranch -f
git branch -D nameOfMyBranch

Git Diff

Show the modifications in the unstaged files

git diff

Show the modifications made in the staged files

git diff --staged

Stash

  • It is used to save the changes you have been working on, but are not ready to be integrated in the main project, and the same time you are forced to integrate into the project everything you have done.
  • For obvious reason you save into the stash all those unfinished changes that will cause troubles in the main project if you integrate them.
  • You only send to integrate the features which are finished and doesn’t cause troubles
  • It is not recommended to create several stash, because it could be hard to integrate them into the project because of the possible conflicts the may arise

Create

Create Stash

git stash

Create Stash with a Description (Recommended)

git stash save "Your meaningful description goes here"

List and show all the stash

Simple

git stash list

Descriptive

git stash list --stat

Return

Return and Delete the last stash

  • Return in to the stash and delete it from the stash list
git stash pop

Return to an specific stash

git stash apply stash@{2}

Delete

All

git stash clear

Only one in specific

git stash drop stash@{0}

Show

  • show all the modification that were done in the stash
git stash show stash@{1}

Rebase

  • Moves the changes from the main branch to the branch you are working on
  • Update your branch with the new content added in main after you created the branch
  • You need to be positioned in your side branch in order to exec the rebase command

Using Rebase to add the new changes from main to your branch

git rebase master

Using Rebase Interactive to merge(squash) commits

  • You can also this command to edit the commits instead of combining them. Just watch the menu

For commits before HEAD (4)

git rebase -i HEAD~4

Using Rebase Interactive to edit a commit (split)

  • If your your last commit could be divided in more commits you can use the rebase interactive to do it
  • Uncommit and unstage the files with reset HEAD^
  • make a regular commit for every file
  • To finish the changes, you execute the command git rebase –continue
  • At the end, you will have divided one big commit into several commits as a result of that
git rebase -i HEAD~3
git reset HEAD^
git rebase --continue

GitHub

Remote

Create Remote

  • Origin is the name for the remote repository
  • We can create several remote repository for one project

Origin

git remote add origin URL

Upstream

  • Upstream is a the parent repository of your origin repository if it was forked
  • It is a read only repository
git remote add upstream URL

Show a list of the Remotes

command:

git remote -v

posible output:

Upload Tags

All

git push --tags

Update local repository

Pull

Locally for Git

Command
  • Use the command pull
Origin (Default)
git pull
Upstream (Other remote)
git pull remoteName branchName
git pull upstream main
Team Work Notes
Pull including Branches References from the remote
  • This will add the information about your team mates branches
git pull --all
List the branches obtained from remote
git branch --all
Change into a team member branch
  • After executing pull –all, you will be able of changing into your team member branch
git checkout yourTeamMemberBranch
Deleting unnecessary branches that were pull from remote
  • The first command only works if the branch has still not being deleted from the remote
git push origin :branchName
git remote prune origin
Warning01

solution:

git config --global pull.ff only
Warning02

solution:

git config --global pull.rebase true

Fetch

  • Update your local repository without forcing you to make a rebase or a merge
  • Basically, it updata your references in order to see the complete history of your remote repertory
git fetch

** Work-flow in GitHub

Feature Branch

Checking another team member branch (work)

  • All the team member will be working in their own branch
  • You can see their work by executing:
git fetch
git branch -a
git checkout theBranchOfYourFriend

Merge your team member branch

  • Any team member can merge another team member branch
git checkout master
git merge yourTeamMemberBranch
git push

Alternative to Merge by usig a Pull Request

  • This is an Alternative to the last point (merge)
  • It is more recommended because a pull request will notify the other team member and start a debate about the merging of that feature branch
git push origin featuredBranch
  • The last command will update that branch in GitHub, allowing the user to start a Push Request from there

Working from a featured branch to main in remote (GitHub)

  • Create the branch, move into it, do all the work there to implement the desired feature and then exec the command git push
  • GitHub will tell you which command to use in order to update remote with your local feature branch
git push
  • Finally you create a pull request and merge your featured branch with the main branch

Clone repository

git clone https://github.com/xandro2021/MyGitNotes.git

Close Issue outside of Github

  • Use the key word “Fixes #” with the number of the issue in github, and it will automatically close the issue
git commit -am "Fixes #5: Done, I've Deleted Capitan Marvel"

Submodules

Tutorial Submodules

  • Basically it is a repository inside another repository.
  • Every repository will have their own history.
  • The parent only knows and notify whenever the child repository is updated.

Local

  • You can add a submodule locally with this command
git submodule add nameOfChildRepository

GitHub

Add

git submodule add URL

Push

git push --recurse-submodules=on-demand

Load the submodule content when you clone the parent repository

  • You need to mv into the child or submodule to execute the command
cd submoduleFolderName
git submodule init
git submodule update

Recursively clone a repository with all its submodules

git clone --recurse-submodules URL

Update a submodule from remote

  • Some times when you clone a repository with its remotes recursively, the references used by the parent repository could be outdated. In order to update those submodules you need to exec the command:
git submodule update --remote

Delete submodule

  • Use the last command to manually delete the section related with the submodule using vim
git rm submoduleFolderName
rm -rvf .git/modules/submoduleFolderName
git config -e
  • Then commit the changes and update remote
git add .
git commit -m "modules deleted"
git push

SubTree

Tutorial Subtree

It Different Than Mergin your current repository with another repository

  • Instead of mixing both repositories, a subtree just add in a folder the other repository similar than submodules

If you want to merge two repositories then the commands could be:

git remote add trick https://github.com/nasa/trick ; git merge origin/master

Installation

  • By Default it comes in a different package in Fedora
sudo dnf install git-subtree

Add a SubTree

  • master is the branch
  • https is URL
  • trick is the folder name in our current repository

Including all the history of the subtree repository

git subtree add --prefix=trick https://github.com/nasa/trick master

Without the history of the subtree repository

  • The squash key word, remove the history of the repository we are cloning as a subtree
git subtree add --prefix=trick --squash https://github.com/nasa/trick master

Change the version of the SubTree

  • Maybe to an older or newer version
git subtree merge -P trick --squash ba2df07

WorkTree

mygitnotes's People

Contributors

xandro2021 avatar

Stargazers

Muneeb ur Rehman 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.