A basic tutorial on using git for version control, focusing on git native commands. There are some easy-to-use tools that can be an assistance, such as Git Graph
(vscode extension) and Source Control
(vscode built in tool).
Some reference: Official Tutorial, Docs Tutorial, Video Tutorial
git clone https://github.com/Shihao-Feng-98/GitTutorial.git
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
Note: Up to date, you can coding locally.
git add . # add all modified files
git add file_name # add specific file
git commit -m "some descriptions of submission" # add some descriptions
git push origin main # push to the corresponding remote branch, e.g. main
Note: You can add
, commit
, or push
multiple times as you need.
If you want to rollback to the previous version, run
git log # find the commit_id that you want to rollback
git reset --hard commit_id
Now, if you want to rollback to the future version of the current version, run
git reflog # show the command you have been used, find the commit_id here
git reset --hard commit_id
If you want to undo the changes on the working directory, run
git checkout -- file_name
If you want to undo the changes that have been add to stage, run
git reset HEAD file_name
git checkout -- file_name
If you want to undo the canges that have been commit, run
git reset --hard commit_id
git reset HEAD file_name
git checkout -- file_name
When you developing a new feature in a project, the following process should be followed.
git checkout dev # switch to desired parent branch, e.g. dev
git branch dev/feat_a # create a branch, e.g. dev/feat_a
git checkout dev/feat_a # switch to new branch, e.g. dev/feat_a
Note: The "dev/feat_a" branch would created on the remote after implementing 1.5.
When you cooperate with others, a pull request should be submitted. Before merging your branch, you should modify your code after review.
When new changes have been applied to the dev branch, the current merge may fail. Sometimes conflicts should be resolved before the current merge.
TODO: conflict ref
git checkout dev # switch to desired branch
git merge dev/feat_a # merge feature branch to desired branch
git branch -d dev/feat_a # delete the local branch
git push origin --delete dev/feat_a # delete the remote branch
If there is a bug needs to be fixed when you developing a new feature in a project, the following process should be followed.
git stash # save the current code
Please follow 3.1.
git stash pop # recovery code and delete the stash
git log # find the commit_id for bug fixing
git cherry-pick commit_id
git log # list the commit history
git status # check the status of the current local branch
git remote show origin # check the infomation of remote branch
git branch # list local branch
git branch -r # list remote branch
git branch -a # list all branch
git tag # list tag
git show tag_name # check the infomation of the current tag
During development, you would add many commits, but you should combine them into one commit before Pull request to keep it clear.
Use git reset --soft
:
git log -5 # show last 5 commits
git reset --soft commit_id # restore files from repo to cache without changing the local work area
git add -u # add tracked files
git commit -m "new description"
git push --force
Use git rebase
:
git log -5
git rebase -i HEAD~5 # combine last 5 commits into one commit
git push --force
Notes:
- Run
git rebase -i HEAD~5
would enter a editor, change the reserved commit aspick
, change the discarded commit asfixup
. git rebase
would create a new branch. If you make a mistake, you cangit checkout
to the previous branch andgit rebase
again.