Let's kick it off to understand what Git is and how it works, then it'll be pretty easy for us to use Git effectively and joyfully.
First, let's go through the major difference between Git and other VCS such as SVN:
- Git stores file data as a set of snapshots, not a list of file-based changes or diff.
- Almost all operations in Git can be done locally, no network is required.
- Every commit will be check-summed before it's stored, and Git use SHA-1 for this checksum also known as commit id.
- No need to worry about changes lost or messed up, for Git almost only do add operations to git directory (git repo).
The basic Git workflow looks like below, and we need to make clear of these three stages so to execute Git commands flexibly.
- modify/new files in your
working directory
. - stage the files, adding snapshot of them to your
staging area
. - do a commit, storing the snapshot to your
git directory
.
Here're basic commands:
-
do not mess up with your github account, so better not use --global option
git config --global --add user.name "Firstname Lastname"
git config --global --add user.email "alias@microsoft.com"
-
clone from git remote repo, https, ssh or git protocol
git clone https://msasg.visualstudio.com/defaultcollection/bing_ads/_git/streamcomp
-
create branch
git branch -b <new branch name>
-
check your changes in working directory, it's good to do it before you do any other operations
git status
-
stage changes and commit
git add *.cs
thengit commit -m 'commit msg''
, or directlygit commit -a -m 'commit msg''
if there's no new ops -
merge with other branch
git checkout <your branch name>
, thengit merge <targeted branch name>
-
rebase with other branch
git checkout <your branch name>
, thengit rebase <targeted branch name> -i
-
push your changes to remote repo
git push origin <branch name>
, or you couldgit branch --set-upstream <branch name> origin/<branch name>
and then you can dogit push
to achieve the same
Here's the state transition diagram of your files
Here're some advanced ones:
-
stash changes in staging area, and apply to other branch, or clear them
git stash
,git stash list
,git stash apply
,git stash clear
-
remove untracked files
git clean -fn
will display those to be deleted,git clean -f
to remove them -
checkout specific remote branch
git remote -v
,git remote add <repo name> <remote repo url>
,git fetch <repo name> <remote branch name>
, thengit checkout -b <branch name> <remote branch name>
-
cherry-pick specific commit to your branch
git cherry-pick -n
cherry-pick to your working directory, orgit cherry-pick
to your branch directory -
ignore the changes of some config file
git update-index --assume-unchanged <your file>
,git update-index --no-assume-unchanged <your file>
-
find out who to blame : )
git blame <file>
.