Introduction There are ways to use Git locally for company protected project. However, there are also other ways to publish the code to make it wide spread, like using GitHub. Basics
How to use Git Locally? Steps: 1. create a git repo in your interested region
2. create some branch that you want to develop your code like try something new
3. Find error in branch, want to time to go back.
4. Finally you are satisfied with all the commits, and you think the master branch should be changed
0. How to use locally generated Git push to the Github? Steps: 1. create a GitHub repo using webportal 2. git remote add origin <weburl of that repo in first step> 3. git push --all git push --tags The step 3 actually push all the branches in current repo and push all tags into it as well. An easy way to do so without add the origin is that: just first git clone <weburl> then git push when git push, it first copy the master to origin/master, then the remote master will also be replaced by the master. 1. Copy a particular file in another branch to cover current branch? Answer: git checkout -p another_branch a.py 2. How to view the current branches like a graph in command line? Answer: add an alias in ~/.bashrc alias sbrc='git log --graph --full-history --all --color --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"' 3. How to push to GitHub without entering username and password? Answer: Using ssh key since github supports that. Step 1: Generate ssh key cd ~ ssh-keygen -t rsa Step 2: Check remote repo URL git remote show origin set it as git remote set-url origin git+ssh://[email protected]/username/reponame.git So that ssh is forced to open 4. How to discard changes since last commit in current branch? Answer: git resit --hard HEAD 5. How to remove file from tracking in git? Answer: git rm --cached a.txt; if you want to also remove the file locally, use git rm a.txt 6. Lazy way to staging files automatically before adding? Answer: Use git commit -a -m "initial commit" -a --allTell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected. With -m, you don't have to deal with the CTRL+X stuff... 7. How to see changes you have made on the files before going into staging area? Answer: Use git diff 8. More concise way to checkout the status of a git? Answer: git status -s 9. How to create a relative reference? Answer: git checkout branch1^ the ^ means go to the parent hash (parent commit) of branch1, means to go inverse in time for 1 commit of branch1. 10. How to update my LOCAL "remote repo" to the newest one with the TRUE remote repo? Answer: git fetch. Remember that it will not change your local working repo. It only updates the origin/blablabla, not blablabla Bouns answer: git pull = git fetch + git merge origin/blablabla which updates your current blablabla to the newest one from the origin. First the remote repo will be updated from TRUE remote repo. Second we will merge the current branch using origin/blablabla, which is the LOCAL remote repo. |
AuthorShaowu Pan Archives
December 2017
Categories
All
|