This is just a list of git commands to help me to remember and learn them Those could be a nice help to work together with others in bootcamps like codeio, 4geeks, and so on, or as a professional developer
- About
- PreIntroductory Commands
- GIT
- Initialize
- Git Status
- IMPORTANT NOTES!!!!
- Add elements in the staged area
- Remove elements from the staged area
- Creating the first instant copy of the proyect or staged elements
- Creating a commit and pushing the elements to the stage area with only one command
- Fixing the description message of last commit
- Reset
- Linux commands with git?
- See all the copies made by git for the project
- Return the project to an earlier stage
- Tags
- Branches
- Git Diff
- Stash
- Rebase
- GitHub
- Useful to see the current git version of if it is not installed at all
git --version
git help
man git
git help commit
git config --global user.name "Alejandro Salazar"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
- You can even modify all the current config from there instead of writing the commands again
git config --global -e
- git config –global alias.newAliasNameInventedByTheUser {command that comes after git}
Example:
git config --global alias.s "status --short"
git config --global alias.lg "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all"
git config --global -e
- Turn the current folder you are working on into a git repository
- This command will create the hidden folder .git
- Create the local repository
git init
- This command gives information about the current branch you are working on
- It also notifies which files in the current repository are untracked by git
- It also notifies about the files which are ready to be commited (the staged files)
- It shows all the files within the repository that have been modified including staged and untracked files
git status
- The “??” symbols means that those files or folders have not been added to stage where is being tracked by git
- The symbol “A” means the file/folder is already being tracked by git because of it being in the stage area
- The symbol “M” in color red means the stagged file/folder was modified, and those changes have not being reported to the stage area by the user
- The symbol “M” in color green means the stagged file/folder was modified, and those changes were already reported to the stage area by the user
- The symbol “R” means the file or folder was rename
- The symbol “D” means the file or folder was deleted
git status --short
git status -s
- git ignore folders that are completely empty
- Some projects could need an empty folder to save data generated by the program or user
- In order to avoid broken the project as a result of the default behavior de git, The user must create a folder with an special file called .gitkeep
Example:
mkdir uploads
touch uploads/.gitkeep
- There is going to be times, when you would want git to ignore some files that were created by third parties libraries you used in your project
- In order to achieve that, we need to create a special git file named gitignore in the repository root
- Some example of undesired files to keep a track of them are the node_modules, dist and a log of your local machine
Example:
- You create the file
touch .gitignore
- Inside the file you write which folders or files to ignore
dist/
node_modules/
*.log
- Command use to stage the untracked files to be ready for the commit
- The add command send the element to the stage area. Therefore, the next time you write the previous command, instead of the symbols “??”, it will appear and “A” symbol
git add index.html
git add *.html
git add folderName/*.js
git add .
- The css file contains other directories like bootstrap
git add css/
- Any of the two commands unstaged the desired file
git reset fileName
git rm --cached fileName
- Take a picture of the current repository which saves the current staged files
- Create a historical register of the repository with all the changes made in the staged files
git commit -m "my new message to identify this commit"
git commit -am "Description of the changes for this commit"
git commit --amend
git commit --amend -m "new description"
- Beware! Reset hard delete the changes but the soft one doesn’t
- you can add a number after the symbol ^ to notify how many commits you want to go back
- In other words, we preserved all the changes but return back to the desired commit in order to make a new commit which is going to include all those changes instead of creating new commits for the new additions
git reset --soft HEAD^
- This like the –soft state, it is not destructive.
- It preserve the changes in the repository but unstaged all the changes made after the selected commit
- you can select the desired commit to return by using the hash/id/identification
git reset --mixed 01cdac6
- The alphanumeric thing at the end is just an example about a possible visible value from git log in order to return to the expected git copy
- This command delete all the copies that comes after the selected commit or copy. You return in the time, as if all those commits have never existed before
git reset --hard ce82803
- This command is used to preserved a log about all the changes made in the repository, including those that where deleted by reset –hard
git reflog
- The changes in the files made through git are kept registered.
- Because of it being registered, you can easily recover them by using commands like git reset –hard
- If you use the typical linux commands to do the same, git will not interpret the changes like rename as what it is but as a creation of a new file
- That new interpretation is not desired because a rename file is losing all the registered changes it suffered since its creation if you rename the file by other means different than git mv unless you add them in the stage
git mv oldName newName
git rm fileName
git log --oneline
- Rebuild or return the project exactly as it was in the last commit
- It restore everything except the untracked filed
git checkout -- .
git checkout -- fileName
git checkout 762ac65 fileName
- This first command is not recommended due to lack of information
git tag super-release
git tag 29-10-22v1 -m "First Project Version"
git tag -a v1.0.0 -m "Version 1.0.0 lista"
git tag -a v0.1.0 d2ac819
git tag
git show tagName
git show v0.1.0
git tag -d nameOfMyTag
git branch nameOfMyBranch
- Both commands show the existing branches in the project, although it is more clear in git branch
- Git branch list all the branches and mark the exact branch you are currently working on
git branch
git log --oneline
git status # only shows the current branch
git branch -m currentName newName
git checkout nameOfMyBranch
git checkout -b nameOfMyBranch
- Return to the main branch and then use the command merge
git merge nameOfMyBranch
- The last two commands are used to force the deletion of the branch
git branch -d nameOfMyBranch
git branch -d nameOfMyBranch -f
git branch -D nameOfMyBranch
git diff
git diff --staged
- It is used to save the changes you have been working on, but are not ready to be integrated in the main project, and the same time you are forced to integrate into the project everything you have done.
- For obvious reason you save into the stash all those unfinished changes that will cause troubles in the main project if you integrate them.
- You only send to integrate the features which are finished and doesn’t cause troubles
- It is not recommended to create several stash, because it could be hard to integrate them into the project because of the possible conflicts the may arise
git stash
git stash save "Your meaningful description goes here"
git stash list
git stash list --stat
- Return in to the stash and delete it from the stash list
git stash pop
git stash apply stash@{2}
git stash clear
git stash drop stash@{0}
- show all the modification that were done in the stash
git stash show stash@{1}
- Moves the changes from the main branch to the branch you are working on
- Update your branch with the new content added in main after you created the branch
- You need to be positioned in your side branch in order to exec the rebase command
git rebase master
- You can also this command to edit the commits instead of combining them. Just watch the menu
For commits before HEAD (4)
git rebase -i HEAD~4
- If your your last commit could be divided in more commits you can use the rebase interactive to do it
- Uncommit and unstage the files with reset HEAD^
- make a regular commit for every file
- To finish the changes, you execute the command git rebase –continue
- At the end, you will have divided one big commit into several commits as a result of that
git rebase -i HEAD~3
git reset HEAD^
git rebase --continue
- Origin is the name for the remote repository
- We can create several remote repository for one project
git remote add origin URL
- Upstream is a the parent repository of your origin repository if it was forked
- It is a read only repository
git remote add upstream URL
command:
git remote -v
posible output:
git push --tags
- Use the command pull
git pull
git pull remoteName branchName
git pull upstream main
- This will add the information about your team mates branches
git pull --all
git branch --all
- After executing pull –all, you will be able of changing into your team member branch
git checkout yourTeamMemberBranch
- The first command only works if the branch has still not being deleted from the remote
git push origin :branchName
git remote prune origin
solution:
git config --global pull.ff only
solution:
git config --global pull.rebase true
- Update your local repository without forcing you to make a rebase or a merge
- Basically, it updata your references in order to see the complete history of your remote repertory
git fetch
** Work-flow in GitHub
- All the team member will be working in their own branch
- You can see their work by executing:
git fetch
git branch -a
git checkout theBranchOfYourFriend
- Any team member can merge another team member branch
git checkout master
git merge yourTeamMemberBranch
git push
- This is an Alternative to the last point (merge)
- It is more recommended because a pull request will notify the other team member and start a debate about the merging of that feature branch
git push origin featuredBranch
- The last command will update that branch in GitHub, allowing the user to start a Push Request from there
- Create the branch, move into it, do all the work there to implement the desired feature and then exec the command git push
- GitHub will tell you which command to use in order to update remote with your local feature branch
git push
- Finally you create a pull request and merge your featured branch with the main branch
git clone https://github.com/xandro2021/MyGitNotes.git
- Use the key word “Fixes #” with the number of the issue in github, and it will automatically close the issue
git commit -am "Fixes #5: Done, I've Deleted Capitan Marvel"
- Basically it is a repository inside another repository.
- Every repository will have their own history.
- The parent only knows and notify whenever the child repository is updated.
- You can add a submodule locally with this command
git submodule add nameOfChildRepository
git submodule add URL
git push --recurse-submodules=on-demand
- You need to mv into the child or submodule to execute the command
cd submoduleFolderName
git submodule init
git submodule update
git clone --recurse-submodules URL
- Some times when you clone a repository with its remotes recursively, the references used by the parent repository could be outdated. In order to update those submodules you need to exec the command:
git submodule update --remote
- Use the last command to manually delete the section related with the submodule using vim
git rm submoduleFolderName
rm -rvf .git/modules/submoduleFolderName
git config -e
- Then commit the changes and update remote
git add .
git commit -m "modules deleted"
git push
- Instead of mixing both repositories, a subtree just add in a folder the other repository similar than submodules
If you want to merge two repositories then the commands could be:
git remote add trick https://github.com/nasa/trick ; git merge origin/master
- By Default it comes in a different package in Fedora
sudo dnf install git-subtree
- master is the branch
- https is URL
- trick is the folder name in our current repository
git subtree add --prefix=trick https://github.com/nasa/trick master
- The squash key word, remove the history of the repository we are cloning as a subtree
git subtree add --prefix=trick --squash https://github.com/nasa/trick master
- Maybe to an older or newer version
git subtree merge -P trick --squash ba2df07