Get started
Setting up your username and email
- GIT records your name and email address with every commit you make, so the first step is to tell GIT what these are so you can get credit for your work
| GIT command |
| |
| Sample Usage |
|
Beginner
PLEASE NOTE, Section I and II below, are not to be followed in order, they are 2 separate Actions (one to create a new local git repo to push to remote GIT, and the other to obtain "clone" an existing repo from GIT).
Please use one or the other for your purposes.
NOTE: Using Project DEVOPS for Sample Usage Examples
I. Version controlling your existing project
- "git init" can be used to create a new GIT repository by converting an existing unversioned project to a GIT repository or initializing a new empty repository.
| GIT command |
| |
| Sample Usage |
|
II. Cloning the codebase from remote GIT Bitbucket repository
- "git clone" will copy the entire history of a project so you have it locally and it will give you a working directory of the main branch of that project so you can look at the code or start editing it. If you change into the new directory, you can see the
.gitsubdirectory - that is where all the project data is.
| GIT command |
| |
| Sample Usage |
|
III. Create a new file and push it to the remote Bitbucket Git repository
- use "git
add" to start tracking new files and also to stage changes to already tracked files - use "git commit" to record the items you have staged (using git add),the -m option provides the commit message in which a JIRAKEY is required as a prefix to the commit message
- use "git push" to move/push your changes to the remote repository
| GIT command |
NOTE: Wildcards can be used: To add all files: git add * To add all txt files: git add *.txt | |
| Sample Usage |
NOTE: "git status" used above is to showcase the status changes for each command, it is completely optional to use, but recommended to verify execution |
IV. Deleting a file from the remote Bitbucket Git repository
| GIT command |
| |
| Sample Usage |
|
V. Get updates to your local workspace from a remote repository
You can use two methods
- git fetch & git merge
- git pull
git fetch will update your local remote tracking branches but not your local workspace
git pull will update your local remote tracking branches as well as your local workspace with changes
| GIT command |
| |
| Sample Usage |
|
Intermediate
NOTE: Using Project DEVOPS for Sample Usage Examples
I. Branching and merging
- "git branch" gives you a list of all the branches (use -v for more detail, eg. git branch -v)
- "git branch branchname" is used to create a new branch
- "git checkout branchname" let's you switch to the branch
- "git merge branchname" this let's you merge your current active branch with the other branch
- "git branch -d branchname" this deletes the mentioned branchname
Scenario: An urgent fix is needed to fix DEVOPS-242
| GIT command |
| |
| Sample Usage |
|
II. Creating and using tags
- "git tag" let's you tag specific points in history as important
- "git checkout <tagname>"
- "git describe"
| GIT command |
NOTE: if you want to push all your tags, you can use --tags, eg. git push origin --tags NOTE: you can also create branches from tags, eg. git checkout -b <branchname> tagname | |
| Sample Usage |
|
III. Modifying remote branches
- "git checkout -b <new local branch> <remote branch> , this will create a copy of the remote branch to your local workspace as the "new local branch"
| GIT command |
| |
| Sample Usage |
|
IV. Renaming remote branches
- "git branch -m <old branch name> <new branch name> , this will change the local branch name
- "git remove -v" , this checks which remotes you have (assuming origin for these steps)
- "git push origin :<remote branch to delete>" , this will delete the remote branch
- "git push origin <new branch name>" , this will push the new branch name to the remote
| GIT command |
| |
| Sample Usage |
|
V. Diff changes
- use "git diff" to see what is still unstaged
- use "git diff -staged" to see what you have staged so far
- use "git reflog" provides a list of hashes which represent where you have been
- use "git diff <#commithash> <#commithash>" to compare differences between commit's
| GIT command |
| |
| Sample Usage |
|
git merge --squash <branch_name> 將另一個 branch 的 commit 合併為一筆,特別適合需要做實驗的 fixes bug 或 new feature,最後只留結果。合併完不會幫你先 commit。
git cherry-pick 321d76f 只合併特定其中一個 commit。如果要合併多個,可以加上 -n 指令就不會先幫你 commit,這樣可以多 pick幾個要合併的 commit,最後再 git commit 即可。
本文是一份Git教程,涵盖了从设置用户名和邮箱开始的基础知识,包括创建本地仓库、克隆远程仓库、新增文件并推送、获取远程仓库更新。对于进阶用户,介绍了分支管理、打标签、修改远程分支、重命名分支以及对比差异等高级操作。

4209

被折叠的 条评论
为什么被折叠?



