Git uses workflow. Which can be broken into three states:
- Modified
- Staged
- Committed
Heres a quick view of the commands we use to change the workflow in git:
Git add and commit
The command git add
stages the change of file.
We can un-stage the change too. with the following command:
Git Branch
We can checkout to the newly created branch with
Make some changes and add the changed. But after that we need to push the changes to the remote repository:
Alternatively we can use,
It would also push your local newBranch
to the remote repository origin
. However, you’d need to specify the entire remote and branch name (origin newBranch
) for every future push to that same branch.
Resolving Conflicts
Lets say a developer gets the following conflict while trying to push his code to the remote server:
How does he resolve the conflict? Running git status will show the following output:
In order to merge, developer needs to see and compare the changes from other developer. It is good practice to see what branch is causing the conflict. To inspect the branch the developer runs the following:
So the updated code is the commit chore: add feature 1
. And the commit was made by Develper 1
. Now what is the difference in code? Developer 2 runs the following command to see the diff
:
So the conflict is in the file Feature.js
. And in the line of the return statement. Developer 2 changes the code and commits it accordingly and push the change to the remote accordingly.
Developer 2 has now fixed a merge conflict and can create their PR to get the code merged into the main line.
Git HEAD
Git references the current HEAD. Just as soon as we commit new changes in the code, the HEAD changes. We can see the current HEAD in the .git
folder.
Also the hash can be seen if we cat the following path
We can see the branches in remote repositories as well in .git/refs/remotes
Git diff
Git diff is used to see the difference in the
- file
- commit
- branch.
To see the change in a file:
We can compare the different commit as well with the following:
We can compare the changes between two branches as well
Git Blame
Git blame shows the entire history of a file and by whom the changes were made, at which time, at which line and the commit hash.
We can see the specific line numbers changes
We can use the hash key of the commit to and use the git log
in conjunction to have better understanding of a change commited: