-
Notifications
You must be signed in to change notification settings - Fork 12
Git Commands Memo
A small article to help you with day to day Git commands and common problems.
-
git status
- Your current repository status. -
git clean -f
- Removes untracked files. -
git clean -fd
- Removes untracked files & directories. -
git reset --hard
- Discard all local changes. -
git reset --hard origin/master
- Discard all local changes & local commits. -
git add -A
- Add all untracked fiels. -
git commit -am "<commit_message>”
- Commit. -
git rm --cached -r .
- Clear cache
-
git checkout -b <branch_name> master
- Create feature branch of master. Master branch name is optional if currently inside the master branch. -
git checkout <branch_name>
- Switch to the branch . -
git push -u origin <branch_name>
- Push your branch to the remote repository.
Sometimes after a sprint, all the remaining branches are just taking up space.
Here's a small snippet to remove all your local branches but keep the one you need <branch_name>
in one go.
git branch | grep -v "<branch_name>" | xargs git branch -D
git fetch origin
git reset --hard origin/master
-
git branch -d <branch>
- Delete a branch on your local filesystem. -
git push origin <branch>
- Delete a branch on the remote repository.
git pull --rebase origin master
- Base your commits on top of the original branch.
It is possible that a merge failure will prevent this process from being completely automatic. You will have to resolve any such merge failure and run git rebase --continue
. Another option:
-
git rebase --skip
- To bypass the commit that caused the merge failure. -
git rebase --abort
- Abort the rebase process.
Assume the following history exists and the current branch is topic:
A---B---C topic
/
D---E---F---G master
From this point, the result of the following command git pull --rebase origin master
would be:
A--B--C topic
/
D---E---F---G master
-
git push -f
- Once you are done with local rebase, you need to force push the changed branch to the remote repository.
In order to undo rebase firstly, we need to find the commit right before the rebase this can be done by using:
git reflog
Suppose the old commit was HEAD@{5} in the ref log:
git reset --hard HEAD@{5}
Then to complete action:
git push -f branch
When should I use cherry-pick?
The short answer is: as rarely as possible. The reason why you should use cherry-pick rarely is that it easily creates "duplicate" commits: when you integrate a commit into your HEAD branch using cherry-pick, Git has to create a new commit with the exact same contents. It is, however, a completely new commit object with its own, new SHA identifier.
Whenever you can use a traditional Merge or Rebase to integrate, you should do so. Cherry-pick should be reserved for cases where this is not possible, e.g. when a Hotfix has to be created or when you want to save just one/few commits from an otherwise abandoned branch.
You only need to provide the SHA identifier of the commit you want to integrate into your current HEAD branch:
git cherry-pick af02e0b
You can also instruct Git to only add the commit's changes to your Working Copy - without directly committing them:
git cherry-pick af02e0b --no-commit
Assume the following history exists and the current branch is topic:
A---B---C topic
/
D---E---F---G master
From this point, the result of the following command git cherry-pick F
would be:
A---B---C---F topic
/
D---E---F---G master
If you do now want to commit changes right after the cherry-picl
git cherry-pick -n F
If it's the first time you check-out a repo you need to use --init
first.
git submodule update --init --recursive
For git 1.8.2 or above, the option --remote
was added to support updating to latest tips of remote branches:
git submodule update --recursive --remote
For git 1.7.3 or above you can use
git pull --recurse-submodules
if you want to pull your submodules to the latest commits instead of the current commit the repo points to. See git-submodule for details
Create Tag For Last Commit
-
git tag <tag_name> HEAD
(for the last commit) -
git tag <tag_name> HEAD~1
(for the commit before HEAD) -
git tag <tag_name> HEAD~2
(for two commits before HEAD)
Delete a tag from remote repo
git tag -d [tag];
git push origin :[tag]
Git is a distributed version control system, meaning the entire history of the repository is transferred to the client during the cloning process. For projects containing large files, particularly large files that are modified regularly, this initial clone can take a huge amount of time, as every version of every file has to be downloaded by the client. Git LFS (Large File Storage) is a Git extension developed by Atlassian, GitHub, and a few other open source contributors, that reduces the impact of large files in your repository by downloading the relevant versions of them lazily. Specifically, large files are downloaded during the checkout process rather than during cloning or fetching. Read more how it works.
First of all, when you ready to use git lfs in your project here is what you need to do:
Install Git-LFS - Download Git-LFS Or if you on mac
Homebrew:brew install git-lfs
MacPorts: port install git-lfs
Install into your project.
git lfs install
Select files to track.
git lfs track "*.psd"
This will create or add extensions to the .gitattributes
file. Make sure to add this file into your reprository.
Now all the new files with *.psd
extension will be uploaded to LFS storage. But all the existing files will be still partf or the git and not part of lfs. To remove it from git and move/upload to lfs use:
git lfs migrate import --include="*.png,*.psd"
To print all the LFS tracked files use:
git lfs ls-files
Now if you wan't to remove files from LFS and put it back to git, here is an example how to do that
git lfs untrack "*.psd"
git rm --cached "*.psd"
git add -A
git commit -am "psd moved from lfs to git"
git push
The solution to some well-know mistakes when working with git-flow.
-
git checkout -b new-branch
- Create a new branch that has the current state. -
git checkout master
- Go back to the branch you want to remove the un-pushed commits from. -
git reset --hard origin
- Remove the un-pushed commits from the master.
- Check the repository
.gitignore
. - Check your global
.gitignore
.
- Windows cmd
%USERPROFILE%\.gitignore
- *nix or Windows git bash:
open ~/.gitignore_global
This is useful if you want to delete all your commit history but keep the code in its current state or part of the code. It is very safe to do it as in the following:
-
git checkout --orphan <name_you_choose_for_orphan_branch>
. -
git add -A
(you can remove any unwanted files manually). -
git branch -D master
- Delete the branch. -
git branch -m master
- Rename the current branch to master. -
git push -f origin master
- Finally, force update your repository.
git clean -fd
git rm --cached -r .
git reset --hard
git status
In case if this is not working (because this was not working for me), the following hack may work:
git rm --cached -r .
git reset --hard
git rm .gitattributes
git reset .
git checkout .
Reliable and high-quality Unity Development service. Let's Talk!
Website | AssetStore | LinkedIn | Youtube | Scripting Reference