In this blog we will see the basic life-cycle of a file in git. Refer below diagram.
Round 1 Changes
I have created the ‘git-learning’ repository using GitHub. Initialized the repository using the README.md file. That is my initial commit on the master branch. See below unique commit id: 1d4ce0714c2e9a8d873c1a5e97cc0ce7bc12fd1b
As of now we have only one branch that is master, we can create other branches also, that we will cover later.
I have cloned the repository & run git log list down the version history for the current branch i.e. master. Showing all information like when & who performed the commit.
Lets do some changes in README.md file. After that run git status
So after changing the file, git saying there is ‘Changes not staged. Use git add to update….’ i.e. there are some changes in your working area. Let’s run the git diff to see the changes.
Let’s run git add command to move the changes from working to staging area & see the git status again.
Here if you notice that, git is saying you are up to date with master. You have staged your changes, now ready for commit. It is also showing changes to be committed.
Here if you run git diff, it will not show any data, as you have pushed changes in staging area. You have to run the git diff –staged
Let’s commit our changes using git commit command. This will move the changes from staging area to local repository & records file permanently in version history.
You have committed your changes. Permanently noted in version history locally. This commit is not available in remote repository. Check latest commit id using git log.
Notice below screenshot the red line ‘origin/master, origin/Head‘ – is on previous commit. Which is remote repository latest commit. Local repository latest commit is on top, that we have done above.
You can compare the commits using git diff <new-commit-id> <old-commit-it>
Final thing is to push the changes in remote repository using git push
git push is done i.e. local commits uploaded to remote repository. Checking remote repository:
Check git log now. After push, notice the red line ‘origin/master, origin/Head‘ – is now present on latest commit.
Round 2 Changes
I have added two new files & changed the README.md
Checking git status
Checking git diff
Moving all changes to staging area: git add . –all
Commit & Checking git log
Final git push.
Restoring Files (git restore)
When you have changed file in workarea & want to restore with original one. Run git restore <file> command.
When you have staged the file in staging area & want to restore. Run git restore –Staged <file> command.
Reset Commit (git reset)
I have commit the change in local repository & commit got generated f975da9d4ecb7c45db061b72fd1b450fd61cfa0f. Now want to reset the commit.
Lets reset to last commit. i.e. 179359643be893c489a61b50233752a299b01f4a
Lets run git reset –soft HEAD1 command to reset to last commit
Provide HEADn for nth last commit.
Happy Learning! Your feedback would be appreciated!Follow @shobhitsinghIN