Definition

⇎ Repositories is just a container for a project you want to track with Git

⇎ Commit : Save point

⇎ Modified : file created or modified

⇎ github is a service that lets us set up hosted repositories , remote repositories

⇎ central online repository which multiple team-members could access

⇎ myProject folder

⇎ -- .get

⇎ -- index.html

⇎ -- style.css

Modified Staging Committed
index.html add any changed files to staging
area that you want to commit

⇎ git --version : check the installed version of git

⇎ config git username and email

⇎ git config --global user.name "Your Name"

⇎ git config --global user.email "you@example.com"

⇎ git config --global user.name : get the name

Commandes

⇎ git init // turn the folder into a git repo ( folder tracked with git )

⇎ git status // if = red files changed and ready to be staged // if = green files staged and ready to be committed

⇎ git add index.html // add ' index.html ' file to the staging area

⇎ git reset // remove all files from staging area

⇎ git rm --cached index.html // remove ' index.html ' file from staging area

⇎ git add . // add all files to the staging area

logical points after developing certain features of a website or application - we can split our work by different commits

⇎ git commit -m "added index and style" // commit ( save ) files , 'm' stands for message

⇎ staging area allows us to review the changes before committing them

⇎ git log --oneline // each commit in one line

⇎ git log // history of all commit

Rename the local master branch to main

⇎ git branch -m master main

// if it is not working try to commit something to the master branch and try again

Add Submodule

⇎ git submodule add <repo-link> // repo inside repo

Undo

undoing things (order of danger)

Checkout commit (safe)

Revert commit (little safe)

Reset commit (unsafe)

⇎ git log // get all the commits with their id

⇎ git checkout af6b84c // af6b84c : commit id (detach this commit from the master branch)

and see how the code is looks like before this commit this does not affect the commit history just readonly

⇎ git checkout master // reatach to the master bracnch ( like undo : Ctrl+z) back to normal

after checking the code go back to the normal state


⇎ git revert af6b84c // af6b84c : commit id, :wq ( undo one particular commit (this commande do a commit for us just revert the given commit 'add border' become 'Revert "add border"' ))

revert the giving commit (af6b84c)


⇎ git reset af6b84c // af6b84c : commit id ( remove all the commit after this commit id but not changing the files)

⇎ git reset af6b84c --hard // af6b84c : commit id, --hard // remove the changes after this commit id - reset the code to a particular commit

Branches

⇎ when creating a new repository it also creates a master branch

⇎ master branch represent the stable version of the code which will be released or published

⇎ if we need to add a new feature to our code we add it to a new branch and if we like it we can merge this branch with the master branch

⇎ if we have two developers working on the same project they can create a new branch for each feature and when they done we can merge them to the master branch

⇎ we can also delete the branch

⇎ git branch screenFeature // create new branch

⇎ git branch -a // display all branches

⇎ git checkout screenFeature // work in the screenFeature branch

⇎ branche is like copy past the same project and work on it

⇎ git branch -D screenFeature // delete a branch (not merged ? -D : -d)

⇎ git checkout -b screenFeature // create and work in a new branch 'screenFeature' ( just a shortcut)

Remove file / folder from git

if the file is still in the staging area :
⇎ git reset < file_name > // to unstage the file

⇎ git rm --cached < file_name > // remove the file

⇎ git rm -r --cached < folder_name > // remove the folder

Merging Branches Conflicts

When working with Git, you'll often be switching between branches to work on different features or fixes. Eventually, you'll want to bring your changes back together. This is done by merging branches.

Once you're happy with the changes in a feature branch (e.g. feature-a), it's time to merge those changes back into the main branch (usually called main or master).

⇎ first you have to be in the branch you want to merge into (master)

⇎ git merge feature-a // merge the code in feature-a with master

⇎ git merge feature-b // merge the code in feature-b with master

⇎ " when you create a new branch and change something in the master and change the same thing in the new branch this may create a conflict when you want to merge them "

⇎ after merging, you can push the master branch to GitHub

Github

⇎ if you have a local repository // ( clone a repo create a local repository )

⇎ make sure all is commited : git status

⇎ git push https:// github.com/MohcineDev/gitDes.git master // push files to Github repo with the master branch

⇎ clone by press the clone btn

add alias to a url

⇎ git remote add aliasName https:// github.com/projectname/name.git

⇎ git remote add origin https:// github.com/MohcineDev/gitDes.git // give the url an alias = origin

⇎ push after make changes

⇎ add code to staging area then commit

⇎ git push origin master

update remote

⇎ git remote set-url origin <new-url>

⇎ if you dont have a local repository

⇎ create one in github and clone it

⇎ git clone https:// github.com/MohcineDev/gitDes.git // clone repo and add alias origin

⇎ make changes

⇎ git add . // add to staging area

⇎ git commit -m "added index. file" //commit changes

⇎ git remote -v info about the alias for push and fetch

⇎ git push origin master push master branch files to github

Collaborate

⇎ first make sure you are in master (update our master) and everything is commited

⇎ git pull origin master : pull code from remote master branch and update our local master branch with it

⇎ git checkout -b index-html : create your working branch dont miss with the master

⇎ add to staging area and commit it

⇎ dont merge it with master

⇎ git push origin index-html : push your branch to give your team the chance to reviewing it before merging it with master

⇎ git pull origin master :

⇎ fork means create a copy of the repo from the main account to our account

⇎ clone it make changes commit it push it to github

⇎ click btn 'new pull request' ..



⇎ 1 : fork the open source repo to your account

⇎ 2 : clone the repo

⇎ 3 : create your branch

⇎ 4 : commit your changes

⇎ 5 : push the changes to your account

⇎ Contribute this changes to the original repository

⇎ 1 : click btn 'new pull request' ..

⇎ 2 : see if it can be merged

⇎ 3 : press create pull request

⇎ and the rest is on the repo owner

Git Best Practices

⇎ Always pull before you start working

⇎ Commit often with clear, descriptive messages

⇎ Use feature branches for new developments

⇎ Never commit directly to master/main branch

⇎ Use .gitignore to prevent unnecessary files

⇎ Review changes before committing

⇎ Keep commits atomic (one purpose per commit)

⇎ Use pull requests for code reviews

⇎ Merge with squash for clean history

Using Token

Token authentication requirements for Git operations

using access token instead of password

if the repo is already cloned remove the remote first

⇎ git remote remove < remote name >

add the new remote with the token

⇎ git remote add <remote name> https:// <accress token>@github.com/<username>/<repo name>.git

@Mohcinedev