- Quick Reference
- Quick Reference - The Basics
- Quick Reference - Undoing Changes
- Quick Reference - Branches I
- Quick Reference - Branches II
- Quick Reference - Rebasing
- Quick Reference - Rewriting History
- Quick Reference - Remotes
- Quick Reference - Centralized Workflows
- Quick Reference - Distributed Workflows
- Quick Reference - Patch Workflows
- Quick Reference - Tips and Tricks
- Quick Reference - Plumbing
- Rename a branch
- Merging 2 git reporsitories into One, keeping history
- Reset only one file
- Set mergetool
- Use vimdiff to solve conflicts
- Update Github token
- Create a remote repository from a local repository using gh command
- Add a repository inside a repository
git config --global user.name "c4arl0s"
git config --global user.email c.santiago.cruz@example.com
git config --global core.editor vim
touch ~/.git-message.txt; echo "subject line" > ~/.git-message.txt; echo "What happened" >> ~/.git-message.txt;
git config --global commit.template ~/.git-message.txt
echo "*~" > ~/.gitignore_global; echo ".DS_Store" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
git init
Create a git repository in the current folder.
git status
View the status of each file in a repository.
git add fileName
Stage a file for the next commit.
git commit
Commit the staged files with a descriptive message.
git log
View a repository's commit history.
git config --global user.name "<nameOfTheUser>"
Define the author name to be used in all repositories.
git config --global user.email <email>
Define the author email to be used in all repositories.
git checkout commitID
View a previous commit.
git tag -a tagName -m "description"
Create an annotated tag pointing to the most recent commit.
git revert commitID
Undo the specified commit by applying a new commit.
git reset --hard
Reset tracked files to match the most recent commit.
git clean -f
Remove untracked files.
git reset --hard / git clean -f
Permanently undo uncommitted changes.
git branch
List all branches.
git branch <branchName>
Create a new branch using the current working directory as its base.
git checkout <branchName>
Make the working directory and the HEAD match the specified branch.
git merge <branchName>
Merge a branch into the checked-out branch.
git branch -d <branchName>
Delete branchName
git rm <fileName>
Remove a file from the working directory (if applicable) and stop tracking the file.
git push origin --delete <remote-branch-to-remove>
This flag allows you to delete a remote branch. (--delete)
git commit -a -m "messageToCommit"
Stage all tracked files and commit the snapshot using the specified message.
git branch -D branchName
Force the removal of an unmerged branch (be careful: it will be lost forever).
git rebase newBase
Move the current branch's commits to the tip of new-base, which can be either a branch name or a commit ID.
git rebase -i newBase
Perform an interactive rebase and select actions for each commit.
git commit --amend
Add staged changes to the most recent commit instead of creating a new one.
git rebase --continue
Continue a rebase after amending a commit.
git rebase abort
Abandon the current interactive rebase and return the repository to its former state.
git merge --no-ff branchName
Force a merge commit even if Git could do a fast-forward merge.
In this case that I am going to document I found that I wanted to commit in my history a commit which contains a work that I didn't want to lose. So first, I tried to rebase main with C1 and C2, but C1 had merge conflicts with the last commit on main branch.
This branch was not essential for the project because the idea was too complex, so I just wanted to commit the executed work. So in order to commit this work you have to squash C1 and C2 in one commit. You can do this doing an interactive rebase.
Solution 1:
- Remove last commit (it has minimum changes on main branch)
- Then try to do the interactive rebase.
- Once you rebase the main branch you are able to marge this branch.
- After merging, you are able to revert this commit, which contains the "executed work" and keep it in the history.
The interactive rebase was successful, because it is a fast forward. In this case last commit had minimum changes so I was able to discard them.
Solution 2:
- In order to keep "last commit". You have to rebase your branch with minimum number of commits, I mean one commit. So then try to do the rebase.
- Once you are able to do the rebase, you merge this branch and then revert this commit, remember, this commit it is juts to insert this effort in the history line.
- Start an interactive rebase:
git rebase -i HEAD~n
n: is how far do you want to go back in history
- Your default text editor will open. At the top, a list of your latest
n
commits will be displayed, in reverse order.
pick a5f4a0d commit-1
pick 19aab46 commit-2
pick 1733ea4 commit-3
pick 827a099 commit-4
pick 10c3f38 commit-5
pick d32d526 commit-6
- replace
pick
keyword withsquash
keyword in order to try to squash those commits:
pick a5f4a0d commit-1
squash 19aab46 commit-2
squash 1733ea4 commit-3
squash 827a099 commit-4
squash 10c3f38 commit-5
pick d32d526 commit-6
-
Save and exit.
-
Git will apply all changes and will open again your editor to merge the three commit messages. You can modify your commit messages or leave it as it is (if so, the commit messages of all commits will be concatenated).
git reflog
git reset --mixed HEAD~n
Move the HEAD backward n commits, but do not change the working directory
git reset --hard HEAD~n
Move the HEAD backward n commits, and change the working directory to match.
git log since..until
Display the commits reachable from until but not from since. These parameters can be either commit ID's o branch names.
git log --stat
Include extra information about altered files in the log output.
I used to do git reset HEAD~
to include insignificant changes in the last commit. But after including those changes, I did included important changes. These important changes were not good, so I was losing my functional version of the project. To revive the commit you have to do this:
git reflog
- Look for the commit you remember have the functional version.
- go to the commit with the functional version:
git checkout <commitWithFuncionalVersion>
- Then:
git checkout -b functionalBranch
- Now: go back to the main branch:
git checkout main
. - Finally merge the functional branch:
git merge functionalBrancg
- Remember: with git you don't miss any information.
git clone <remotePath>
Create a copy of a remote Git repository
git remote
List remote repositories.
git remote add <remoteName> <remotePath>
Add a remote repository
git fetch <remoteName>
Download remote branch information, but do not merge anything
git merge <remoteName>/<branchName>
Merge a remote branch into the checked-out branch
git branch -r
List remote branches
git push <remoteName> <branchName>
Push a local branch to another repository
git branch -r
List remote branches.
git push <remoteName> <branchName>
Push a local branch to another repository
git push <remoteName> <tagName>
Push a tag to another repository
- Create a repository on github, name: RepositoryOnGithub
- Navigate to the original repository you want to use
cd /path/to/original/repository
- Push the master or main branch, force to delete the trash you create after creating it.
git push -f https://github.com/c4arl0s/RepositoryOnGithub.git master:main
- Clone RepositoryOnGithub repository. It will contain all the files and history of the original repository.
git clone https://github.com/c4arl0s/RepositoryOnGithub.git
git init --bare <repositoryName>
Create a Git repository, but omit the working directory
git remote rm <remoteName>
Remove the specified remote from your book-marked connections.
git format-patch branchName
Create a patch for each commit contained in the current branch but not in branchName. You can also specify a commit ID instead of branchName
git am < patchFile
Apply a patch to the current branch
git archive branchName --format=zip --output=fileName
Export a single snapshot to a ZIP archive called fileName
git bundle create fileNam branchName
Export an entire branch, complete with history, to the specified fileName
git clone repo.bundle repoDirectory -b branchName
Re-create a project from a bundle repository and checkout branchName
git stash
Temporarily stash change to create a clean working directory
git stash apply
Re-apply stashed changes to the working directory
git diff commitID..anotherCommitID
View the difference between two commits
git diff
View the difference between the working directory and the stagging area.
git reset HEAD fileName
Unstage a file, but don't alter the working directory or move the current branch
git checkout commitID fileName
Revert and individual file to match the specified commit without switching branches
git config --global alias.aliasName gitCommand
Create a shortcut for a command and store it in the global configuration file
<<<<<<< Updated upstream
echo .DS_Store >> ~/.gitignore_global
then:
git config --global core.excludesfile ~/.gitignore_global
Now each commit you will not be able to add any .DS_Store
file.
If you already add a .DS_Store file in your repo, you have to delete it manually.
You can create a pull request on your own project just pushing your branch
Console output:
git push origin <nameOfTheBranch>
Then on Github will appear the message that you can generate a pull request, follow the instructions and ask for the pull request, someone in the project then can merge your branch on main or develop branch.
Once you did push (your buddy) a branch to the repository, you can fetch the branch to your macbook with these commands:
- Step 1: From your project repository, bring in the changes and test.
git fetch origin
git checkout -b CSC/13-how-to-fetch-a-branch-and-merge-Only-Test origin/CSC/13-how-to-fetch-a-branch-and-merge
In this case I had to create another branch with different name in order to test the branch that it is on
origin
(because I have not buddy :-( )
-
Step 2: Run a test, verify if everything is ok.
-
Step 3: Merge the changes and update on GitHub.
git checkout master
git merge --no-ff CSC/13-how-to-fetch-a-branch-and-merge
- Step 4: Push the current branch (master) to origin.
git push origin master
If you already created these .DS_Store
files, use this command.
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
Console output example:
rm 'Projects/.DS_Store'
rm 'Projects/1ButtonsProjectCommonInputControls/.DS_Store'
rm 'Projects/2SwitchesProjectCommonInputControls/.DS_Store'
rm 'Projects/3SlidersProjectCommonInputControls/.DS_Store'
rm 'Projects/4TextFieldsProjectCommonInputControls/.DS_Store'
rm 'Projects/5ActionsAndOutletsProjectCommonInputControls/.DS_Store'
rm 'Projects/6GestureRecognizersProjectCommonInputControls/.DS_Store'
rm 'Projects/7ProgrammaticActionsProjectCommonInputControls/.DS_Store'
rm 'Projects/LabStep1CreateYourViewInInterfaceBuilderTwoButtons/.DS_Store'
rm 'Projects/LabStep2CreateOutletsAndActionsTwoButtons/.DS_Store'
rm 'Projects/LabStep3AddCodeForYourActionsTwoButtons/.DS_Store'
git cat-file type objectID
Display the specified object, where type is one of commit, tree, blob, or tag.
git cat-file -t objectID
Output the type of the specified object.
git ls-tree treeID
Display a pretty version of the specified tree objects.
git gc
Perform a garbage collection on the object database.
git update-index --add file
Stage the specified file, using the optional --add flag to denote a new untracked file.
git write-tree
Generate a tree from the index and store it in the object data-base. Returns the ID of the new tree object.
git commit-tree treeID -p parentID
Create a New commit object from the given tree object and parent commit. Returns the ID of the new commit object.
Console output:
git branch -m old-name-branch new-name-branch
Rename a branch
- Create a Merge-2-Unrelated-Projects directory
- Inside Merge-2-Unrelated-Projects clone both repostories, Example: trad3 and DictEnEsScript
- Change to trad3 directory repository (4 years old).
- Type this command:
filter-branch --index-filter \
'git ls-files -s | sed "s-control+v, TAB\"-&trad3/-" |
GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
git update-index --index-info &&
mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD
Remember the line where it says
trad3
- Create a new repo, where first and second project are located, in this case I will call it DictEnEs (shorter)
mkdir DictEnEs
Then:
cd DictEnEs; git init
- Add the 2 repos as remote repos for the new "DictEnEs"
git remote add --fetch trad3 ../trad3
git remote add --fetch
DictEnEsScript ../DictEnEsScript
- Merge the 2 remote repos
git merge trad3/master --allow-unrelated-histories
- Take a look and see that the log is present:
git log
- Move these new files into a new directory to avoid conflicts,
mkdir OldTrad3Repository
and move all files. - Create a new commit with these new files
git add .; git commit -m "add files that comes from old trad3 repository"
- Now merge the files that come from the second repository
git merge DictEnEsScript/master --allow-unrelated-histories
As you can see the merge is successfully. WoW!.
Thank you to Medhat Dawoud Tutorial
git restore fineName
git config --global merge.tool vimdiff
- After executing the command merge, lets say that you are working on a branch and it is needed to merge new updates to your work:
git merge main
- Your console is going to say that there is merge conflics, then you invoke vimdiff as:
git mergetool --tool=vimdiff
-
Once you finish editing all the files that contains conflicts, save and create a new commit.
-
Make sure that all is up to date.
If you lost the current tokens, create a new one, and update your repository access.
- Authenticate if necessary:
gh auth login
- Start using git:
git init
- Create repository:
gh repo create
git submodule add git@github.com:c4arl0s/5iOSApplicationDevelopment.git