Git commands Flashcards
What is the routine you should do when commiting changes to the code?
git add , (adds files to the staging area)
git commit -m ‘message’ (takes a snapshot of the repository)
git commit -am ‘message’ (shortcut to add files and commit changes)
git pull (pulls any changes from remote repository to local computer)
git push (pushes any local changes (commits to a remote server)
What do you need to do with merge conflicts?
If the same line was edited in different versions, it may result in merge confilict. Git by default will try to resolve it automatically, however sometimes it might require human action.
In such a case, locally you need to delete the lines that are not wanted (git provides support in those places) and push the results (this version will be marked as ‘the correct one’
What are the commands that support branching in git?
‘Branching’ is a feature of Git that allows a project to move in multiple different directions simultaneously. There is one master branch that is always usable, but any number of new branches can be created to develop new features.
git branch (lists all the branches currently in a repository) git branch (creates a new branch called feature) git checkout (switch working branch (move the HEAD) to feature branch.
How do you merge branches?
Go to the master branch.
git checkout master
Type git merge (to merge a feature branch)
In case a message appears: Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch
press 'i' (for insert) writhe message press esc write ':wq' for (write and quit) then press enter
After I am done with the branch i can
git branch -d
How can we break down git pull?
Git pull fetches the origin/master.
And merges it with local master, so we are exactly at the same spot.
How do you checkout to a commit?
git checkout SHA
the repository will most likely become have a DETACHED HEAD.
In order to fix that, we need to create a branch and checkout to it, then the changes could be commited.
git branch sample
git checkout sample
Deleting a branch
git branch -d branch_name
“How do I delete file from Github?”
git rm -r
alias gri=”git ls-files –ignored –exclude-standard | xargs -0 git rm -r”
How to revert a git repository to a previous commit?
git revert –no-commit 0766c053..HEAD
git commit
The –no-commit flag lets git revert all the commits at once- otherwise you’ll be prompted for a message for each commit in the range, littering your history with unnecessary new commits.)
How can you refresh a repository after changing line endings
When you set the core.autocrlf option or commit a .gitattributes file, you may find that Git reports changes to files that you have not modified. Git has changed line endings to match your new configuration.
$ git add . -u
$ git commit -m “Saving files before refreshing line endings”
$ git rm –cached -r . # Remove every file from Git’s index.
$ git reset –hard # Rewrite the Git index to pick up all the new line endings.
$ git add . # Add all your changed files back, and prepare them for a commit.
$ git commit -m “Normalize all the line endings” # Commit the changes to your repository.