Working on a personal forked repository
Most of the time when you are working in an organisation who uses git, you will be requested to fork the project and create pull for your feature or bug fixes (Most of the open source contribution in git will be happening like this).
In this note I will let you know some useful git commands which will be very helpful for you in your day to day life with git. I am not going to explain any git technical terms. I am assuming you already know about it or all those terms are linked to a proper source for more information.
In this note I will let you know some useful git commands which will be very helpful for you in your day to day life with git. I am not going to explain any git technical terms. I am assuming you already know about it or all those terms are linked to a proper source for more information.
Lets start.
git clone git@github.com:<your-github-id>/Project.git
- to clone your forked Project (personal repo)cd Project/
- get into your repositorygit fetch origin
- will fetch all the details/indexes (branches and commits) from remote origin but will not apply.git branch
- is used to check all the available branches and current working branch (working branch will have * prefixedgit checkout origin/master
- change your current working branch to master.git pull origin master
- update your local branch to with your fork repo in the remote(origin)git add file_path
- add this file to commitgit rm file_path
- delete file from gitgit commit
- commit your changes (It is allways good to have your repo uptodate(pull the code) before you do a commit else you may be getting a merge commit).git push origin master
- push your commits to remote repositories master branch.
Changing the last commit
- Don't use this if you pushed the code to remote repo (specifically public repo) - because you have to do -f push which will change the history of others commit also if any.
- Do all the changes same as you do for a normal commit (
add
,rm
) and do commit with --amend flag git commit --amend
this will overwrite your last commit, don't worry your previous changes will also be there.- If you just want to change the commit message, simply enter git commit --amend which will prompt you for commit message with last commit message auto filled.
- For more info refer Changing the last commit
What is git stash?
- It is simply a backup for your local modified files since last commit of the current branch.
git stash save
- save/backup modified filesgit stash list
- list all the saved stashgit stash pop
- apply saved/backed up files to the current branch
Sync/Update your fork(personal repository) with the upstream(remote repository)
git remote -v
- to list out all the remote repositories(remote url's and it alias names) you have added. origin will be by default created and pointing to the repository from where you clone the project.git remote add upstream git@github.com:remote/Project.git
- add upstream as a remote which points to main repository of your projectgit remote -v
- Check you new remote listedgit fetch upstream
- fetch all the details(branches and commits) from remote "upstream"git stash save
- Save your local changes if anygit pull --rebase upstream master
- update your local repo with upstream repo ( --rebase is used to avoid merge commit ) make sure you are not much deviated remote repository else you will end up reolving conflicts for a long time.git satsh pop
- if you did stash save before, apply this to the new updated code now.git push origin <branch_name>
- update your fork with the local changes- For more info refer: Sync a fork with upstream
Merge/Squash branches
- To merge all the changes from branch1 to current branch
git merge --squash <branch1>
this commmand will get all the changes from your branch1 to your current branch and will not commit it. You can see the modified files usinggit status
.- Now proceed to commit as usual and dont forget to delete the branch1, else your changes will still be there and create problem when you do merge (squash) for next fix.
- Ref: git merge doc
Update/sync a branch with other branch changes
git checkout <fix1_branch>
git rebase <sync_from_branch>(can be master)
or
git rebase <sync_from_branch> <fix1_branch>
this will update your branch and apply all the changes of current branch on top of it.- rebase will update the git log also, whereas squash will just merge the changes but will not commit the same
Reset your fork to upstream
- You messed up your fork and wanted to reset your fork to upstream as if you forked freshly.
git reset --hard upstream/master
- your all local changes will be WIPED OUT (master is a branch name you want to reset to from upstream)- Now update your fork forcely -
git push origin <branch> -f
Creating and applying patch
- Patch of an entire branch
git format-patch master --stdout > fix_branch.patch
- your current branch should be <fix_branch>. - Above command will create a new file fix_branch.patch with all changes from the current branch (fix_branch) against master branch.
- Create patch for a single commit
git format-patch -1 commitssh --stdout > commit_name.patch
- Patch of an entire branch
- Check what are all the changes in the patch using
git apply --stat fix_branch.patch
- Check whether you have any issues appying this patch by
git apply --check fix_branch.patch
- Now apply the patch using
git apply < fix_branch.patch
- you can see the applied changes ingit status
- When you apply patches created from someone else you can use
git am --signoff < fix_branch.patch
- so that signed off message will be appended to your commit.
- Alternatively you can use patch command
patch -p1 < path/file.patch
- Check what are all the changes in the patch using
Revert all permission changes
git diff -p -R
- See all the diff in reverse ordergrep -E "^(diff|(old|new) mode)" >revert_modes
- Take all the mode changes alonegit apply <revert_modes
- Apply all those reversed permission (mode) changes- It can be done in a one liner -
git diff -p -R | grep -E "^(diff|(old|new) mode)" | git apply
Creating a git alias
git config --global --add alias.alias_name 'your_git_command'
- will add a git command alias- eg:
git config --global --add alias.update "git stash save && git pull --rebase upstream && git stash pop"
- now you can simply usegit update
.
Comments
Post a Comment