Figure taken from https://homes.cs.washington.edu/~mernst/advice/version-control.html
The key is to understand that you are dealing with three objects: your working copy, your local git repository, and the remote repository on GitHub.
$ cd /where/you/want/to/save/your/project/ # do this in your local PC $ git clone email@example.com:yourgithubusername/yourproject.git
$ cd /path/to/your/project/ # do this in your local PC $ ls -a # check if there is .git/, if not, check previous section $ git pull # update local repository with remote one $ git status # check status of your working copy $ touch new.txt # create a new file $ git status # see if git detects the new file $ git add new.txt $ git commit -m "added" new.txt # working copy -> local repository $ git status # see if new.txt is added $ git push # local repository -> GitHub repository $ git status # see if there is anything else you need to deal with
If this is your first time to do this, Git may ask you to setup your name and email address. This is important if you work on your project with a team. Your teammates need to know who did what. That’s why Git needs to attach your name and email address to each commit you do. Git saves this information in
~/.gitconfig. Simply follow the instruction Git prints on your screen to create such a file. You can also check: https://help.github.com/en/github/using-git/setting-your-username-in-git. In short, you can run
$ git config --global --edit # create or edit ~/.gitconfig
and Git will open
~/.gitconfig using an editor. The default
~/.gitconfig looks like
# This is Git's per-user configuration file. [user] # Please adapt and uncomment the following lines: # name = User.Name # email = firstname.lastname@example.org
# in front of the last two lines and put your real name and email address there.
Another useful section you can add to your
[alias] br = branch co = checkout ci = commit st = status
which creates short aliases for some long Git commands.
Git does not store any information about the directory, just the files within it. If a directory is empty, simply rm it. Git does not need to know about it.
$ git branch # list existing branches $ git branch a-new-branch # create a new branch $ git checkout a-new-branch # switch to that branch $ git commit some.file # commit some file in that branch $ git push origin a-new-branch # sync that branch with server $ git checkout master # switch back to master branch $ git merge a-new-branch # merge a-new-branch to master $ git push origin :a-new-branch # delete a-new-branch on server $ git branch -d a-new-branch # delete it after deleting it on server
$ vi .gitignore $ git commit -m "updated" .gitignore
The ./gitignore file in the root directory of a project effects every directory inside the project unless the directory has its own .gitignore.
There are two scenarios
In case of failed merging
$ git status # to find which file has conflict $ vi theConflictFile # fix conflict manually $ git add theConflictFile $ git commit -m "conflict solved" -a # need to commit everything $ git push
The reason why one needs to run “git add” is because, sometimes, the file in one branch is deleted, git does not know whether it needs to keep the file or really delete it. In this case, one need to run “git add” to tell git that he does not want to really delete it; or one need to run “git rm” to tell git that he does want to really delete it.
In case of a binary file, the automatic merging is doomed to fail. If you want to keep the local copy, simply type in the last three lines of commands above, since your local copy has not yet been overwritten. If you want to keep the remote copy, do the following to overwrite the local copy with the remote version, and then type the last three lines of commands above.
git checkout origin/master file.bin
If you have not git commit changes, run
$ git reset things-added-but-not-committed
$ git rm --cached things-added-and-committed
The option “–cached” helps to keep the working copy. Since git rm won’t change the history. The files that you added will stay in the git history. It is complicated to completely remove them from the history. Deleting the .git directory and do git init again is much faster and simpler if you don’t care about the history.
Simply use “git mv” again to rename the file back.
git log does not show modified file by default. According to Stack Overflow, one has to use one of the followings,
$ git log --name-status $ git log --name-only $ git log --stat
$ git remote -v $ git remote set-url origin email@example.com:usr/project.git $ git remote -v
$ vi .git/config # change url setting inside directly
$ git diff-tree --no-commit-id --name-only -r SHA1
Ref. to [Stack Overflow] : http://stackoverflow.com/questions/424071/list-all-the-files-for-a-commit-in-git
According to [Stack Overflow], for a specific file use: : http://stackoverflow.com/questions/52704/how-do-you-discard-unstaged-changes-in-git
$ git checkout path/to/file/to/revert
For all unstaged files use:
git checkout -- .
Make sure to include the period at the end.
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git git fetch upstream git pull upstream master git push
Another way is to use branches instead of