Git for version control
Git is a great version control tool. It allows you to keep a history of your code, sync your project online and collaborate with others.
resource
- Online git simulator and tutorial: git branching
- git tutorial: git tutorial
- A visual git reference
BASIC
-
configurations
git config --global user.name "your name"
git config --global user.email "your.email"
-
initiate a git repository:
cd
into the directory where you want to initiate a git repository, and usegit init
-
chech status of current repository:
git status
.
It reports which branch you are on, what files are changed, and which files are not tracked. -
track a file:
If you want to keep track of the changes infile_A
, usegit add file_A
. -
rename or move a file:
git mv old_file new_file
-
remove a file:
git rm filename
with this command the file is removed both in repository and in file system.
If you just want to remove it from repository, but keep the file in system (simply untrack), use:
git rm --cached filename
-
check the changes in file with last staged version:
git diff filename
changes are marked with+
or-
-
show your commit history:
git log
# show latest n commits
git log -n
# simplified log
git log --oneline
git log --oneline [--decorate]
-
use
.gitignore
to specify files to be ignored
Usually this kind of files are product files, including but not limited to:- image files
- pdfs
- compiled code
- system files
Commits
- commit changes:
git commit -a -m "commit message" #commit all changes in repository
git add changed_file1 changed_file2 # add files to staging area
git commit -m "commit message" # commit changes in staging area
-
git show
: can be used to show various objects
eg:git show HEAD
—– show most recent commit -
change file back to its older version.
git checkout HEAD filename
change file back to its version inHEAD
. Can replace head with other commits. Without specification offilename
, the whole repository will be rewired. -
move current branch to a specified commit (first 7 digits, shown in
git log
):
git reset commit_id
See a discussion of
git reset
andgit checkout
: reset and checkout.
Topics
save new commits after git checkout
once you use git checkout commit_id
, your head is detached, and you are not on any branch anymore (or you are on an anonymous branch). You can still stage and commit changes, but they won’t show up on branch master.
To be able to keep those changes, you can do:
# put current changes in a branch: new
git checkout -b new
# merge new with master
git checkout master
git merge new # may have to manually adjust conflicts
see illustration save commits after git-checkout.
Branching
Branching of git is good for modulized work. Assume you have some intermediate product, and you are going to add another feature to it. It’s good practice to open up a new branch new_feature
, work on that branch until your new program passed all kinds tests, and then you can safely merge the new_feature
branch back to your master branch
.
This way if anything horrible happens to your new_feature
code, you can always return to your master branch without worrying that your intermediate product is also messed up.
-
check which branch I’m currently on:
git branch
-
add a new branch:
git branch new_branch_name
-
switch branch:
git checkout branch_name
-
merge a new branch to master branch:
git merge branch_name
-
delete a branch:
git branch -d branch_name
Remote
With git remote
you can freely transport your own git repository or copy others’ git repository.
-
clone git from
remote_repository
and put under folderclone_name
:
git clone remote_repository clone_name
will automatically give the remote repository a nameorigin
-
show current remotes:
git remote -v
-
add a new remote under name
remote_name
:
git remote add remote_name url
-
rename a current remote:
git remote rename cur_name new_name
-
change url of a current remote:
git remote set-url remote_name new_url
-
get most recent update from remote (saved in
origin/master
branch) :git fetch origin
If you want to move to the downloaded branch, use
git checkout -- track origin/master
-
push a branch in your repository to a remote repository:
git push remote_name branch_name
which pushes branch_name onto remote_name
Collaboration
Git can make life easier when you are doing collaboration work with other people. You may have a shared repository on Github, and each of your pull the repository to your local laptop, and work on different branches. Here’s how you contribute to the online repository:
1.git fetch
and git merge
(= git pull
) changes from the remote
2.Develop the feature on your branch and commit your work
3.git pull
from the remote again (in case new commits were made while you were working)
4.Push your branch up to the remote for review
At step 3 you can have a problem: after you make changes, the remote repository may have changed (maybe your collaborator has updated it). So git pull
would fail in this case, unless you resolve the fatal file differences. git mergetool
becomes very handy for this purpose (link).
Others
-
git grep string
:
search forstring
in git repository. This is extremely useful when you want to change the name of certain functions or variables, as you can locate which files contain the things you wanna change. -
git commit --amend
:
modify the most recentgit commit
message