CheatSheet for GitHub - Git Notes

Photo by Yancy Min on Unsplash

CheatSheet for GitHub - Git Notes

Here is a quick review of Git and its commands which might be handy when in need.

Repositories:

  1. There are remote repos and local repos.

    • Remote are the ones hosted on the server and everyone has access.

    • Local is the one present in the system.

Setup a local git repo

  1. To create a local repo. create a folder and move into it.

  2. execute the command git init

Staging in git

  1. Whenever you create a git repo. You can include individual files which you want to maintain version.

  2. Now you need to execute git add file_name this will include the file in the staging area.

  3. You can only commit the files that are added.

  4. Alternatively, you can execute git add . / git add -A (Which will include all the files)

Commit in git

  1. A commit is a snapshot of the project and all the files included.

  2. for every commit, we generate a unique hashcode to identify it.

  3. To commit you can execute

    • git commit

    • git commit -m 'comments'

  4. git push -u origin master(optional) this command will publish the master(local) to the remote repo and -u will be used to track the branch.

  5. once you use -u from next you can use git push/pull

Git logs:

  1. To view the logs or previous commits you can use the command

    • git log - full length log.

    • git log --graph - graphical commits.

    • git log --oneline - one line history.

Add a remote to the local repo

  1. If the repo is created in the local we have to set up a upstream.

  2. use this command git remote add origin(remote name) < URL link >

  3. use git remote -v to view the remotes.

Branches:

  1. We have two types

    • Local branch

    • Remote Branch

  2. git branch < new branch name > - To create branch

  3. git branch < new branch name > xHexCode(commit id)

  4. *git checkout/switch < branch name >

  5. git branch -m < new branch name >*changes the current branch name*

  6. git branch -m < old nbeanch name > < new branch name >(Non head branch)

  7. git push -u origin/< local branch name> - this is to publish a local branch to the remote for first time.

  8. git branch --track < local name> origin/< remote branch name> ( To get a branch from remote)

  9. git checkout --track origin/< remote br name>

  10. If we have tracking is set correctly we can pull push easily

    • git pull

    • git push

    • git branch -v

    • Ahead:

      • having uncommitted branches in local
  • Behind:

    • having commits in remote not pulled.
  1. To delete a branch check out other branch and delete the branch.

    • git -d branch_name
  2. To delete a remote the branch on the remote

    • git push origin --delete branch_name

    • Remember to delete all the counterpart branches.

  3. We always integrate the changes from other branches INTO YOUR LOCAL BRANCH

  4. Merging branches:

    • Switch into the branch in which you want to merge the changes into.

    • execute git merge < other_branch_name >

    • The merge creates a merge commit to give the message.

  5. Rebasing the branches.

    • checkout to the base branch.

    • execute git rebase < branch name>

  6. Comparing the difference in branches.

    • execute git log branch1...branch2

      • this will give the commits in b2 but not in b1
    • execute git log origin/main...main

      • this will give the commits diff in remote to the local

Branching Strategies:

  1. Strategy 1: We need to maintain long-running Branches.

    • One master, one Dev, and feature branches must be taken from the dev and integrated into the dev branch.
  2. Strategy 2: One version, One branch.

    • In this, we maintain different versions of one software and integrate features into different versions.

    • Each version will have a master dev etc branch and all the branches will have one common branch.

  3. One bug for each branch.

Mergin, Rebase and Conflicts:

  1. Merging branches:

    • Switch into the branch in which you want to merge the changes into.

    • execute git merge < other_branch_name > The merge creates a merge commit and gives the message.

  2. Rebase: Rebase makes history look like it went in one timeline. No branches happened.

    • checkout to the base branch.

    • execute git rebase < branch name>

  3. Conflict is when there is some different version at the same place conflict occurs we have to take the required files and add, and commit.

  4. If you want to automatically take from branches.

    • git merge -X <ours|theirs>

    • ex: git merge -X ours login-feature.

Tags in Git:

  1. Tags are used to maintain important details about the branch.

  2. add using git tag -a // git tag -a v0.1 -m ’v0.1 stable release, changes from...’

  3. Deleting the tags git tag -d < tag-name>

Deleting Commits

  1. git reset --hard HEAD~ - last commit is deleted.

  2. git reset --hard HEAD~ n - deletes n ancestors.

All about Commits

  1. To visit a previous commit

    • use git checkout < 7d checksum>

    • use git checkout branch_name to get normal

  2. To Undo a commit

    • use git revert <7d checksum you want to remove> this will ignore the changes in the commit specified and create a new commit identical to the previous 2nd commit.

A -> B -> C // git revert C // A -> B -> C -> D(same as B)

  1. To remove the unstaged changes(This will permanently remove the changes)

    • use git clean -f, meaning our project matches the most recent commit.
  2. To undo the last commit and have them as in the unstaged area use the reset command in git.

    • use git reset --soft [<commit>] to undo the changes in the commit and retain them under the staging area.

    • use git reset --hard [<commit>] to undo the changes and are not shown in the staging they are completely deleted. - Not to use it.

Maintaining your work in the working branch during the development

  1. To store any unsaved progress of your work in the branch you are working can be done using the command called stash.

  2. To save the files in a stash use the commands like follow.

    • use git stash -m "<your_stash_message>" --include-untracked here --include-untracked flag is to make sure you save any files which are not yet tracked in git(files added/deleted).

    • next use git stash list to view your list of stashes avaialble.

    • Then you can checkout to other branch or finish your work and the use command git stash apply <0,1,2.., stash_number> stas_number is the number available in stash stack.

    • To delete any old stash you can use git stash drop <number> or If you want to delete it immediately after using it once you can use git stash pop.

Export the stash into a patch file

We can export a stash into a new patch file and then we can share it with others.

  1. git stash list - to list the current stash lists

  2. git stash show -p stash@{<stash_number>} > <file_name>.patch - to export the changes into a new .patch file

  3. git stash apply <file_name>.patch - to apply the stash changes in your current repository.

  4. In-case the last command is giving the error you can open the .patch file using notepad and update the encoding to 'UTF-8' or 'UTF-16' then save it again.

Stay updated with the Remote and fork

  1. use the fetch command to get the commits into your repo.

    • git fetch origin master

    • Then merge it into the local master

    • Then push into your remote.

These are few commands I have learn and been using during my development. I will comeback and update this commands as I learn and use new ones.

Thanks,

Mallikarjun