Category: Git

Git- undo working directory and amend commits

Undo working directory

If you want to undo any changes to working directory, user following command-

git checkout -- file.txt

Use — to undo a file in a current branch.

You may checkout specific commit or old version, use following command.

To see the commit user-

git log

Copy the first 10 characters of SHA and use following command to checkout a specific commit-

git checkout sha121212 -- file.txt

Where sha121212 is a SHA of the specific commit.

Amend commits

If you want to amend the commit i.e. something in the repository, you may do that using amend command. Note only the last/recent commit i.e. the HEAD point to can be amended and in this case date and SHA will be changes along with message, use following command-

git commit --amend -m "message goes here"

You may check the commit changes in log-

 git log

If you want to revert the current commit use following command-

git revert SHA21121

SHA21121 is the SHA for that commit.

Git workflow or three tree architecture

Git follows following three tree architecture.

Working – is the working directory where the file changes are made. For the first time you either push the changes to the repository and following changes you may pull the repository copy to your working directory

Staging – before the work is been committed to repository the changes are prepared for commit and sits in a staging index.

Repository – once the changes are staged these can be committed to repository.

gitarch

Git- add (stage), commit and unstage changes

Before this if you want to see Git workflow/ three tree architecture

Stage changed files
To add the changes to the staging index from working directory use following command-

 git add .

This will stage all changed files (.) in working directory
To add specific file to staging index-

 git add filename.txt

Stage and commit at once

Per above this is 2 stage process to commit changes to repository. If you have all files that are ONLY modified use following command to directly commit from working repository. This command can also be used for renamed file.

 git commit -am "provide a message here"

So no need to explicitly add to the staging index, instead the above command shall update repository.

Commit changes

Going back to committing changes, use following command-

 git commit -m "message goes here"

Please note the best practice to put the message on what is this commit about and the message to be in the present tense.

Unstage changes

 git reset HEAD fiel.txt

If you put something in staging directory and don’t want there, then use reset head.

GIT – diff, remove and rename

Before this if you want to see Git workflow/ three tree architecture

To check the difference between Repository and working directory using following command-

git diff

To get the difference in words instead  of whole statement highlighted with diff, use following command-

git diff --color-words

To check  staged changes difference, use following command-

git diff --staged

To delete file from working directory and from repo, use following command-

git rm file-to-delete.txt

The file will be staged with this command so you dont need to explicitly stage the deleted file.

Commit this file-

git commit -m "file deleted."

Please note while doing so the files will be permanently deleted and not found in thrash to recover. So use this command wisely if the file is not in the repo for you to pull back.

Rename file in repository and working folder, use following command-

 git mv filebeforerenaming.txt fileafterrenaming.txt

This shall rename file and stage the changes. So this is not required to manually staged.