3 Git Area
- The working tree is a single checkout of one version of the project for you to use and modified.
- The staging area is a file, generally contained in you Git directory, that stores information about what you are going to commit.
- The git directory (repository) just contain all those committed files and the snapshot of files in staging area permanently.
git revert
Revert operation will take the specified commit, inverse the changes from that commit, and create a new “revert commit”.
git reset
To revert to the specific commit and delete the following commit after that.
But in this case we may meet the problem of “Head detach” problem. Since the current Head is not belong to the current branch. To fix this problem. We need to check out to the branch we want and commit the previous commit to this branch.
git reset --hard <commit id>
Unstaging a Staged File
git rest HEAD <filename>
Unstaging all changes
git reset HEAD
git rebase
Rebasing is the process of moving or combing a sequence of commits to a new base commit.
Rebasing is exactly saying : “I want to base my changes on what everybody has already done.”
Usage:
- Rebasing is a common way to integrate upstream changes into your local repository.
-
We checkout to the working branch.(For instance: the upstream branch)
-
Rebase the branch to the current branch
git rebase <local branch>
git checkout
Just check the branch out to the working directory.
git branch
Create a new local branch
git checkout -b <branch_name>
create a remote branch
git push <remote_name> <branch_name>
Delete a local branch
git branch -D <branch_name>
Delete a remote branch
git push <remote_name> --delete <branch_name>
Track the remote branch
git checkout --track <remote_name>/<branch_name>
Get the commit history
git log --pretty=oneline
What if we want to create a feature for the upstream repository and sychronize with upstream repository?
-
We need to set up the upstream remote repo with the below command.
git remote add upstream <upstream repo url>
-
Fetch the remote resource to local
git fetch upstream
-
Create a new local branch for the remote upstream resource
git checkout -b <new brach name> <remote>/<branchname>
-
Checkout to your working branch and merge with upstream
git merge <new branch name>
How to stash the change before we sink from the remote upstream
When we finished the new features for the project, the one thing we want to do is push the changes to the remote upstream repository. However, we may encounter such situation that we may have committed the current changes before we pull the upstream repository. After we sink the remote upstream repository, we need to merge the remote changes into the current local repository, then we need to commit again. Finally , we will need to commit twice at the end of the day.
However. There is something called git stash
At this time, even though you didn’t sink the changes from the upstream, you still can use one commitment to get everything done.
After we add every modified or new files into staging area, then we can use the command :
git stash save "<message>"
So we can save the changes and back to the working directory.
At this time we perform the sink operation.
After that, we can use command to reapply changes and keep it in the stash
git stash apply
Alternatively, we can pop it up and reapply them to the working copy.
git stash pop
If you want to the get the detail of a stash
git stash show
add -p
(–patch) option will show the full diff of a stash.
Revise the most recent commit
We can use –amend option to replace the most recent commitment with revised commitment.
git commit --amend -m <"Commit message">
Leave a Comment