Skip to content

GaijinOtohp/git_notes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Git Notes

Lesson 01

  1. compare two files using the following command on command prompt:
  FC file1.extension file2.extension

The following commands are supposed to be run on "git CMD".

  1. To create a new version of a code use "Git" version control system, and create a commitment.
  2. Compare two versions of a file using git diff command followed with the two version IDs of the file.
  3. The best checkpoint to commit is after finishing a task in one location of the file (eg. modifying a function).
  4. If you changed multiple files in one task (add parameter to a function used in different file), you can commit multiple files at once.
  5. Get commits logs with statistics about which file have changes in each commit:
git log --stat
  1. Stop viewing the logs: press q, which stands for quit.
  2. Download a cloned repository from Github:
git clone the_link_to_the_repository_in_github
  1. Show colored outputs from git results:
git config --global color.ui auto
  1. change the version of the project to a different previous versions:
git checkout id_of_the_version

(HEAD is the commit that you're currently working on)

Configuring Git Bash to look much better

  1. Copy "git-completion.bash" file to home directory ("windows_user_name" folder).
  2. Copy "git-prompt.sh" file to home directory ("windows_user_name" folder).
  3. Copy ".bash_profile" file to home directory ("windows_user_name" folder).
  4. Set the default text editor for Git:
git config --global core.editor "'directory_of_the_editor_with.exe' -n -w"

(-n means open in a new window, -w means git will wait for the editor until the window is closed to continue).
example of the command:

git config --global core.editor "'C:/Users/windows_user_name/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe' -n -w"

(This editor will be used for writing your commit messages)

  1. Create a shortcut of the editor to run from "Git Bash":
    add the following line in the file ".bash_profile":
    alias shortcut_name="directory_of_the_editor".
    example:
    alias vscode="C:/Users/windows_user_name/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe"

  2. Run these two commands in "Git Bash" command line:

git config --global push.default upstream
config --global merge.conflictstyle diff3

Lesson 02

  1. Git stores its metadata about the logs of a repository in a directory named as ".git", which is a hidden folder.
  2. Set the Author's identity:
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
  1. Create a Git repository in a directory:
git init
  1. Get the status of the repository reflections:
git status

(reflections are the files or directories that are tracked by Git)

  1. Add a file or folder to the staging area so that it could be tracked in the repository:
git add name_of_the_file_or_folder
  1. Add everything in the working directory to the staging area:
git add .
  1. Remove a file or directory from the staging area:
git reset name_of_the_file_or_folder
  1. Commit changes to the files in staging area:
git commit

(This will open your selected default editor to allow you write your message for this commit. If no default editor is set to git configuration, it will give an error.)

  1. Commit changes directly with message:
git commit -m "your message"
  1. The staging area is a copy of the last commit.
  2. Compare files changed in the working directory that are not updated to the staging area:
git diff
  1. Compare files updated in the staging area and the last commit:
git diff --staged
  1. Discard any changes in the working directory and the staging area:
git reset --hard
  1. Get all branches created in the repository:
git branch
  1. Create a branch starting from the selected commit:
git branch name_of_the_branch
  1. select the branch where to attach new commits:
git checkout name_of_the_branch

(This will set HEAD to the last commit in the branch)

  1. Visualize branches connections in the repository in a graph:
git log --graph name_of_the_branch name_of_other_branch

You can aslo remove unwanted details:

git log --graph --oneline name_of_the_branch name_of_other_branch".
  1. Log command will only show the track of commits starting from the Initial commit through branches that leads to the commit where HEAD is attached to (each commit has its parents till the Initial commit).
  2. If a commit is created in detached HEAD status without creating a branch for it, then this new commit will be lost.
  3. Create a new branch for the new detached HEAD commit:
git switch -c name_of_the_new_branch
  1. Merge two branches and the selected branch as a new commit in the selected branch:
git merge name_of_the_first_branch name_of_the_second_branch

Or just merge the selected branch with another branch:

git merge name_of_the_other_branch

(You can either set your commit message by adding -m "your_message", or wait for the editor to open and type your commit message)

  1. Merging branches will move all commits of the branches to the checked branche where the merge will happen.
  2. When merging branches, some commits will get separated from each other and that will make comparing changes between commits hard.
    To compare a commit with its parent commit automatically:
git show commit_id
  1. Remove the commit where HEAD is attached to:
git reset --hard HEAD~1
  1. Remove a branch from the repository:
git branch -d name_of_the_branch
  1. If a branche is deleted then all commits within that branch will be discarded. Git garbage collector will get rid of them automatically (Git garbage collector run from time to time automatically).

  2. Collect garbage manually:

git cg
  1. Get log with a declared max of commits:
git log -n max_commits_number
  1. When merging two commits that contains changes to the same lines of code, then you'll have to decide which chages to keep in this conflict.

Lesson 03

  1. Create a remote repository in Github in your account.
  2. Copy the URL link to that remote repository.
  3. Connect the remote repository to your local PC repository:
git remote add any_name_for_the_remote the_URL_link_of_the_remote

example:

git remote add tri_repo https://github.com/GaijinOtohp/trial.git
  1. Get all remotes created with more detail:
git remote -v

(-v stands for verbose)

  1. Push data from local PC repository to the remote repository in Github:
git push name_for_the_remote name_for_the_branch_to_push

example:

git push tri_repo master
  1. If asked for username and password, insert your Github username and password.
    If annoyed of setting password each push then follow instruction in following link: https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git

  2. You can create and modify on files on Github directly. Other developers that are allowed to modify on your remote repository can also push and modify on files on your remote repository.
    (To do so just modify on the file and commit changes with a commit message)

  3. Pull data from the remote repository to the local reflections repository:

git pull name_of_the_remote name_of_the_branch_to_pull

example:

git pull tri_repo master

The branch that you want to pull must be the chekced one in your local repository (selected one). Otherwise, you will merge the selected branch with the pulled branch.

  1. Cloning a repository from a different developer will create a remote to that different developer named as origin.

  2. The name of the branch that is cloned will be named as name_of_the_remote/name_of_the_branch_pulled.
    example: origin/master

  3. Get all branches, even cloned ones:

git branch -a
  1. Forking is the same as cloning but just on your Github account with more features (You can copy the repository directly into your account without downloading it into your own machine, Github keeps track of the number of people who've made forks on a repository, all forks refers back to the original, it makes it easier to suggest changes to the original repository).

  2. Give permission to other developers to edit on your Github repository: insert their Github names in collaborators list.

  3. Update cloned local branches alone origin/master:

git fetch name_of_the_remote

example:

git fetch origin
  1. Ask the original repository owner to merge a branch of your creation to his repository: create a pull request from the new branch.
    The base repository by default is the original repository (the merge pull request is for the original repository)

  2. You can also merge the branches in your own fork: click edit on the page where the pull request is opened, and change the base fork to your own repository.

  3. Editing and pushing the branch of the pull request will also change the file in the pull request.

  4. Merging a commit that is ancestor to the other commit will make a Fast-Forward Merge (takes the label of the ancestor commit and moves it to the newer commit without creating a new commit of the merge).

  5. Fast-Forward Merge doesn't work when merging in Github.

  6. If a merge of a pull request has a conflict with changes of other pull requests, then you'll have to fix that conflict in your own machine by pulling the remote master into local master (which will also update origin/master), fixing the conflict, merging local master into the branch of the pull request, then pushing that branch of the pull request.

Additional notes

  1. Get remote repositories commits:
git remote update
  1. Show commits of all branches (local and remote repositories) each in one line:
git log --all --oneline --graph --decorate
  1. Copy a commit from a branch to the current branch:
git cherry-pick commit_id
  1. If a commit is deleted in the local repository but has already been pushed to the remote repository, then the next push from the local repository to the remote should be forced with lease. This command discards the deleted commit in the remote repository, but guarantees its content to be in the new commit.
    example:
git push --force-with-lease tri_repo master

About

Note of the usual commands in "Git Bash".

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages