Setup .gitignore
initial one folder for git / clone one folder from github / git server
git init # initial the git in the current folder
git init --bare # initial bare struture for push
git clone https://github.com/babylonlin/MDM---Live # clone the repository
git remote add origin https://github.com/babylonlin/MDM---Live # add repository to existing
git remote add origin git@github.com:babylonlin/MDM---Live # for ssh connection
git clone -b branchName remote_repo_url # clone only the branche
put .gitignore in the folder which should not be synced with git repository.
in the root folder, give general rules in the .gitignore and the folders as well, which should be ignored.
after every changes, (recommended to have multiple local small commits other than a big one)
stage the change:
git add -A # track all changes and add them to stage
git add filename # track changes of certain file
git add -u/--update # track modified and deleted files only, not the new ones
git add . # track all files as well
git commit -m "commit description" # make commits for upload, use git commit -a -m "commit without staging first"
git push # push the changes to git server/github
In case of inconsistency with git server/github, use the following commands to check possible reasons.
git rev-list origin/master..HEAD # list commits in the master branch (HEAD) that are not in origin
git log origin/master..HEAD # log the commits as well
git remote -v # list origin versions in teh remote server
git reset --soft HEAD^ # discard commits without changing local files
git reset --hard HEAD^ # delete local working directory changes
git diff commit-number1 commit-number2 # check the differences of two commits
remove just some files / folders in the repository
git rm -r --cached foldername # foldername to remove -r enable recursive deletion of content --cached enable deletion only on the repository
# e.g. git rm -r --cached *.file_extension
git commit -m "remove foldername"
git push origin master
git push origin master HEAD --force # force push to git server in case of commit inconsistency
git status # check commits details
git show commit_id # show specific commit details.
get the updated files from git server
the provided username and password will be remembered.
git config --global credential.helper store # store in file
git config --global credential.helper cache <timeout> #store in memory cache
git pull
initialize existing folder
git init
git remote add https://github.com/username/repository
git config --global credential.helper store
git fetch --all
git reset --hard origin/master
change remote server
git remote rm origin
git remote add origin http://linkaiyi@h2855032.stratoserver.net:8080/mdm.git
git config master.remote origin
git config master.merge refs/heads/master
check the commits of history and print the details
git log # show all historical commits
git log -n # git log -1 show the last commit
git log -p -n #show last n commits and details of the commits, similar to git diff commit1 commit2
git log --stat -n # show statistics of the last n commits
git reflog # show HEAD history use git reset HEAD^ back to one commit, git reset HEAD^^ go back 2 commits, git reset --hard SHA-1(7 digits are enough for most of cases)
git checkout -- filename: if filename exists in staging area, roll back the staging area version, otherwise roll back the last commits
git tag tagname # tag one snapshot and make the snapshot
make new branches and merge to master
git branch linuxprobe # make new branch linuxprobe
git checkout linuxprobe # switch to linuxprobe branche
.... # do something
git checkout master # switch to master
git merge linuxprobe # merge the linuxprobe to master. In case of complicated changes, need to modifiy the files and merge again
git branch -d linuxprobe # delete the branch linuxprobe
git push origin --delete test # delete remote branch test
save the files changes and working directory temporarily, e.g. for changing branch
git stash # save the directory
git stash pop # restore the last saved stash
git stash list # list stashed modifications
git stash show # show stash channged files
git stash show -p # show stash changed content
git stash is saved globally per repository. use git stash list to get list of stashes and use stash@{number} to refer specific stash
To apply a stash and remove it from the stash list, run:
git stash pop stash@{n}
To apply a stash and keep it in the stash cache, run:
git stash apply stash@{n}