It is a course from Google IT Automation with Python Specialization
diff <first_file_name.extension> <second_file_name.extension>
Alternatively
diff -u <first_file_name.extension> <second_file_name.extension>
(<.....> indicates file name)
wdiff <first_file_name.extension> <second_file_name.extension>
diff -u <first_file> <second_file> > <diff_file_name.diff>
patch <file_name.extension> < <diff_file_name.extension>
(All changes are applied to <file_name.extension>)
cp <original_file_name.extension> <duplicate_file_name.extension>
To find all directory with more info(with all hidden files and folders)
ls -la
ls -l .extension/ (example: ls -l .git/)
cp ../<file> . (To do that you must be in subdirectory)
git commit
(It open default text editor where you can write commit message)
- Git directory
- working directory
- staging area
- First files are modified when we changed it
- Then it becomes staged when we mark those changes for tracking
- It will get commited, when we want to store those changes in VCS File stage/cycle - modified, staged,committed
git config --l
git diff a.extension
(just after modification before adding modified files for tracking)
git diff -r HEAD <path/to/file>
HEAD
always refers to most recent commit- The label
HEAD~1
then refers to the commit before it, whileHEAD~2
refers to the commit before that, and so on.git diff HEAD~2
git config --global user.name ="<your_username>"
git config --global user.email ="<your_email>"
git init
git add <file_name.extension>
git add .
git restore --staged <file_name.extension>
git reset HEAD <file_name.extension>
git restore --staged .
git commit
(prompts default text editor,you can write multiline comments)
git commit -m "< write your commit message here>"
git commit -a
(Doesn't work for new files which are not tracked yet,opens in default text editor)
git commit -a -m "<write your commit message>"
git log
git log -n
git log -p
git log -p -n
`git log <directory/file_path>
git show <commit_id>
(<commit_id> is identifier associated with each commit)
git log --stat
git log --graph
git log --graph --oneline
git diff
(for all changes)
git diff <file_name>
(for specific changes in <file_name> file)
git diff --staged
git rm <filename.extension>
(file <filename.extension> will be deleted from your repository)
git mv <a.extension> <b.extension>
(<a.extension> file name is renamed as <b.extension>)
(Note: mv command is also used to move files/folder in linux)
write file name inside .gitignore file
Example
To ignore readme.txt file
echo readme.txt > .gitignore
git checkout <file_name.extension>
(old approach)
git restore <file_name.extension>
(new approach)
(Restore file to last staged/tradked state)
git commit --amend
git add <file_name.extension>
(add or unstage file )
or
git add .
Fillowed by
git commit --amend
(can change commit in editor)
git revert HEAD
(creates new commit with inverse changes, add reason for rollback in commit)
git revert <commit_id_for_snapshot>
git branch
(current branch is indicated by *)
git branch <new_branch_name>
git checkout <new_branch_name>
Note: Before you checkout branch you need to save/commit your changes
git checkout -b <new_branch_name>
git branch -d <branch_name>
Note: to delete branch you must be in master branch
git branch -D <branch_name>
git merge <merging_branch_name>
Note: to merge you must be in your destination branch
- fast forward merge
- three way merge
git merge --abort
Basic Interaction with Github:
- Create new github repository
- Create your account and login to it,
- click on create repository link or plus sign,
- Name repository,
- Add description to repo and is optional,
- Select visibility for repo that is either private or public,
- initialize repo with README,
- Then click on create repository
i. Clone repo make changes and again push to remote
- open your terminal and enter command
git clone <REPO_HTTPS_URL>
- Enter username and password for your github account
- Directory with repository name is created in your local computer
- README.md file is present inside your repo folder
- Edit file
- Stage changes and commit changes
git commit -a -m 'your commit message'
- Then push your modified local repo to github using
git push
orgit push -u origin main
orgit push origin main
ii. If you want to push your local repository to existing repository
- To initialize git working directory use
git init
- If you have README.md file in remote repository then create that file on local repo too using
touch README.md
or
echo "# <your_repo_name> " >> README.md
- To track/stage file
git add <file_name>
for single file
orgit add .
for all files - To make snapshot/version of your changes use
git commit -m '<commit_message>'
orgit commit
enter good commit from default text editor - To stage and commit use
git commit -a -m
orgit commit -a
- To add remote url as origin use
git add remote origin <REPO_URL>
- To push your repo to github main branch use
git push -u origin main
- To push your repo to github <branch_name> branch use
git push -u origin <branch_name>
- To avoid repeated username and password entry:
- Use SSH Key-pair or,
- Use Credential Helper
git config --global credential.helper cache
Note : You need to enter your credential one more time and your credentials are cached for next 15 minutes
4. retrieve new changes from remote and merge to local repository using `git pull ``
- To see status of the remote repository
git status
( we have clone remote repo, thusgit status
shows status of remote repository)
git remote -v
git remote show origin
git branch -r
git fetch
git log origin/<branch_name>
(exanple: git log origin/main
,git log origin/master
)
git merge origin/<branch_name>
git pull
git log -p -n
git status
git remote show origin
git checkout <branch_name>
(This will create copy of remote branch to local)
git add -p
git log --graph --online --all
- If same line or same section of the file is already changed and committed in both remote branch and
local branch,And then if you will try to push the local branch on the remote branch where file is already changed
Then it will throw merge conflict. - To solve merge conflict:
- first pull your remote repository to merge remote changes to local
git pull
- It will throw merge conflict message , for file which has been changed from both remote and local
- This merge is tree way merge, because file are changed in both repo (if file is changed in only one repo, then merge will be fast forward merge)
- Open file in text editor in which merge conflict arises
- Where you can find <<<<<<<, =======, and >>>>>>> conflict markers
- Remove all of the conflict markers and only leave the code as it should be after the merge and save it.
- Then stage that file using
git add .
orgit add <file_name>
- Then commit changes using
git commit
where commit is already written, if you wish all more explanation. - Then push your local merged version to remote using
git push
- git checkout -b <branch_name>(creates branch <branch_name> and switched to <branch_name>)
- Do changes as your requirements
- Stage your changes and commit changes Using
git add .
followed bygit commit -m 'your commit message'
- Do step 3 as per your requirements
- Finally push your branch <branch_name> to remote repository before merging
git push -u origin <branch_name>
- This method addresses changes in single branch only(example remote master branch and local mater branch)
- If same line or same section of the file is already changed and committed in both remote branch and
local branch,And then if you will try to push the local branch on the remote branch where file is already changed
Then it will throw merge conflict and rebase come to action,it avoids three way merge and make history linear. - first fetch your remote repository
git fetch
- To rebase local commit against remote use
git rebase origin/main
- Then it pop up merge conflicts, to solve merge conflict:
- Open file in text editor in which merge conflict arises
- Where you can find <<<<<<<, =======, and >>>>>>> conflict markers
- Remove all of the conflict markers and only leave the code as it should be after the merge and save it.
- Then stage that file using
git add .
orgit add <file_name>
- Then continue rebase
git rebase --continue
- Then see your log, where you can see that commit message are linear i.e local branch commit message rebase remote message(local commit history becomes last history and remote history becomes second last history)
- Then push your local rebased final version to remote using
git push
To learn more about rebase changes click here