CheatSheet for GitHub - Git Notes
Here is a quick review of Git and its commands which might be handy when in need.
Table of contents
- Repositories:
- Setup a local git repo
- Staging in git
- Commit in git
- Git logs:
- Add a remote to the local repo
- Branches:
- Branching Strategies:
- Mergin, Rebase and Conflicts:
- Tags in Git:
- Deleting Commits
- All about Commits
- Maintaining your work in the working branch during the development
- Export the stash into a patch file
- Stay updated with the Remote and fork
Repositories:
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
To create a local repo. create a folder and move into it.
execute the command git init
Staging in git
Whenever you create a git repo. You can include individual files which you want to maintain version.
Now you need to execute git add file_name this will include the file in the staging area.
You can only commit the files that are added.
Alternatively, you can execute git add . / git add -A (Which will include all the files)
Commit in git
A commit is a snapshot of the project and all the files included.
for every commit, we generate a unique hashcode to identify it.
To commit you can execute
git commit
git commit -m 'comments'
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.
once you use -u from next you can use git push/pull
Git logs:
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
If the repo is created in the local we have to set up a upstream.
use this command git remote add origin(remote name) < URL link >
use git remote -v to view the remotes.
Branches:
We have two types
Local branch
Remote Branch
git branch < new branch name > - To create branch
git branch < new branch name > xHexCode(commit id)
*git checkout/switch < branch name >
git branch -m < new branch name >*changes the current branch name*
git branch -m < old nbeanch name > < new branch name >(Non head branch)
git push -u origin/< local branch name> - this is to publish a local branch to the remote for first time.
git branch --track < local name> origin/< remote branch name> ( To get a branch from remote)
git checkout --track origin/< remote br name>
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.
To delete a branch check out other branch and delete the branch.
- git -d branch_name
To delete a remote the branch on the remote
git push origin --delete branch_name
Remember to delete all the counterpart branches.
We always integrate the changes from other branches INTO YOUR LOCAL BRANCH
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.
Rebasing the branches.
checkout to the base branch.
execute git rebase < branch name>
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:
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.
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.
One bug for each branch.
Mergin, Rebase and Conflicts:
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.
Rebase: Rebase makes history look like it went in one timeline. No branches happened.
checkout to the base branch.
execute git rebase < branch name>
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.
If you want to automatically take from branches.
git merge -X <ours|theirs>
ex: git merge -X ours login-feature.
Tags in Git:
Tags are used to maintain important details about the branch.
add using git tag -a // git tag -a v0.1 -m ’v0.1 stable release, changes from...’
Deleting the tags git tag -d < tag-name>
Deleting Commits
git reset --hard HEAD~ - last commit is deleted.
git reset --hard HEAD~ n - deletes n ancestors.
All about Commits
To visit a previous commit
use git checkout < 7d checksum>
use git checkout branch_name to get normal
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)
To remove the unstaged changes(This will permanently remove the changes)
- use git clean -f, meaning our project matches the most recent commit.
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
To store any unsaved progress of your work in the branch you are working can be done using the command called stash.
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.
git stash list - to list the current stash lists
git stash show -p stash@{<stash_number>} > <file_name>.patch - to export the changes into a new .patch file
git stash apply <file_name>.patch - to apply the stash changes in your current repository.
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
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