:::: MENU ::::

Thursday, April 14, 2022

To be advanced user of Git, a developer needs to be master on these topics like undoing change before committing, handling Git branches, merging techniques, rollback in critical scenario. 


Making modified files to unmodified:

$ git checkout [file_name/directory]
Description: It reverts changes to a modified file before it gets staged. 
Use case:
For example, A user has made the change in the file named all_check.py. After making the change, all_check.py doesn't work properly as the user desired. 
So user thinks that the unchanged file was better. In this case, this command is a handy tool for undoing and reverting to the latest committed or staged file.
$ git checkout -p [file_name]
Description: It is similar to the previous one, except it helps to revert change line by line.

$ git restore [file_name/directory]
         It is same as git checkout [file_name/directory].

Another use of the git checkout command:
$git checkout -b new_branch
        it will create newBranch from current working tree pointer (head).

$ git checkout <commitId>
        working-tree head will go to that specific commit-Id, which is the so-called 'detached head'. If anyone want to develop or change any file or anything, he/she has to make a new branch and do commit to changes.

$ git checkout -b new_branch
        it will create a new branch on the detached head pointer.

Making staged files to unstaged files

$ git restore --staged [fileName/directory]
        it makes staged files unstaged.

$ git reset head [file_name]
Description: It makes the staged file to unstaged.
Sometimes, we stage all files located in the current working tree. So, it is a common situation where git reset Head [filename] would make a great effect.
$ git reset -p head [file_name]
Description: Similar to the previous one, except it helps to un-staging file by file with more options. 



Override Commit message:


$ git commit --amend
Description: It overrides the previous commit. It takes all staged files together for overrides commit.
It also helps to modify the previous commit message. It should be used in a local repository. Because the use git commit --amend in shared or remote repository can make confusion to others.
Precaution: Avoid amending commits that have already been made public.


RollBack:

With git revert, a new commit is created with inverse changes. This cancels previous changes instead of making it as though the original commit never happened.

$ git revert head

Commit id is 40 characters long and it is calculated using SHA 1

$ git revert [commit-id]
this command helps to roll back to the provided commit id.


Subsidiary commands: 
$ git show
$ git log
$ git commit -a -m [commit-message]


git reset is another rollback mechanism of git. But it have significant difference between git revert and git reset. Extra precaution is needed while executing git reset commnads. Because it causes of losing changes of without knowing of its implication. 
Points to be rememberred:
  •        git reset can be applied on specific commit id.
  •     git reset [commit id], it means to reset all changes from head pointer upto given commitId.

There are three types of reset commands.
        git reset --soft
        git reset --mixed
        git reset --hard


git reset --soft [commitId]
        commit will be discarded and modified files will be at staged area.
       
git reset --mixed [commitid]
        this flag is default flag of git reset command. Whenever it executes, all the modified files go to unstaged area and commits are discarded.

git reset --hard [commitId]
        this is most dangerous commit, because it removes all changes completely.



Git  Branch


Branches and merge:

What is a branch in git:
A pointer to a particular commit.

The default branch that git creates for a newly created repository is called master.
By creating a new branch, we can experiment without breaking what already works.


$ git branch 
Description:
It shows the all existing branches.
$ git branch [name] 
Description:
creates a branch with name
$ git checkout [branch-name]
Description:
switching to new branch.
Note: We can use git checkout to checkout the latest snapshot for both files and for branches.
 
 
$ git checkout -b [new-branch-name]
Description: 
It helps to switching to new branch, after creating branch.  

$ git branch -d [branch-name]
        Description: delete the [branch-name]


Merge

What is merge in git:
The term that git uses for combining branched data and history together.
Git uses two different algorithms to perform a merge:
1. Fast-forward
2. Three-way merge
If two lines have differences Git is unsure about, it's best we decide than risk losing work forever.
$ git merge [branch-name]
Description:
It merges the given branch with the current checkout branch.


Rename Git branch name (both local and remote)

Step 1: First checkout the branch
    $git checkout <old_name>

Step 2: Rename the branch name at local
    $git branch -m <new_name>

Step 3: If the remote branch has already created. Push the <new_name> local branch and reset the upstream branch:
    $git push origin -u <new_name>

Step 4: Delete the old remote branch.
    $git push origin --delete <old_name>

0 comments:

Post a Comment