Skip to content

Instantly share code, notes, and snippets.

@zachallaun
Last active May 2, 2021 06:15
Show Gist options
  • Select an option

  • Save zachallaun/436ec0a88b36bbfd5002 to your computer and use it in GitHub Desktop.

Select an option

Save zachallaun/436ec0a88b36bbfd5002 to your computer and use it in GitHub Desktop.
Git Q&A

Title: A bit beyond Git basics: Q&A Date: 2013-10-04

Q: How do I create a new branch?

git branch <my-branch>

This creates a new branch that points to whichever commit you're currently on. Remember though that it doesn't change your current branch – it only creates a new one.

Q: How do I switch to another branch?

git checkout <my-branch>

This will change your current branch to <my-branch>, updating your filesystem to reflect the contents of whichever commit that branch points to.

Q: How do I create and switch to a new branch in a single command?

git checkout -b <my-branch>

Q: I've made whatever changes I want on my new branch. How do I merge these changes back into master?

(Make sure you're on whichever branch you're merging into – in this case, master.)

git checkout master
git merge <my-branch>

Q: After merging, git told me that I made a "fast-forward merge." What does that mean?

You branched off of the same branch that you're merging into, and it hasn't moved forward in the meantime. Your history might look something like this:

  2 - <my-branch>
  |
  1
 /
0 - master

When you merged <my-branch> into master, all that had to happen was to update master to point at the same spot as <my-branch>.

2 - <my-branch>, master
|
1
|
0

Q: After merging, git told me that there were auto-merge conflicts. What does that mean and how do I fix it?

Git tried to merge your two branches together, but found that some of the changes conflicted. If you type git status, you'll see that git was unable to merge some (or all) of the changed files. You'll need to manually resolve conflicts in the un-merged files.

If you open one of these files, you'll see something like this:

<<<<<<< HEAD
print "good luck"
=======
print "!!!"
>>>>>>> <my-branch>

This indicates the site of a conflict, including the contents of the file on the branch you're merging into (print "good luck"), and the contents of the file on the branch you're mergine (print "!!!"). To fix the conflict, just edit the file as you see fit.

print "good luck!!!"

As you fix conflicting files, add them to the staging area as usual with git add, and then use git commit to complete the merge.

Q: I committed in error. How do I undo a commit without losing my changes?

git reset --soft HEAD~1

A "soft reset" will move your branch back a number of commits (in this case 1), leaving the changes those commits introduced in your staging area.

Q: I hate my last commit. How do I undo it completely without keeping the changes?

git reset --hard HEAD~1

A "hard reset" will move your branch back a number of commits (in this case 1). None of the changes introduced by those commits will be in your staging area. (Note though that you don't ever lose those changes. If you do a hard reset in error, stop sweating and look up git reflog.)

Q: I forgot to add some change to my last commit. How can I fix it?

Stage the changes you want to be a part of the last commit and create an amend commit.

git commit --amend

An amend commit will add new changes to the changes of the previous commit and give you an opportunity to re-craft the commit message, ultimately creating a new commit containing all the changes you've specified.

Q: I messed up my last commit message. How can I fix it?

git commit --amend

If you have no changes in your staging area, doing a commit --amend will still allow you to rewrite the last commit message, creating a new commit.

Q: How can I stage only some of the modifications to a file?

git add -p <my-file>

The patch flag (-p / --patch) allows you to stage parts of a single file. @akaptur already has a great blog post about git add -p.

Q: How do I ignore files?

Let's say you want git to ignore those pesky .pyc files that Python generates. Create a .gitignore file in the root of your repository, containing this:

*.pyc

Stage and commit the file. This tells git to begin ignoring all files (even those contained in subdirectories) with the extension .pyc. If you later decide that there are other files or directories that need to be ignored, simply add them on new lines.

*.pyc
deps/
notes-tmp

Q: I accidentally began tracking a .pyc file in a git commit -a before I realized that it was a terrible, terrible thing to do. How do I begin ignoring it?

Git won't forget about a file that's already being tracked, even if it's extension is contained in your .gitignore. You have to tell git to "forget" about the file.

git rm --cached *.pyc

The --cached flag means that you don't mean to delete it from your file system, just that you want git to stop tracking it. This command added one or more deletions to your staging area, which you'll have to commit to finish up the process.

@HesamN1378
Copy link
Copy Markdown

How to show Matlab Simulink calculations in IBM Rational Rhapsody C++ Generated code?

Hi,

I have a project on embedded system and should link some C++ codes...

I used Matlab Simulink to make my model:

* Make a PID model

* Generate RealTime C++ code

I did every way which exist on internet or IBM guidance PDF, such as these steps:

* File > Add Profile to Model > Simulink > Add "Simulink.sbs"

* Make a class and set it's stereotype to "SimulinkBlock in Simulink"

* Right click on my class > Imported/Sync Simulink Model > Fill "Simulink Model File" and "Simulink Generated Source Code (C++)"

* Add new Attributes for my simulink block and other classes (same name as ports)

* shift my simulink block and other classes to a composite class > Right click > "Make an object"

* Link my ports (from my simulink block to other classes)

* Components > DefaultComponent > Configuration > DefaultConfig > Initialization > Tick my 'Composite Class's Name'

I use Visual Studio 2010 and have no problem for generating but when generate IBM C++ code, it is just some structure of my whole project and there is no code from my Simulink model! i need my PID model and it's calculations in C++ code which generated from IBM.

any help will appreciate ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment