s

git for beginner edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 03 December 2021 | 1861

git for beginners

basic commands

Terms: working tree, index / staging-area / cache, current commit / HEAD

****************tracking the local codebase using git**************** 

display version information
> git --version

git help
> git help verb
or
> git verb --help
example 
> git help config
> git config --help

list global configuration variables
> git config --list
 
set global configuration variables
> git config --global user.name "Murugan Andezuthu Dharmaratnam"
> git config --global user.email "murugan@company.com"

****************tracking the local codebase using git**************** 

Initialized empty Git repository ( go to the folder & type )
> git init
adds a .git folder, which contains everything that's related to a repository. 
If you want to stop tracking your code using git, all you have to do is to delete this .git folder

Add the remote repository: Use the git remote add command to connect your local repository to the remote one
> git remote add origin [remote repository URL]

show the working tree status. Displays the list of changed files together with the files that are yet to be staged or committed.
> git status

ignore files you don't want to add to the repository
- create a .gitignore file 

git 
working directory                           staging area                         .git directory (Repository) committed files 

Add files to the staging area
> git add filename.ext
add individual files
or
> git add -A
add all files 

Remove files from the staging area
> git reset filename.ext
remove a file from stage area
or
>git reset
remove everything from staging area

commit 
> git commit -m "message"

undo a commit
> git reset HEAD
OR
> git reset HEAD~ 

view the commit you have made
> git log

Move or rename a file, a directory
git mv oldname newname

remove a file from the working directory 
git rm removes a file from the working directory and the index, and when you commit, the file is removed from the tree.

git rm --cached 
removes the file from the index alone and keeps it in your working copy. if the file was previously committed, 
then you made the index to be different from the HEAD of the tree and the working copy so that the HEAD now
 has the previously committed version of the file, the index has no file at all, and the working copy has the last 
modification of it. A commit now will sync the index and the tree, and the file will be removed from 
the tree (leaving it untracked in the working copy)




****************tracking the remote codebase using git****************

clone repository
> git clone <url>  <local folder path where you want to clone the repository>
> git clone http://abc.com/remote_repo.git .
clone remote repository. . for cloning to current folder
or
> git clone ../ .
clone local repository.  . for cloning to current folder

View information about the remote repository
> git remote -v
> git branch -a

List the changes you have made to the file
> git diff

commit changes to the remote repository
> git pull origin master
origin is the name of the remote repository and master is the branch
> git push origin master
pull any change that has been made since the last time we pulled from the remote repository


****************branch****************
create a branch
> git branch your-branch-name

start working on the branch you have just created
> git checkout your-branch-name

push new branch to remote
> git push -u origin your-branch-name

merge branch with master
> git checkout master
> git pull origin master
> git branch --merged
> git merge your-branch-name
> git push origin master

delete your-branch-name after merging
> git branch -d your-branch-name after merging
local
> git push origin --delete your-branch-name 
delete remote branch


https://www.youtube.com/watch?v=HVsySz-h9r4

Difference Between Head Working Tree and Index

it's simpler to explain with a diagram.

Ways to unstage the files that were staged by the command


Terms: working tree, current commit / HEAD, index / staging-area / cache,
There are three ways to unstage the files that were staged by the command 'git add'
>git rm --cached <file>
>git restore --staged <file>
>git reset <file>
What's the difference between 'git rm --cached', 'git restore --staged', and 'git reset' if you run it you get similar output


commit holds a snapshot of all files that Git knew about, as of the form they had when you said to commit them;
the snapshot is made from the files that are in Git's index, aka staging-area, aka cache (three terms for the same thing); and
git add means to make the copy in the index/staging-area/cache match the copy in my working tree

  • git rm --cached file: removes the copy of the file from the index / staging-area, without touching the working tree copy. The proposed next commit now lacks the file. If the current commit has the file, and you do in fact make a next commit at this point, the difference between the previous commit and the new commit is that the file is gone.
  • git restore --staged file: Git copies the file from the HEAD commit into the index, without touching the working tree copy. The index copy and the HEAD copy now match, whether or not they matched before. A new commit made now will have the same copy of the file as the current commit. If the current commit lacks the file, this has the effect of removing the file from the index. So in this case it does the same thing as git rm --cached.
  • git reset file: this copies the HEAD version of the file to the index, just like git restore --staged file.

Note: git restore, unlike this particular form of git reset, can overwrite the working tree copy of some file, if you ask it to do so. The --staged option, without the --worktree option, directs it to write only to the index.

Merging & Resolving Merge Conflicts

some basic commands

first checkout the branch you want to merge into 

git merge branch-name 

git merge --about 
if you want to about the merge